题目链接
Fibonacci in the Pocket
Time Limit: 1 Second Memory Limit: 65536 KB
DreamGrid has just found a Fibonacci sequence f1,f2,… and two integers a and b in his right pocket, where fk indicates the k-th element in the Fibonacci sequence.
Please tell DreamGrid if ∑i=abfi is even or is odd.
Recall that a Fibonacci sequence is an infinite sequence which satisfies f1=1, f2=1 and fi=fi−1+fi−2 for all i≥3.
Input
There are multiple test cases. The first line of the input contains an integer T (about 100), indicating the number of test cases. For each test case:
The first and only line contains two integers a and b (1≤a≤b<1010000). Their meanings are described above.
Output
For each test case output one line. If ∑i=abfi is even output "0" (without quotes); If ∑i=abfi is odd output "1" (without quotes).
Sample Input
6 1 2 1 3 1 4 1 5 123456 12345678987654321 123 20190427201904272019042720190427
Sample Output
0 0 1 0 0 1
Hint
The first few elements of the Fibonacci sequence are: f1=1, f2=1, f3=2, f4=3, f5=5, f6=8...
题解:
对于斐波拉契的每一项
1 1 2 3 5 8 13 21
容易知道其单项的奇偶性为 1 1 0 1 1 0 1 1 0(1代表奇数,0代表偶数),是以3为循环节的序列。
容易知道其前缀和为 1 0 0 1 0 0 1 0 0 1 0 0,也是以3为循环节的序列。
对于一个数能不能取余3只需要把每一位加起来看能不能取余3即可 例如111111一定可以取余3,因为(1+1+1+1+1+1)=6可以取余3。也可以直接用JAVA、python直接大数取余。
对于题目,先求出 序列 1 - a的奇偶性,然后求出序列 1 - b的奇偶性,然后判断一下,如果两个的奇偶性相同就代表是偶数,如果不用则是奇数。
#include<bits/stdc++.h>
using namespace std;
char str1[100005];
char str2[100005];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%s %s",str1,str2);
int len1=strlen(str1);
int len2=strlen(str2);
long long ans1=0;
long long ans2=0;
for(long long i=0; i<len1; i++)
{
ans1+=str1[i]-'0';
}
for(long long i=0; i<len2; i++)
{
ans2+=str2[i]-'0';
}
if(ans1==ans2)//如果a等于b直接判断一个的奇偶性就好了
{
if(ans2%3==0)
printf("0\n");
else
printf("1\n");
}
else if(ans1==1)//如果从 1 开始就不需要两个序列了
{
if(ans2%3==1)
{
printf("1\n");
}
else
printf("0\n");
}
else if(ans1%3==0)//如果 1-a 此序列是偶数
{
if(ans2%3==1)
printf("1\n");
else
printf("0\n");
}
else
{
if((ans1+1)%3==0)
{
if(ans2%3==1)
{
printf("0\n");
}
else
printf("1\n");
}
else
{
if(ans2%3==1)
{
printf("1\n");
}
else
printf("0\n");
}
}
}
}