单点时限: 1.0 sec
内存限制: 512 MB
聪明的小蚂蚁最近学习了斐波那契数列,但是它想到了一个问题:
从L到R之间斐波那契数列和的奇偶是什么呢?
其中Fib[1]=1,Fib[2]=1 .
输入格式
单组输入:
每组输入两个以空格隔开的数字 L 和 R
其中 (0<L<=R<1018)
输出格式
从 L 到 R 斐波那契数列和的奇偶,如果是奇数输出 "1" (不带引号) ,否则输出 "0" (不带引号)
样例
input
1 2
output
0
被这个题目卡了很久 主要是 当l与R相同时,他们指的是同一个数,这时候不能再求和了,而是直接判断。还有就是关于斐波切数列的一些性质 当第N项可以被3整除时,此时的项对应的数为偶数,还有一个公式就是前n项斐波那契的奇数的个数 2*n/3 要向上取整。
思路1:
#include<iostream> #include<cstdio> #include<algorithm> using namespace std; typedef long long ll; ll l,r; int main(){ cin>>l>>r; ll x,y; if(l%3==0){ x=2*l/3; } else { x=2*l/3+1; } if(r%3==0){ y=2*r/3; } else { y=2*r/3+1; } if(l%3==0){ if((y-x)%2==0){ cout<<0<<endl; } else { cout<<1<<endl; } } else { if((y-x+1)%2==0){ cout<<0<<endl; } else { cout<<1<<endl; } } return 0; }
思路2根据偶数个数求奇数单位个数:
#include<iostream> #include<cstdio> #include<algorithm> using namespace std; typedef long long ll; ll l,r; int main(){ cin>>l>>r;//前n项中偶数的个数就是n/3; ll x=l/3; ll y= r/3; x=l-x;//前l项中奇数的个数 y=r-y;//前r项中奇数的个数 if(l%3==0){ if((y-x)%2==0){ cout<<0<<endl; } else { cout<<1<<endl; } } else { if((y-x+1)%2==0){ cout<<0<<endl; } else { cout<<1<<endl; } } return 0; }