The sum of P1009 factorial
- Topic provider Rokua Onlinejudge
- Tag number theory (mathematics-related) high-precision 1998Noip Increase group Noip popularization Group
- Difficulty Popularization-
- Pass/Submit 1139/3791
Submit a discussion of the problem record
Title Description
Calculate the s=1! with high precision +2! +3! +...+n! (N≤50)
Which "! "denotes factorial, for example: 5! =5*4*3*2*1.
Input output Format input format:
A positive integer n.
Output format:
A positive integer s that represents the result of the calculation.
Input and Output Sample input example # #:
3
Sample # # of output:
9
#include <iostream>#include<cstring>using namespacestd;inta[100000],n,i,j,x[100000],y[100000];voidAdd ()//Handling Rounding{memset (x,0,sizeof(x)); x[0]=max (y[0],a[0]); for(i=1; i<=x[0];i++) {x[i]+=y[i]+a[i],x[i+1]=x[i]/Ten, x[i]%=Ten;} while(x[x[0]+1]>0) {x[x[0]+2]=x[x[0]+1]/Ten, x[x[0]+1]%=Ten, x[0]++;} y[0]=x[0]; for(i=1; i<=x[0];i++) {y[i]=x[i];}}intMain () {CIN>>N; a[0]=1, a[1]=1, y[0]=1, y[1]=0; for(j=1; j<=n;j++) {memset (x,0,sizeof(x));//Clear Order Multiplier Groupx[0]=a[0]; for(i=1; i<=a[0];i++) {x[i]+=a[i]*j,x[i+1]=x[i]/Ten, x[i]%=Ten;}//analog factorial while(x[x[0]+1]>0) {x[x[0]+2]=x[x[0]+1]/Ten, x[x[0]+1]%=Ten, x[0]++;}//plus factorial for(i=1; i<=x[0];i++) {a[i]=x[i];} a[0]=x[0]; Add (); } for(i=y[0];i>=1; i--) {cout<<y[i];}//Reverse storage will be reversed output return 0;}
The sum of P1009 factorial