HDU 5167 Fibonacci (DFS + Fib数列)

78 篇文章 0 订阅


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:
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
BestCoder Round #28

题目链接: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");
    }
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值