ZOJ-4108 Fibonacci in the Pocket 2019浙江省省赛

题目链接

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≤ab<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");
			}
		}
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值