15: factorial and 15: factorial
15: factorial and
- View
- Submit
- Statistics
- Question
-
Total time limit:
-
1000 ms
-
Memory limit:
-
65536kB
-
Description
-
Calculate S = 1 with high precision! + 2! + 3! +... + N! (N ≤ 50)
"!" Indicates a factorial, for example, 5! = 5*4*3*2*1.
Input positive integer N and output computation result S.
-
Input
-
A positive integer N.
-
Output
-
Calculation Result S.
-
Sample Input
-
5
-
Sample output
-
153
-
Source
-
Question 2 of NOIP1998 semi-finals popularity Group
-
1 # include <iostream> 2 # include <cstdio> 3 # include <cmath> 4 # include <cstring> 5 using namespace std; 6 int a [10001] = {1 }; 7 int ans [100001] = {0}; 8 char n [1001]; 9 int en [1001]; 10 int lans = 1; 11 int la = 1; 12 int x; 13 int main () 14 {15 int n; 16 cin> n; 17 int x = 0; 18 for (int I = 1; I <= n; I ++) // find every number 19 {20 memset (a, 0, sizeof (a); 21 a [0] = 1; 22 for (int k = 1; k <= I; k ++) // returns a factorial of 23 {24 for (int j = 0; j <la; j ++) 25 {26 a [j] = a [j] * k + x; 27 x = a [j]/10; 28 if (x> 0 & j = la-1) 29 la ++; 30 a [j] = a [j] % 10; 31} 32 33} 34 int l = 0; 35 x = 0; 36 while (l <= la | l <= lans) 37 {38 ans [l] = ans [l] + a [l] + x; 39 x = ans [l]/10; 40 if (x> 0) 41 lans ++; 42 ans [l] = ans [l] % 10; 43 l ++; 44} 45} 46 int flag = 0; 47 for (int I = lans; I> = 0; I --) 48 {49 if (ans [I] = 0 & flag = 0) 50 continue; 51 else flag = 1; 52 cout <ans [I]; 53} 54 return 0; 55}