hdu 5167 Fibonacci(预处理)

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
3 4 17 233
 

 

Sample Output
Yes No Yes
 

 

Source
 
 
 
题意:判断一个数能否由任意个菲波那媞数相乘得到,能的话输出“Yes”,否则输出“No”
思路:首先要处理菲波那媞数组,然后用一个队列来实现菲波那媞数的相乘,用一个map数组来标记。具体看代码

 

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<stdlib.h>
 6 #include<algorithm>
 7 #include<queue>
 8 #include<map>
 9 using namespace std;
10 #define N 46
11 #define ll long long
12 ll f[N];
13 map<ll,bool>mp;
14 void init()
15 {
16     mp.clear();
17     queue<ll>q;
18     f[0]=0;
19     f[1]=1;
20     mp[0]=true;
21     mp[1]=true;
22     q.push(0);
23     q.push(1);
24     for(int i=2;i<N;i++)
25     {
26         f[i]=f[i-1]+f[i-2];
27         mp[f[i]]=true;
28         q.push(f[i]);
29     }
30     while(!q.empty())
31     {
32         ll tmp=q.front();
33         q.pop();
34         for(int i=0;i<N;i++)
35         {
36             ll cnt=tmp*f[i];
37             if(cnt>1000000000L)
38               break;
39             if(mp[cnt]) continue;
40             mp[cnt]=true;
41             q.push(cnt);
42         }
43     }
44     
45 }
46 int main()
47 {
48     init();
49     int t;
50     scanf("%d",&t);
51     while(t--)
52     {
53         ll n;
54         scanf("%I64d",&n);
55         if(mp[n]==true)
56           printf("Yes\n");
57         else 
58           printf("No\n");
59         
60     }
61     return 0;
62 }
View Code

 

转载于:https://www.cnblogs.com/UniqueColor/p/4743704.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值