Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2400 Accepted Submission(s): 610
Now we need to check whether a number can be expressed as the product of numbers in the Fibonacci sequence.
For each test case , the first line contains a integers n , which means the number need to be checked.
0≤n≤1,000,000,000
#include <iostream> #include <cstdio> #include <cstring> #include <queue> #include <algorithm> using namespace std; typedef long long LL; LL f[45]; bool flag; void init(){ f[0] = 0; f[1] = 1; for(int i=2;i<=45;i++){ f[i] = f[i-1]+f[i-2]; } } void dfs(LL ans,int step){ if(ans==1){ flag = true; return; } for(int i=3;i<=step;i++){ if(ans<f[i]) break; if(ans%f[i]==0){ if(flag) return; dfs(ans/f[i],i); } } return; } int main() { init(); int tcase; scanf("%d",&tcase); while(tcase--) { LL n; scanf("%lld",&n); if(n==0){ printf("Yes\n"); continue; } flag = false; dfs(n,45); if(flag) printf("Yes\n"); else printf("No\n"); } return 0; }
-------------------------------------------------------------------------------------------------------------------------------------------------------
然后我把循环顺序改了,171msAC...
#include <iostream> #include <cstdio> #include <cstring> #include <queue> #include <algorithm> using namespace std; typedef long long LL; LL f[45]; bool flag; void init(){ f[0] = 0; f[1] = 1; for(int i=2;i<=45;i++){ f[i] = f[i-1]+f[i-2]; } } void dfs(LL ans,int step){ if(ans==1){ flag = true; return; } for(int i=step;i>=3;i--){ if(ans%f[i]==0){ if(flag) return; dfs(ans/f[i],i); } } return; } int main() { init(); int tcase; scanf("%d",&tcase); while(tcase--) { LL n; scanf("%lld",&n); if(n==0){ printf("Yes\n"); continue; } flag = false; dfs(n,45); if(flag) printf("Yes\n"); else printf("No\n"); } return 0; }