[BZOJ2796][Poi2012]Fibonacci Representation

时间:2021-12-12 08:06:59

由于是斐波那契数列,所以$x_i+x_j<=x_k,i<j<k$

所以猜测可以贪心选择两边近的数处理。

 #include<cstdio>
#include<algorithm>
#define ll long long
#define mid (l+r>>1)
using namespace std;
ll f[],tot=;
inline ll findl(ll x)
{
int l=,r=tot,ans=;
while(l<=r)
{
if(f[mid]<=x)ans=mid,l=mid+;
else r=mid-;
}
return f[ans];
}
inline ll findr(ll x)
{
int l=,r=tot,ans=;
while(l<=r)
{
if(f[mid]>=x)ans=mid,r=mid-;
else l=mid+;
}
return f[ans];
}
int solve(ll x)
{
ll l=findl(x),r=findr(x);
if(l==r)return ;
if(x-l<=r-x)return solve(x-l)+;
return solve(r-x)+;
}
int main()
{
f[]=f[]=;
while(f[tot-]<=4e17)
f[++tot]=f[tot-]+f[tot-];
int t;scanf("%d",&t);
ll x;
while(t--)scanf("%lld",&x),
printf("%d\n",solve(x));
}