您的位置:首页 > 其它

HDU 5167 Fibonacci (DFS + Fib数列)

2015-02-01 21:47 295 查看

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=⎧⎩⎨⎪⎪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

题目链接: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");
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: