hdoj-5167-Fibonacci

来源:互联网 发布:温什么知什么的成语 编辑:程序博客网 时间:2024/05/21 06:41

Problem Description
Following is the recursive definition of Fibonacci sequence:
Fi=01Fi1+Fi2i = 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. (T100,000)
For each test case , the first line contains a integers n , which means the number need to be checked.
0n1,000,000,000
 

Output
For each case output "Yes" or "No".
 

Sample Input
3417233
 

Sample Output
YesNoYes


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


#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>using namespace std;typedef long long ll;#define N 100000ll f[N],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;}


0 0
原创粉丝点击