Nyoj ___ Big Data factorial

Source: Internet
Author: User

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.