传送门
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.
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
#include<iostream>
#include<stdio.h>
#include<string>
#include<vector>
#include<queue>
#include<map>
#include<algorithm>
using namespace std;
long long fib[45];//fib[45]=1134903170故只需前面45个斐波那契数即可
map<long long, bool>test;//用来记录关键值是否可表示为斐波那契数的乘积
queue<long long>q;
int main()
{
fib[0] = 0;
fib[1] = 1;
test[1] = true;
test[0] = true;
for (int i = 2; i < 45; i++)
{
fib[i] = fib[i - 1] + fib[i - 2];
test[fib[i]] = true;
q.push(fib[i]);
}
while (!q.empty())
{
long long product, head;
head = q.front();
q.pop();
for (int i = 2; i < 45; i++)
{
product = head * fib[i];
if (product > 1e9)
break;
if (test[product])
continue;
test[product] = true;
q.push(product);//注意,因为结果可能是多个斐波那契数相乘,所以每两个数的乘积也要加入队列
}
}
int T;
scanf("%d", &T);
while (T--)
{
long long n;
scanf("%I64d", &n);
if (test[n])
cout << "Yes" << endl;
else
cout << "No" << endl;
}
return 0;
}