2019ACM浙江省赛-Fibonacci in the Pocket

Fibonacci in the Pocket


Time Limit: 1 Second Memory Limit: 65536 KB

DreamGrid has just found a Fibonacci sequence f1,f2,··· and two integers and in his right pocket, where fk indicates the k-th element in the Fibonacci sequence.

Please tell DreamGrid if ∑ i = a b f i \displaystyle\sum_{i=a}^{b} f_i 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··· and 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< 1 0 10000 10^{10000} 1010000). Their meanings are described above.

Output
For each test case output one line. If ∑ i = a b f i \displaystyle\sum_{i=a}^{b} f_i i=abfi is even output “0” (without quotes); If ∑ i = a b f i \displaystyle\sum_{i=a}^{b} f_i 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

解析:
题意是求闭区间的斐波那契数列的和,如果和为偶数输出0,反之输出1。
斐波那契数列有个规律就是它总是以奇数,奇数,偶数的形式循环,
并且有个规律:3的倍数的各个位数加起来也是3的倍数。
我的做法是:
a1是闭区间左端点的各个位数的和,a2是闭区间右端点的各个位数的和.
只要a1-1和a2的余数同时不为1或者同时为1,则闭区间的和为偶数。

#include<iostream>
using namespace std;
int main(){
    int T;
    cin>>T;
    for(int i=0;i<T;i++){
        string s1,s2;
        cin>>s1>>s2;
        long long a1=0;
        long long a2=0;
        for(int j=0;j<s1.length();j++){
            a1=a1+s1[j];
        }
        for(int j=0;j<s2.length();j++){
            a2=a2+s2[j];
        }
        a1--;
        int f1=0;
        int f2=0;
        if(a1%3==0 || a1%3==2){
            f1=1;
        }
        if(a2%3==0 || a2%3==2){
            f2=1;
        }
        if((f1 && f2 )|| (!f1 && !f2)){
            cout<<0<<endl;
        }else{
            cout<<1<<endl;
        }
    }
    return 0;
}

你可能感兴趣的:(ACM2019浙江省赛)