Fibonacci
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2391 Accepted Submission(s): 609
Problem Description
Following is the recursive definition of Fibonacci sequence:
Now we need to check whether a number can be expressed as the product of numbers in the 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
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
思路:直接爆搜会TLE,所以这里有个剪枝技巧,我们从大小大枚举,这一次搜索选择了i,那么我们下次只需要枚举3~i即可,相等于从序列最大的数开始向下枚举。
注意0,1,2不能算进去,0,和1要特判,不然会死循环。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
using namespace std;
#define N 100000
long long f[N];
int flag,cnt;
long long n;
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(long long k,int num)
{
if(k==1){
flag=1;
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=0;
dfs(n,cnt-1);
if(flag) printf("Yes\n");
else printf("No\n");
}
}
return 0;
}