Http://acm.nyist.net/JudgeOnline/problem.php? PID = 91
Factorial sum time limit: 3000 MS | memory limit: 65535 kb difficulty: 3
-
-
Description
-
-
We will give you a non-negative integer n to determine whether N is the sum of certain numbers (these numbers cannot be reused, and are positive numbers), such as 9 = 1! + 2! + 3 !, If yes, yes is output; otherwise, no is output;
-
-
Input
-
-
The first row has an integer of 0 <m <100, indicating that there are M groups of test data;
Each group of test data has a positive integer n <1000000;
-
-
Output
-
-
If the conditions are met, yes is output; otherwise, no is output;
-
-
Sample Input
-
-
2 9 10
-
-
Sample output
-
-
Yes No
Solution: preprocessing is less than a factorial of. Then, each input has a number of N, which reduces the factorial from large to small, and finally determines whether it is equal to 0.
1 # include <stdio. h>
2 # include <math. h>
3
4 long a [100], T;
5
6 int main (){
7 int n, m, I, J;
8 A [0] = 1;
9 I = 0;
10 while (A [I] <1000000 ){
11 I ++;
12 A [I] = A [I-1] * I;
13}
14 scanf ("% d", & M );
15 while (M --){
16 scanf ("% d", & N );
17 For (j = I-1; j> 0; j --){
18 if (n> = A [J]) {
19 N-= A [J];
20}
21}
22 if (n = 0 ){
23 printf ("Yes \ n ");
24}
25 else {
26 printf ("NO \ n ");
27}
28}
29 return 0;
30}
Sum of NYOJ-91-factorial