给出一个正整数x,问x最少能由多少个Fibonacci数加减算出。
例如1070=987+89-5-1,因此x=1070时答案是4。
#include<set>
#include<map>
#include<cmath>
#include<stack>
#include<queue>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define mid ((l+r)>>1)
using namespace std;
long long f[600];
long long cnt=1;
int t;
long long x;
long long findL(long long x)
{
int l=1;
int r=cnt;
int ans=1;
while(l<=r)
{
if(f[mid]<=x)
{
ans=mid;
l=mid+1;
}
else
{
r=mid-1;
}
}
return f[ans];
}
long long findR(long long x)
{
int l=1;
int r=cnt;
int ans=1;
while(l<=r)
{
if(f[mid]>=x)
{
ans=mid;
r=mid-1;
}
else
{
l=mid+1;
}
}
return f[ans];
}
int find(long long x)
{
long long l=findL(x);
long long r=findR(x);
if(l==r)
{
return 1;
}
if(x-l<=r-x)
{
return find(x-l)+1;
}
else
{
return find(r-x)+1;
}
}
int main()
{
f[0]=f[1]=1;
while(f[cnt-1]<=4e17)
{
f[++cnt]=f[cnt-1]+f[cnt-2];
}
scanf("%d",&t);
while(t--)
{
scanf("%lld",&x);
printf("%d\n",find(x));
}
}
BZOJ2796[Poi2012]Fibonacci Representation——记忆化搜索
原文:https://www.cnblogs.com/Khada-Jhin/p/9593694.html