623 - 500!

All about problems in Volume 6. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

uvasarker
Learning poster
Posts: 96
Joined: Tue Jul 19, 2011 12:19 pm
Location: Dhaka, Bangladesh
Contact:

Re: 623 - 500!

Post by uvasarker » Tue Nov 06, 2012 9:08 pm

WOW! ACCCCC! thanks guru.

angileena
New poster
Posts: 1
Joined: Fri Jan 11, 2013 9:56 am

Re: UVA Problem ID :623(Why i am getting Time Limit Excedded

Post by angileena » Fri Jan 11, 2013 9:58 am

Nice surprise about this shorter form. Can you tell us what it is you are talking/writing about?
Has any info. about this shorter form been posted before (and i just missed it)?

naim
New poster
Posts: 3
Joined: Fri Jan 11, 2013 10:46 am

623-500! i got it WA plz anyone help me

Post by naim » Fri Jan 11, 2013 11:00 am

#include<stdio.h>
#include<string.h>
int fact(int i);
char x[120005];
int len;
int main()
{
int n,i;
while(scanf("%d",&n)==1)
{
//if(n>500)
//continue;
printf("%d!\n",n);
if(n==0 || n==1)
{
printf("1\n",n);
continue;
}
x[0]='1';
x[1]='\0';
len=1;
for(i=2;i<=n;i++)
fact(i);
for(i=len-1;i>=0;i--)
printf("%c",x);
printf("\n");
}
}
int fact(int i)
{
int carry=0,j;
int value1,value2;
for(j=0;j<len;j++)
{
value1=(int)x[j]-48;
value2=i*value1+carry;
carry=value2/10;
x[j]=(char)(value2%10+48);
}
while(carry>0)
{
value2=carry%10;
x[j++]=(char)(value2%10+48);
carry=carry/10;
}
x[j]='\0';
len=j;
}

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: UVA Problem ID :623(Why i am getting Time Limit Excedded

Post by brianfry713 » Fri Jan 11, 2013 8:31 pm

Precompute all the possible outputs before reading the input.
Check input and AC output for thousands of problems on uDebug!

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 623-500! i got it WA plz anyone help me

Post by brianfry713 » Fri Jan 11, 2013 8:39 pm

That is AC code.
Check input and AC output for thousands of problems on uDebug!

dibery
Learning poster
Posts: 76
Joined: Sat Feb 23, 2013 4:16 pm
Location: Taiwan, Taipei
Contact:

623 - 500!

Post by dibery » Sat Feb 23, 2013 4:25 pm

Hello, I'm having some problem.
I've checked the answer from 0! to 1000!, but I still couldn't find the bug.
Can somebody help me? Thanks.

Here's my code:

Code: Select all

Accepted.
Last edited by dibery on Sun Dec 29, 2013 7:06 am, edited 1 time in total.
Life shouldn't be null.

lbv
Experienced poster
Posts: 128
Joined: Tue Nov 29, 2011 8:40 am

Re: 623 - 500!

Post by lbv » Sun Feb 24, 2013 12:49 am

dibery wrote:Hello, I'm having some problem.
I've checked the answer from 0! to 1000!, but I still couldn't find the bug.
Did you really check the output for 0! ?

dibery
Learning poster
Posts: 76
Joined: Sat Feb 23, 2013 4:16 pm
Location: Taiwan, Taipei
Contact:

Re: 623 - 500!

Post by dibery » Sun Feb 24, 2013 11:06 am

Oh, I was so silly...
Sorry for asking such an easy problem...
I thought 0! = 0
Thank you.
Life shouldn't be null.

helli3papa
New poster
Posts: 8
Joined: Wed Apr 11, 2012 5:48 pm

Re: 623 - 500!

Post by helli3papa » Mon Sep 16, 2013 12:28 pm

Hi All
Can someone help me?
I Got TLE but I try 1000! and my code answer in 2 sec in release mode :cry:

Code: Select all

#include <iostream>
#include <string>
#include <algorithm>

using namespace std;
int a[2000], b[2000], c[2000];
int number = 3;
long long Max = 0, Min = 100000007, f = 0;

int len_num (int n)
{
	int ans = 0;
	while(n > 0)
	{
		ans++;
		n /= 10;
	}
	return ans;
}

void makea(int n)
{
	int n2 = n;
	for (int i = 0; i < len_num(n2); i++)
	{
		a[i] = n % 10;
		n /= 10;
	}
	reverse(a, a + len_num(n));
}
void makeb(int n)
{
	int n2 = n;
	for (int i = 0; i <= len_num(n2); i++)
	{
		b[i] = n % 10;
		n /= 10;
	}
	reverse(b, b + len_num(n));
}

void mult ()
{
	long long m = Max;
	long long o = 0;
	for (int i = 0; i < Max; i++)
		c[i] = 0;
	for (int i = 0; i < Min; i++)
	{
		for (int j = 0; j < Max; j++)
		{
			c[j + o] += a[i] * b[j];
			m = max(m, j + o + 1);
		}
		o++;
	}
	for (int i = 0; i < m; i++)
	{
		if(c[i] > 9)
		{
			c[i + 1] += c[i] / 10;
			c[i] = c[i] % 10;
			if(i == m - 1)
				m = m + 1;
		}
	}
	Max = m;
	for (int i = 0; i < m; i++)
	{
		b[i] = c[i];
	}
	makea(number);
	Min = len_num(number);
	number++;
}

void print()
{
	bool flag = false;
	for (int i = 0; i < Max; i++)
			cout << b[i];
	cout << endl;
}

int main()
{
	string s, s2;
	int facto;
	makea(100);
	while(cin >> facto)
	{
		number = 3;
		if(s == "0" || s2 == "0")
			cout << 0 << endl;
		else
		{
			Max = 1; //max(s2.size(), s.size());
			f = 0;
			for (int i = 0; i < 2000; i++)
			{
				a[i] = 0;
				b[i] = 0;
				c[i] = 0;
			}
			if(s.size() < s2.size())
				f = 1;
			else f = 2;
			makea(1);
			Min = 1;
			makeb(2);
			if(f == 2)
				swap(a, b);
			//:))
			while(number <= facto + 1)
				mult();
			reverse(b, b + Max);
			/*bool flag = false;
			for (int i = 0; i < (Max + 1) / 2; i++)
			{
				swap(c[i], c[Max - i + 1]);
			}*/
			cout << facto << "!" << endl;
			print();
			//:D
		}
	}
}

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 623 - 500!

Post by brianfry713 » Tue Sep 17, 2013 12:18 am

Precompute all the possible outputs.
Check input and AC output for thousands of problems on uDebug!

helli3papa
New poster
Posts: 8
Joined: Wed Apr 11, 2012 5:48 pm

Re: 623 - 500!

Post by helli3papa » Tue Sep 17, 2013 9:53 pm

TY brainfry
But why my code is wrong?
Last edited by helli3papa on Wed Sep 18, 2013 3:09 pm, edited 1 time in total.

helli3papa
New poster
Posts: 8
Joined: Wed Apr 11, 2012 5:48 pm

Re: 623 - 500!

Post by helli3papa » Tue Sep 17, 2013 9:58 pm

TY brainfry
But why my code is wrong?
and I got TLE?

techbd123
New poster
Posts: 14
Joined: Tue Aug 06, 2013 3:42 pm

Re: 623 - 500!

Post by techbd123 » Fri Oct 04, 2013 11:19 am

Guru! Brianfry!
I didn't find any wrong in my code. After all the code hasn't been accepted.
Please help me find the bug.

My code is here:
[Removed since AC]
Date: 04/OCT/2013 Time: 2:50 pm [GMT]
I found out my simple mistake.

X123
New poster
Posts: 11
Joined: Fri Oct 04, 2013 1:28 am

623

Post by X123 » Thu Oct 24, 2013 10:26 am

getting TLE..i tried to solve it using C

my code-

#include<stdio.h>
int main()
{
int ara1[2600],ara2[2600],ara3[2600],sum[2600],uff[1001];
long int i,a,d1,l1=0,d2,l2=0,j,m,t,k,p=0,l4=0,q=0,x,A,B=0,C,T,s,max=0,f=0,y,u=0;
char fin[1001][2600];
while(scanf("%ld",&d1)==1)
{
uff=d1;
u++;
if(d1>1){
d2=d1-1;
for(i=0;i>=0;i++)
{
a=d1%10;
d1=d1/10;
ara1=a;
l1++;
if(d1==0)
break;
}
for(T=d2,s=0;T>=1;T--,s++)
{
d2=T;
for(i=0;i>=0;i++)
{
a=d2%10;
d2=d2/10;
ara2=a;
l2++;
if(d2==0)
break;
}
for(j=0,m=0;j<l2;j++,m++)
{
for(i=0;i<l1;i++)
{
t=ara1*ara2[j]+p;
k=t%10;
p=t/10;
ara3[q]=k;
q++;
}
if(p!=0)
{

ara3[q]=p;
q++;
}
l4=0;
for(i=m,x=0;x<q;i++,x++)
{
A=sum+ara3[x]+B;
C=A%10;
B=A/10;
sum=C;
l4++;
}
if(B!=0)
{
sum=B;
l4++;
}
if(l4>max)
max=l4;
for(i=0;i<q;i++)
ara3=0;
q=0;
p=0;
B=0;
}
m=0;
max=max+l2-1;
for(i=0;i<l1;i++)
ara1=0;
for(i=0;i<max;i++)
{
ara1=sum;
}
for(i=0;i<max;i++)
sum[i]=0;
for(i=0;i<l2;i++)
ara2[i]=0;
l1=max;
l2=0;
}
for(i=max-1,y=0;i>=0;i--,y++)
fin[f][y]=ara1[i]+48;
f++;
max=0;
B=0;
q=0;
l4=0;
p=0;
l2=0;
l1=0;
}
}
for(i=0;i<u;i++){
printf("%d!\n",uff[i]);
if(uff[i]==1||uff[i]==0)
{
printf("1\n");
}
else
printf("%s\n",fin[i]);
}
return 0;
}

i am a beginner.. help plz :(
Many of life’s failures are people who did not realize how close they were to success when they gave up.

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 623

Post by brianfry713 » Thu Oct 24, 2013 8:53 pm

Precompute all the possible outputs.
Check input and AC output for thousands of problems on uDebug!

Post Reply

Return to “Volume 6 (600-699)”