【bzoj2796】 [Poi2012]Fibonacci Representation

Description

Fib数列0,1,1,2,3,5,8,13,21。

给出一个数字,用FIB数列各项加加减减来得到。例如

10=5+5

19=21-2

17=13+5-1

1070=987+89-5-1

Input

In the first line of the standard input a single positive integer is given (1 <=P<=10) that denotes the number of queries. The following lines hold a single positive integer K each 1<=K<=10^17.
Output

For each query your program should print on the standard output the minimum number of Fibonacci numbers needed to represent the number k as their sum or difference.
Sample Input

1
Sample Output
1070

题解
因为F[k]*2=F[k+1]+F[k-2],即存在最优解满足同一个FIB数出现次数不超过1
令l表示小等于n且最大的斐波那契数,r为其后一项,可以暴力或者二分求
dp(n) = min{ dp(n-l) , dp(r-n) } + 1
使用记忆化搜索

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<set>
#include<ctime>
#include<vector>
#include<cmath>
#include<algorithm>
#include<map>
#define mod 1000000007 
#define ll long long  
#define inf 2e18
using namespace std;
inline int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
map<ll,int>dp;
ll n,f[105];
int num;
int dfs(ll n)
{
    if (dp[n]) return dp[n];
    int l=1,r=num;
    while (l!=r)
    {
        int mid=(l+r+1)>>1;
        if (f[mid]<=n) l=mid;else r=mid-1;
    }
    if (f[l]==n) return 1;
    int ans=dfs(n-f[l]);
    if (f[l+1]) ans=min(ans,dfs(f[l+1]-n));
    dp[n]=ans+1;
    return dp[n];
}
int main()
{
    int Case=read();
    f[1]=1;f[0]=1;num=1;
    for (int i=2;f[i-1]<=inf;i++,num++)
        f[i]=f[i-1]+f[i-2];
    while (Case--)
    {
        scanf("%lld",&n);
        printf("%d\n",dfs(n));
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值