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

seraph
New poster
Posts: 9
Joined: Tue Dec 15, 2009 4:18 pm

Re: 623 - 500!

Post 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;
}
@mjad
New poster
Posts: 44
Joined: Thu Jul 22, 2010 9:42 am

623 why 500! WA?

Post by @mjad »

hi i have tried to solve 500!
but i got WA
please help me
here is my code

Code: Select all


Last edited by @mjad on Sun Aug 08, 2010 12:42 pm, edited 1 time in total.
sohel
Guru
Posts: 856
Joined: Thu Jan 30, 2003 5:50 am
Location: New York

Re: 623 - 500!

Post by sohel »

What is your output for n=1000
@mjad
New poster
Posts: 44
Joined: Thu Jul 22, 2010 9:42 am

Re: 623 - 500!

Post 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.
sohel
Guru
Posts: 856
Joined: Thu Jan 30, 2003 5:50 am
Location: New York

Re: 623 - 500!

Post by sohel »

There could be input as big as 1000.
Can you post your new code?
CodeChomper
New poster
Posts: 1
Joined: Fri Aug 06, 2010 9:37 am

623-500!

Post 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;
}
@mjad
New poster
Posts: 44
Joined: Thu Jul 22, 2010 9:42 am

Re: 623 - 500!

Post by @mjad »

thanks a lot
finally i got AC
thanks for help me
bulletshot60
New poster
Posts: 1
Joined: Sat Jan 22, 2011 2:18 am

Re: 623 - 500!

Post 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.
Scarecrow
Learning poster
Posts: 69
Joined: Wed Oct 19, 2011 9:06 pm

Re: 623 - 500!

Post 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
Do or do not. There is no try.
kia.masster
New poster
Posts: 6
Joined: Thu Dec 22, 2011 8:47 am

Re: 623 - 500!

Post 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!
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 623 - 500!

Post by brianfry713 »

Yes precalculate them. It shouldn't take too long.
Check input and AC output for thousands of problems on uDebug!
Enayet Kabir
New poster
Posts: 7
Joined: Mon Jun 04, 2012 3:03 pm

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

Post 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;
}
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 »

precalculate
Check input and AC output for thousands of problems on uDebug!
uvasarker
Learning poster
Posts: 96
Joined: Tue Jul 19, 2011 12:19 pm
Location: Dhaka, Bangladesh
Contact:

Re: 623 - 500!

Post by uvasarker »

I am new in (Uva+Java). Please, help me. I am getting Rumtime error. why?

Code: Select all

/* Removed */
Last edited by uvasarker on Tue Nov 06, 2012 9:08 pm, edited 1 time in total.
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 623 - 500!

Post by brianfry713 »

Check input and AC output for thousands of problems on uDebug!
Post Reply

Return to “Volume 6 (600-699)”