LUOGU P3539 [POI2012]ROZ-Fibonacci Representation

本文介绍了一个基于斐波那契数列的问题解决方法。通过预先计算斐波那契数列,并使用递归搜索策略来寻找目标数值的最佳表示方式。此方法能够有效地找出最小的操作次数。

传送门

解题思路

打了个表发现每次x只会被比x大的第一个fab或比x小的第一个fab表示,就直接写了个爆搜骗分,结果过了。。

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>

using namespace std;
typedef long long LL;

LL fab[105];
LL k;
int T,ans;

inline void dfs(LL x,int cnt){
    if(cnt>=ans) return;
    for(register int i=0;i<=91;i++) {
        if(fab[i]==x) {ans=min(ans,cnt+1);return;}
        if(fab[i]<x && fab[i+1]>x) {
            dfs(x-fab[i],cnt+1);
            dfs(fab[i+1]-x,cnt+1);  
            break;
        }
    }
}

int main(){
    fab[1]=1;fab[2]=1;fab[0]=0;
    for(register int i=3;i<=91;i++) fab[i]=fab[i-1]+fab[i-2];       
    scanf("%d",&T);
    while(T--){
        ans=0x3f3f3f3f;
        scanf("%lld",&k);
        dfs(k,0);
        cout<<ans<<endl;
    }
    return 0;
}

转载于:https://www.cnblogs.com/sdfzsyq/p/9676862.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值