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:
1. first obtain the factorial nearest n.
2. After finding the factorial nearest n, n = n-A [I]. Then, repeat the factorial nearest N (key step)
3. If n = 0, N can be decomposed into the sum of factorial. Otherwise, N cannot.
Optimal code:
# Include <iostream> <br/> using namespace STD; <br/> int main () <br/>{< br/> int N; <br/> int K [9] = {120,720,504, 362880,}; <br/> CIN> N; <br/> while (n --) <br/>{< br/> bool flag = false; <br/> int m; <br/> CIN> m; <br/> for (INT I = 8; I> = 0; -- I) <br/>{< br/> If (M> = K [I] & M> 0) <br/> M-= K [I]; <br/> If (M = 0) <br/> flag = true; <br/>}< br/> If (FLAG) <br/> cout <"yes" <Endl; <br/> else <br/> cout <"no" <Endl; <br/>}< br/>}