YbtOJ练习:递推3 无限序列

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
",f(b)-f(a-1));
    }
    return 0;
}
原文地址:https://www.cnblogs.com/smartljy/p/13417432.html