hdoj5167Fibonacci【map记录记忆化搜索】
2016-03-07 21:48
369 查看
Fibonacci
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2220 Accepted Submission(s): 564
Problem Description
Following is the recursive definition of Fibonacci sequence:
Fi=⎧⎩⎨01Fi−1+Fi−2i
= 0i = 1i
> 1
Now we need to check whether a number can be expressed as the product of numbers in the Fibonacci sequence.
Input
There is a number T shows
there are T test
cases below. (T≤100,000)
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
Output
For each case output "Yes" or "No".
Sample Input
3
4
17
233
Sample Output
Yes
No
Yes
Source
BestCoder Round #28
#include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<cmath> #include<queue> #include<list> #include<vector> #include<map> using namespace std; const int maxn=10010; int f[50]; map<int,int >m; void init(){ f[0]=0;f[1]=1;m[0]++;m[1]++; for(int i=2;i<=45;++i){ f[i]=f[i-1]+f[i-2]; m[f[i]]++; } } bool judge(int n){ if(m.count(n)){ if(m ==0)return false; return true; } for(int i=3;i<=45;++i){ if(f[i]>n)break; if(n%f[i]==0){ if(m.count(n/f[i])){ if(m[n/f[i]]!=0)return true; } else if(judge(n/f[i])){ m[n/f[i]]++; return true; } else m[n/f[i]]=0; } } m =0; return false; } int main() { init(); int n,i,j,k,t; scanf("%d",&t); while(t--){ scanf("%d",&n); if(m.count(n)){ if(m !=0) printf("Yes\n"); else printf("No\n"); } else if(judge(n)){ printf("Yes\n"); } else { printf("No\n"); m =0; } } return 0; }
相关文章推荐
- java学习---复用类与多态(一)
- java中Integer.parseInt(String)与Integer.valueOf(String)区别
- PHP数据加密技术之一---CRYPT()加密
- IE8下submit表单没反应
- Exynos4412 内核移植(四)—— MMU 相关知识解析
- EF IN 查询等效语句
- WWW SQL Designer
- 笔记
- 八、Linux下的网络服务器模型
- http://blog.csdn.net/liang12360640/article/details/50350648
- iOS数据库之FMDB
- swift 高级用法
- hihoCoder 1268 九宫
- 1. Memcached 介绍
- TOEFL(20151205)分析
- SPM第一次作业
- java7新特性 java8新特性
- Git自动补全
- Python爬取拉勾网招聘信息
- STM32学习第一例