Fibonacci
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 780 Accepted Submission(s): 209
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
Source
BestCoder Round #28
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5167
题目大意:给个数字判断它能不能由1000000000内的Fibonacci数列的一些项相乘得到
题目分析:打表到范围内的Fib数列为第46项,然后找n的因子,DFS搜一下就可以了,特判一下0
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5167
题目大意:给个数字判断它能不能由1000000000内的Fibonacci数列的一些项相乘得到
题目分析:打表到范围内的Fib数列为第46项,然后找n的因子,DFS搜一下就可以了,特判一下0
#include <cstdio>
int fib[50], fac[50], cnt;
bool flag;
void cal() //获取Fib数列
{
fib[0] = 0;
fib[1] = 1;
for(int i = 2; i < 47; i++)
fib[i] = fib[i - 1] + fib[i - 2];
}
bool DFS(int n, int now)
{
if(n == 1 || flag) //最后商为1则说明可以由某些项的乘积得到
flag = true;
for(int i = now; i < cnt; i++)
if(n % fac[i] == 0 && DFS(n / fac[i], i))
return true;
return false;
}
int main()
{
cal();
int T, n;
scanf("%d", &T);
while(T--)
{
flag = false;
cnt = 0;
scanf("%d", &n);
if(n == 0)
{
printf("Yes\n");
continue;
}
for(int i = 3; i < 47; i++)
if(n % fib[i] == 0)
fac[cnt++] = fib[i]; //找n的fib因子
DFS(n, 0);
if(flag)
printf("Yes\n");
else
printf("No\n");
}
}