sum of factorials
Time Limit:
3000
ms | Memory Limit:
65535
KB
Difficulty:
3
- describe
-
Give you a non-negative integer n, determine whether n is the sum of the factorials of some numbers (these numbers are not allowed to be reused and are positive numbers), such as 9=1! +2!+3!, if yes, output Yes, otherwise output No;
- enter
-
The first line has an integer 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 condition is met, output Yes, otherwise output No;
- sample input
-
2910
- Sample output
-
YesNo
#include<iostream> #include<cstdio> #include<vector> #include<algorithm> #include<cstring> using namespace std; int s1[15]; int jiecheng (int a) { int sum=1; while(a) { sum*=a; a--; } return sum; } int flag=0; int n; void dfs(int cur,int sum) { if(cur > 10 || sum > n) return ; if(sum==n) { flag=1; return ; } dfs(cur+1,sum+s1[cur]);//dfs dfs(cur+1,sum); } intmain() { int k,m; for(int i=1;i<=11;i++) { for(int t=1;t<=i;t++) { s1[i]=jiecheng(t); } } cin>>k; while(k--) { cin>>n; dfs(1,0); if(flag==1) cout<<"Yes"<<endl; else cout<<"No"<<endl; flag=0; } }