[BZOJ2796][Poi2012]Fibonacci Representation

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

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

 1 #include<cstdio>
 2 #include<algorithm>
 3 #define ll long long
 4 #define mid (l+r>>1)
 5 using namespace std;
 6 ll f[505],tot=1;
 7 inline ll findl(ll x)
 8 {
 9     int l=1,r=tot,ans=1;
10     while(l<=r)
11     {
12         if(f[mid]<=x)ans=mid,l=mid+1;
13         else r=mid-1;
14     }
15     return f[ans];
16 }
17 inline ll findr(ll x)
18 {
19     int l=1,r=tot,ans=1;
20     while(l<=r)
21     {
22         if(f[mid]>=x)ans=mid,r=mid-1;
23         else l=mid+1;
24     }
25     return f[ans];
26 }
27 int solve(ll x)
28 {
29     ll l=findl(x),r=findr(x);
30     if(l==r)return 1;
31     if(x-l<=r-x)return solve(x-l)+1;
32     return solve(r-x)+1;
33 }
34 int main()
35 {
36     f[0]=f[1]=1;
37     while(f[tot-1]<=4e17)
38     f[++tot]=f[tot-1]+f[tot-2];
39     int t;scanf("%d",&t);
40     ll x;
41     while(t--)scanf("%lld",&x),
42     printf("%d\n",solve(x));
43 }
View Code

 

posted @ 2016-02-15 19:25  xuruifan  阅读(504)  评论(1编辑  收藏  举报