[2019年浙江省省赛] I - Fibonacci in the Pocket 数学

题目链接:I - Fibonacci in the Pocket

题意

定义 f i = f i − 1 + f i − 2 , f 1 = 1 , f 2 = 1 {f_i=f_{i-1}+f_{i-2},f_1=1,f_2=1} fi=fi1+fi2f1=1,f2=1,给你一个区间[l,r],求 ∑ i = l r f i {\sum_{i=l}^rf_i} i=lrfi能否被2整除,如果可以则输出0,否则输出1。

题解

易知 f i {f_i} fi为斐波那契数列,并且通过分析可知,Fibonacci数列的奇偶性以每三个为“奇奇偶”一个循环。
中学我们知道一个定理:能被3整除的数其各个位数之和也能被3整除。
在此可以稍加推导
s = a 1 + 10 ∗ a 2 + 1 0 2 ∗ a 3 + . . . + 1 0 n ∗ a n + 1 {s=a_1+10*a_2+10^2*a_3+...+10^n*a_{n+1}} s=a1+10a2+102a3+...+10nan+1
s = ( a 1 + a 2 + . . . . + a n + 1 ) + 9 ∗ a 2 + 99 ∗ a 3 + . . . + 9999...999 ∗ a n + 1 {s=(a_1+a_2+....+a_{n+1})+9*a_2+99*a_3+...+9999...999*a_{n+1}} s=(a1+a2+....+an+1)+9a2+99a3+...+9999...999an+1
很明显后面的都能被3整除,所以只需判断 ( a 1 + a 2 + . . . . + a n + 1 ) {(a_1+a_2+....+a_{n+1})} (a1+a2+....+an+1)能否被3整除就行。
其实通过这个还能得到一个结论:
s % 3 = ( a 1 + a 2 + . . . . + a n + 1 ) % 3 { s\%3=(a_1+a_2+....+a_{n+1})\%3} s%3=(a1+a2+....+an+1)%3

知道了这个,我们取模就不需要用大数高精度去写,只需求出各个位数之和就行。至于剩下的分类讨论判断,情况也不多,在此供读者自己思考。

代码

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<deque>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
using namespace std;
//extern "C"{void *__dso_handle=0;}
typedef long long ll;
typedef long double ld;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define lowbit(x) x&-x

const double PI=acos(-1.0);
const double eps=1e-6;
const ll mod=1e9+7;
const int inf=0x3f3f3f3f;
const int maxn=1e5+10;
const int maxm=100+10;
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

int main()
{
    
    
	int t;
	cin >> t;
	while(t--)
	{
    
    
		string a,b;
		cin >> a >> b;
		int l=0,r=0;
		for(int i=0;i<a.length();i++) l+=a[i]-'0';
		for(int i=0;i<b.length();i++) r+=b[i]-'0';
		int ans=0;
		if(l%3==1 && r%3==1) ans=1;
		if(l%3==2 && r%3!=1) ans=1;
		if(l%3==0 && r%3==1) ans=1;
		cout << ans << endl;
	}
}

猜你喜欢

转载自blog.csdn.net/weixin_44235989/article/details/108053046