您的位置:首页 > 其它

hdoj-5167-Fibonacci

2016-07-30 23:27 357 查看
[align=left]Problem Description[/align]
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.
 

[align=left]Input[/align]
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
 

[align=left]Output[/align]
For each case output "Yes" or "No".
 

[align=left]Sample Input[/align]

3
4
17
233

 

[align=left]Sample Output[/align]

Yes
No
Yes

一定会要先打表把所有的斐波那契数求出来,然后直接dfs就ok了,然后又点剪枝

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;

typedef long long ll;
#define N 100000
ll f
,n;
int cnt;
bool flag;

void init()
{
f[0]=0;
f[1]=1;
cnt=2;
for(; f[cnt-1]<=1000000000; cnt++)
f[cnt]=f[cnt-1]+f[cnt-2];
}
void dfs(ll k,int num)
{
if(k==1){
flag=true;
return;
}
for(int i=num;i>=3;i--)
{
if(f[i]>k) continue;
if(k%f[i]==0)
dfs(k/f[i],i);
if(flag) return;
}
}
int main()
{
init();
int t;
scanf("%d",&t);
while(t--)
{
scanf("%lld",&n);
if(!n||n==1) printf("Yes\n");
else
{
flag=false;
dfs(n,cnt-1);
if(flag) printf("Yes\n");
else printf("No\n");
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: