hdu 5167 Fibonacci【思维】【递归】

本文介绍了一个关于斐波那契数列的问题:如何判断一个整数是否能被分解为斐波那契数列中数的乘积。通过递归定义斐波那契数列,并提供了一种解决方案来解决这个问题,包括初始化斐波那契数列、使用深度优先搜索进行判断的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Fibonacci

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2145    Accepted Submission(s): 549


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


数据卡的很死,自己怎么做也没A掉。【后来找到了AC代码测数据,确实有bug存在】

没办法,只能另挑路径了。

代码很好理解,这里直接上代码:

#include<stdio.h>
#include<string.h>
using namespace std;
typedef long long ll;
ll f[50];
void init() {
    f[0] = 0;
    f[1] = 1;
    for(int i = 2; i <= 45; i++) {
        f[i] = f[i-1] + f[i-2];
    }
}
bool  dfs(ll n,int x)
{
    if(n<=3)return true;
    for(int i=3;i<=x;i++)
    {
        if(n%f[i]==0)
        {
            if(dfs(n/f[i],i))
            return true;
        }
    }
    return false;
}
int main()
{
    init();
    ll n;
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%lld",&n);
        if(dfs(n,45))
        printf("Yes\n");
        else
        {
            printf("No\n");
        }
    }
}













评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值