hdu 5167 Fibonacci(预处理)

Problem Description

Following is the recursive definition of 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

Output

For each case output "Yes" or "No".

Sample Input

3 4 17 233

Sample Output

Yes No Yes

Source

BestCoder Round #28

题意:判断一个数能否由任意个菲波那媞数相乘得到,能的话输出“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 }

时间: 2024-10-19 02:21:57

hdu 5167 Fibonacci(预处理)的相关文章

hdu 5167 Fibonacci(DFS)

hdu 5167 Fibonacci 问题描述 斐波那契数列的递归定义如下: Fi=???01Fi?1+Fi?2i = 0i = 1i > 1 现在我们需要判断一个数是否能表示为斐波那契数列中的数的乘积. 输入描述 有多组数据,第一行为数据组数T(T≤100,000). 对于每组数据有一个整数n,表示要判断的数字. 0≤n≤1,000,000,000 输出描述 对于每组数据,如果可以输出"Yes",否则输出"No". 输入样例 3 4 17 233 输出样例

hdu 5167 Fibonacci

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5167 题意: fi[0]=0,fi[1]=1 fi[i]=fi[i-1]+fi[i-2] i>1 给出一个数n,问这个数能不能有fi[]相乘得来. 限制: 0 <= n <= 1e9 思路: 1e9以内的斐波那契数只有44个,用记忆化搜索可以解决这道题. C++ Code 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

HDU 5167 Fibonacci(BestCoder Round #28)

Problem Description: Following is the recursive definition of 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 

HDU 4786 Fibonacci Tree 最小生成树变形

思路: 这题比赛的时候想了好久,最后队友机智的想到了. 不过那时不是我敲的,现在敲的1A. 想好就容易了. 直接把1或者0当做边的权值,然后按边从小到大排序,然后算最小生成用到了几条白边,然后再按边从大到小排序,然后再算白边用了几条.然后最小和最大需要用到的白边都算出来了.如果在这最小最大区间中存在那个啥数列的话就是Yes,否则就是No. 为什么在这区间里面就是对的呢?刚开始我也想了好久,然后发现,因为白边权值是1,然后黑边是0,然后假设用到白边最小的是6,最大的是10,那么,我们可以用黑边去替

HDU - 1848 - Fibonacci again and again

先上题目: Fibonacci again and again Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4964    Accepted Submission(s): 2072 Problem Description 任何一个大学生对菲波那契数列(Fibonacci numbers)应该都不会陌生,它是这样定义的:F(1)=1;F

hdu 3117 Fibonacci Numbers

点击此处即可传送到hdu 3117 **Fibonacci Numbers** Problem Description The Fibonacci sequence is the sequence of numbers such that every element is equal to the sum of the two previous elements, except for the first two elements f0 and f1 which are respectively

hdu 1568 Fibonacci

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1568 数学....囧...害我低沉了好几天提不起劲做题. 用到了斐波那契数列的通项公式. 先看对数的性质,loga(b^c)=c*loga(b),loga(b*c)=loga(b)+loga(c); 假设给出一个数10234432,那么log10(10234432)=log10(1.0234432*10^7)=log10(1.0234432)+7;log10(1.0234432)就是log10(10

HDU 3117 Fibonacci Numbers(斐波那契前后四位,打表+取对+矩阵快速幂)

HDU 3117 Fibonacci Numbers(斐波那契前后四位,打表+取对+矩阵快速幂) ACM 题目地址:HDU 3117 Fibonacci Numbers 题意: 求第n个斐波那契数的前四位和后四位. 不足8位直接输出. 分析: 前四位有另外一题HDU 1568,用取对的方法来做的. 后四位可以用矩阵快速幂,MOD设成10000就行了. 代码: /* * Author: illuz <iilluzen[at]gmail.com> * Blog: http://blog.csdn.

HDU - 5166 - Missing number &amp;&amp; 5167 - Fibonacci

Missing number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 430    Accepted Submission(s): 233 Problem Description There is a permutation without two numbers in it, and now you know what num