http://noip.ybtoj.com.cn/contest/9/problem/3
啊,講道理,這道題想了好久。。
一開始想用移動下標的方式來做,但發現自己找的規律完全是錯的。。。。
然后試圖從這個序列的10進制數下手,發現依然沒有規律。然后嘗試最無腦的思路:不管二進制數的意義,只管0和1的個數,然后真的找到了規律!!!
於是得到了我們的代碼
#include<bits/stdc++.h> using namespace std; typedef unsigned long long LL; int Q; LL a,b,p[105]; LL f(LL x) { LL ans=0,j; while(x) { j=1;//cnt[]和f[]的斐波那契數列錯開了一位 while(p[j+2]<=x) j++; ans+=p[j]; x-=p[j+1]; } return ans; } int main() { p[1]=1;p[2]=1;//此處求的是cnt的斐波那契數列。 for(int i=3;i<=100;i++) p[i]=p[i-1]+p[i-2]; scanf("%d",&Q); for(int i=1;i<=Q;i++) { scanf("%lld%lld",&a,&b); printf("%lld\n",f(b)-f(a-1)); } return 0; }
