Infinite sequence [Fibonacci number]

> Description
We generated sequence as follows:
1, at the start of the sequence is: "1";
2, every change in the sequence of "1" to "10", "0" to "1."
After an unlimited number of changes, we get the sequence "1011010110110101101 ...."
There are a query Q, each query is: 1 between the number of sections A and B.
Task Write a program to answer the Q inquiry


> Input
first line an integer Q, followed by Q rows each separated by a space of two integer numbers a, b.

> Output
total of Q lines, each line an answer


>Sample Input
1
2 8

>Sample Output
4

1 <= Q <= 5000
1 <= a <= b < 2^63


> Outline of Solution
after law can be found include:
each of (i) the number of times changes come as the first (i-1) + views of the (i-2) times - that is the Fibonacci columns
Therefore, in the same manner number, (i), the number 1 for the first column (i-1) + views of the (i-2) times.

Since a ~ b can be seen as: (1 ~ b) - ( 1 ~ a-1), and the prefix,
and then, the number of columns 1 ~ 1 i in number, can be seen through a two changes come number 1 and (a + b), respectively, wherein the two variations which come length and the number of columns must i.


> Code

#include<iostream>
#include<cstdio>
using namespace std;
int n;
long long aa,bb,f[100],c[100];
long long ooo(long long cc)
{
	for(int i=1;i<=92;i++)
	 if(c[i]==cc) return f[i];
	 else if(c[i]>cc) return f[i-1]+ooo(cc-c[i-1]);
}
int main()
{
	scanf("%d",&n);
	f[1]=f[2]=1; //f[i]记录第i个数列中1的个数
	c[1]=1; c[2]=2; //c[i]记录第i个数列的长度
	for(int i=3;i<=92;i++)
	 f[i]=f[i-1]+f[i-2],c[i]=c[i-1]+c[i-2]; //斐波那契数列
	for(int i=1;i<=n;i++)
	{
		scanf("%lld%lld",&aa,&bb);
		long long li=ooo(bb),sa;
		if(aa-1==0) sa=0; //如果数列为1~bb的话就只用求bb
		else sa=ooo(aa-1);
		printf("%lld\n",li-sa); 
	}
	return 0;
} 

Guess you like

Origin blog.csdn.net/qq_43010386/article/details/90729499