Http://acm.nyist.net/JudgeOnline/problem.php? Pid = 28
Big Data factorial time limit: 3000 MS | memory limit: 65535 KB difficulty: 3
-
Description
-
We all know how to calculate a factorial of a number. But if this number is large, how should we calculate it and output it?
-
Input
-
Enter an integer m (0 <m <= 5000)
-
Output
-
Output the factorial of m, and enter a linefeed after the output ends.
-
Sample Input
-
50
-
Sample output
-
30414093201713378043612608166064768844377641568960512000000000000
-
Program code:
-
1 #include<stdio.h> 2 #include<math.h> 3 int main(){ 4 int n; 5 while(scanf("%d",&n)!=EOF){ 6 long a[10000]; 7 int i,j,l,c,m=0,w; 8 a[0]=1; 9 for(i=1;i<=n;i++){10 c=0;11 for(j=0;j<=m;j++){12 a[j]=a[j]*i+c;13 c=a[j]/10000;14 a[j]=a[j]%10000;15 }16 if(c>0) {m++;a[m]=c;}17 }18 19 w=m*4+log10(a[m])+1;20 printf("%ld",a[m]);21 for(i=m-1;i>=0;i--) printf("%4.4ld",a[i]);22 printf("\n");23 }24 return 0;25 }
The calculation method of the number of factorial digits is (int) log10 (x) + 1.
This is the number of digits of x.
Log10 (n !) = Log10 (1) + log10 (2) + log10 (3) +... + log10 (n)