Fibonacci in the Pocket

Fibonacci in the Pocket

题目大意

判断fibonacci数列的\(\sum_{i=a}^{b}f_{i}\)是奇数还是偶数。\((1\leqslant a\leqslant b\leqslant 10^{10000})\)

题目分析:

斐波那契数列的奇偶周期性为3。

1  1  2  3  5  8 
奇 奇 偶 奇 奇 偶 

判断一个数是否整除3:

只需要判断这个数各个数位上的数字之和是否能整除3。

如果和能整除3,那么原数能整除3。
如果和不能整除3,那么余数就是原数除以3的余数。

我们对第a个到第b个数做如下判断:

a % 3 = 0:
表示为斐波拉契周期中第3个数

b % 3 = 1:\(\sum_{a}^{b}f_{i}\)为奇数。

b % 3 != 1:\(\sum_{a}^{b}f_{i}\)为偶数。

a % 3 = 1:
表示为斐波拉契周期中第1个数

b % 3 = 1:\(\sum_{a}^{b}f_{i}\)为奇数。

b % 3 != 1:\(\sum_{a}^{b}f_{i}\)为偶数。

a % 3 = 2:
表示为斐波拉契周期中第2个数

b % 3 = 1:\(\sum_{a}^{b}f_{i}\)为偶数。

b % 3 != 1:\(\sum_{a}^{b}f_{i}\)为奇数。

由此我们得到奇数情况为:l%3!=2 && r%3=1 || l%3=2 && r%3!=1

代码

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=1000000;
int lena,lenb;
int t,c,d;
string a,b;


inline int gcd(int a,int b){
    return b==0?a:gcd(b,a%b);
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin>>t;
	while(t--){
		c=0; d=0;
		cin>>a>>b;
		lena = a.length();
		lenb = b.length();
		for (int i=0;i<lena;i++){
			c = c+(a[i]-'0');
		}
		for (int j=0;j<lenb;j++){
			d = d+(b[j]-'0');
		}
		//cout<<c<<" "<<d<<endl;
		if (c%3!=2 && d%3!=1 || c%3==2 && d%3==1){
			cout<<"0"<<endl;
		}
		else
		cout<<"1"<<endl;	
	}
    return 0;
}
posted @ 2020-09-03 21:00  Treasure_lee  阅读(180)  评论(0)    收藏  举报