Page 11 of 13

Re: 623 - 500!

Posted: Tue Jan 05, 2010 3:40 pm
by seraph
Pliss help me...
i always runtime error with my code, this is my code :

Code: Select all

#include <iostream>
#include <math.h>
using namespace std;
int main()
{
    double x[1010];
	  x[1] = x[0] = 0;
	  for(int i=2;i<=1009;i++)
       x[i] = x[i-1] + log10(i);
    
    int n;
    int *fact[1010];
    n=1009;
    {
        int hasil1[2600]={0};
        int hasil2[2600]={0};
        hasil1[0]=1;
        for (int i=1;i<=n;i++)
        {
            for (int j=0;j<=(int)x[i];j++)
            {
                hasil2[j]+=i*hasil1[j];
                if (hasil2[j]>9)
                {
                    hasil2[j+1]=hasil2[j]/10;
                    hasil2[j]=hasil2[j]%10;
                }
                hasil1[j]=0;
            }
            
            fact[i]=new int [(int)x[i]+10];
            int y=0;
            for (int j=(int)x[i];j>=0;j--,y++)
                fact[i][y]=hasil2[j];
            i++;
            
            for (int j=0;j<=(int)x[i];j++)
              {
                  hasil1[j]+=i*hasil2[j];
                  if (hasil1[j]>9)
                  {
                     hasil1[j+1]=hasil1[j]/10;
                     hasil1[j]=hasil1[j]%10;
                  }
                  hasil2[j]=0;
              }
            fact[i]=new int [(int)x[i]+10];
            y=0;
            for (int j=(int)x[i];j>=0;j--,y++)
                fact[i][y]=hasil1[j];
        }
    }
    
    while (cin>>n)
    {
        cout<<n<<"!"<<endl;
        int y=0;
        for (int j=(int)x[n];j>=0;j--,y++)
            cout<<fact[n][y];
        cout<<endl;
    }
    return 0;
}

623 why 500! WA?

Posted: Wed Aug 04, 2010 5:42 pm
by @mjad
hi i have tried to solve 500!
but i got WA
please help me
here is my code

Code: Select all



Re: 623 - 500!

Posted: Wed Aug 04, 2010 7:02 pm
by sohel
What is your output for n=1000

Re: 623 - 500!

Posted: Wed Aug 04, 2010 7:42 pm
by @mjad
thanks for your reply.
at first my program generates only up to 500,
so there are no output for 1000,
when i got WA,
than i omit it,
please help me a specific way.

Re: 623 - 500!

Posted: Thu Aug 05, 2010 12:11 pm
by sohel
There could be input as big as 1000.
Can you post your new code?

623-500!

Posted: Sat Aug 07, 2010 4:06 pm
by CodeChomper
oh, my god!
why i got WA?
you know, i have checked all answers from 1 to 1000, and there is no WA.
but when i submit, i got WA.
need you help! thanks.

Code: Select all

#include <stdio.h>
#include <string.h>

#define maxn 3000 + 10

int fact[1001][maxn];

int main()
{
    int i, j, n;

    memset(fact, 0, sizeof(fact));

    fact[1][0] = 1;
    for (i = 2; i <= 1000; i++)
    {
        int flag = 0;
        for (j = 0; j < maxn; j++)
        {
            int tmp = fact[i-1][j] * i + flag;
            fact[i][j] = tmp % 10;
            flag = tmp / 10;
        }
    }
    
    while (scanf("%d", &n) == 1)
    {
        printf("%d!\n", n);

        for (j = maxn-1; j >= 0; j--)
           if (fact[n][j]) break;

        for (i = j; i >= 0; i--)
           printf("%d", fact[n][i]);

        printf("\n");
    }
    return 0;
}

Re: 623 - 500!

Posted: Sun Aug 08, 2010 12:41 pm
by @mjad
thanks a lot
finally i got AC
thanks for help me

Re: 623 - 500!

Posted: Sat Jan 22, 2011 2:24 am
by bulletshot60
Hello, I am having a bit of trouble with this one.

For some reason, unbeknown to me, the following code is generating a RE on each submission. I thought that perhaps someone might be able to shed a small amount of light as to why this is happening. Thank you in advance.

Code: Select all

import java.io.*;
import java.math.*;
import java.util.*;

class main
{
    public static void main(String[] argv) throws IOException
    {
    }
}
Nvm, on java programs your class main must be spelt with a capital M.

Re: 623 - 500!

Posted: Sun Jan 22, 2012 1:04 pm
by Scarecrow
for some strange reasons, my code is getting RE. i used the same code for 324 - Factorial Frequencies and got AC.for this prob, increased the limit of the strings as needed and i'm getting RE! can sm1 help me :(

Code: Select all

removed after AC

Re: 623 - 500!

Posted: Thu May 24, 2012 8:01 am
by kia.masster
In this problem we must precalculate all possible factorials, and print them out when requested. Calculate factorials of 1 to 1000 takes so mush time. What is the good way to find them?

Code: Select all

bignum f[1001];

int main(){
	int n;
	f[0] = bignum("1");
	f[1] = bignum("1");
	for (int i = 2; i <= 1000; i++)
		f[i] = f[i - 1] * bignum(i);
	while (cin >> n)
		cout << n << "!" << endl << f[n] << endl;
}
Thank you!

Re: 623 - 500!

Posted: Thu May 24, 2012 9:57 am
by brianfry713
Yes precalculate them. It shouldn't take too long.

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

Posted: Tue Jun 12, 2012 1:28 am
by Enayet Kabir
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#define MAX 10000

void Factorial(int);
void multiply(int);
int nLength = 0;
int fact[MAX];

int main(){
int n;
int i;
while(scanf("%d",&n)==1)
{
memset(fact,0,sizeof(fact));
fact[0]=1;
Factorial(n);
printf("%d!\n",n);
for(i = nLength; i >= 0; i--){
printf("%d",fact);
}
printf("\n");


}
return 0;
}

void Factorial(int n) {
int i;
for(i=2;i<=n;i++){
multiply(i);
}
}

void multiply(int n) {
long i,carry = 0;
int arr[MAX];
for(i = 0;i <= nLength; i++) {
arr=fact;
}

for(i = 0; i <= nLength; i++) {
fact = (arr*n + carry)%10;
carry = (arr*n + carry)/10;
}
if(carry!=0){
while(carry != 0){
fact=carry%10;
carry = carry/10;
i++;
}
}
nLength = i-1;
}

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

Posted: Tue Jun 12, 2012 11:07 pm
by brianfry713
precalculate

Re: 623 - 500!

Posted: Sun Nov 04, 2012 8:47 am
by uvasarker
I am new in (Uva+Java). Please, help me. I am getting Rumtime error. why?

Code: Select all

/* Removed */

Re: 623 - 500!

Posted: Tue Nov 06, 2012 2:10 am
by brianfry713