HDU-5167:Fibonacci(C++详细代码实现)

传送门
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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

艺千秋录

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值