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