Infinite sequence [simulation]

Description–

We generate a 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–

Conduct a first integer Q, followed by Q rows each separated by a space of two integer numbers a, b.

Output–

A total of Q lines, each line an answer


Sample Input–

1
2 8

Sample Output–

4


Notes -

1 <= Q <= 5000
1 <= a <= b < 263


Problem-solving ideas -

The i-th = th + i-1 of the two i-2
but also directly with the number of 1 ~ b is 1 minus the number of 1 ~ a 1 on ok (see the specific code)


Code -

#include<iostream>
#include<cstdio>
using namespace std;
long long q,x,y,by,a[100],b[100];
long long fy(long long ll)
{
	for (int i=1;i<=92;++i)
	{
		if (b[i]==ll) return a[i];
		if (b[i]>ll) return a[i-1]+fy(ll-b[i-1]);//分成两段
	}
} 
int main()
{
	a[1]=a[2]=b[1]=1,b[2]=2;
	for (int i=3;i<=92;++i)
	  a[i]=a[i-2]+a[i-1],b[i]=b[i-2]+b[i-1];//构造
	scanf("%lld",&q);
	for (int i=1;i<=q;++i)
	{
		scanf("%lld%lld",&x,&y);
		by=fy(y);
		if (x==1) printf("%lld\n",by);
		else printf("%lld\n",by-fy(x-1));//"b-a"
	}
	
	return 0;
} 

Guess you like

Origin blog.csdn.net/qq_43654542/article/details/91567858