您的位置:首页 > 其它

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  hdoj5167Fibonaccimap