Large number factorial time limit:MS | Memory limit:65535 KB Difficulty:3
-
Describe
-
we all know how to calculate the factorial of a number, but if the number is large, how do we calculate it and output it?
-
-
Input
-
-
Enter an integer m (0<m<=5000)
-
-
Output
-
-
the factorial of the output m and enter a newline character after the output ends
-
-
Sample input
-
-
50
-
-
Sample output
-
30414093201713378043612608166064768844377641568960512000000000000
Simulation questions:
1#include <stdio.h>2#include <string.h>3 #defineN 10000104 intA[n];5 intImitation (intN)6 {7 inti;8 for(i=0; a[i]>=0; i++)9 {Tena[i]*=N; One } A for(i=0; a[i]>=0; i++) - { - if(a[i]>=Ten) the { - if(a[i+1]<0) -a[i+1]=0; -a[i+1]+=a[i]/Ten; +a[i]%=Ten; - } + A } at returni+1; - - } - intMain () - { - intI,m,flag; inmemset (a,-1Nsizeof(int)); -scanf"%d",&m); toa[0]=1; + for(i=2; i<=m;i++) - { theflag=imitation (i); * } $ for(i=flag;i>=0; i--)Panax Notoginseng { - if(a[i]>=0) theprintf"%d", A[i]); + } Aprintf"\ n"); the return 0; +}
1 2#include <iostream>3#include <iomanip>4 using namespacestd;5 //to store factorial within 200006 inta[15470];7 intMain ()8 {9 //freopen ("1.txt", "R", stdin);Ten //freopen ("2.txt", "w", stdout); One intN; ACin>>N; -a[1]=1; -a[0]=1; the intUp ; - for(intI=2; i<=n;++i) - { -up=0; + for(intj=1; j<=a[0];++J)//each phase multiply - { +A[J] *=i; AA[J] + =Up ; atup=a[j]/100000; -A[J]%=100000; - } - if(up!=0) - { -a[0]++; ina[a[0]]=Up ; - } to } + if(a[0]==1) cout<<a[1]; - Else the { *cout<<a[a[0]]; $ for(inti=a[0]-1;i>0; i--)Panax Notoginseng { -Cout<<setfill ('0') <<SETW (5) <<A[i]; the } + } A}
Nyoj 28 Large number factorial