ZOJ Fibonacci in the Pocket

topic

Insert picture description here

thought

Find the parity of the sum of values ​​in the [a, b] range of the Fibonacci sequence.
First, we can first find the parity of the sum of the range sequence of [1, a-1] and the sum of the range sequence of [1, b]. Assign values ​​af and bf to the parity of the sum of the two numbers respectively (even is 0, odd is 1). There are four cases of af and bf. Where af and bf are odd and one even, it means that the sum of the range of [a, b] is odd; in the other case where af and bf are even and odd, it means that the sum of [a, b] is even.
Then, we need to understand the method to find the parity of the sum of the range of [1, i]. How to ask for it? The Fibonacci sequence is a sequence of three-number odd-even cycles-odd and even. At the same time, it also expresses that the sum of the cyclic number sequence is even. Then we can divide the [1, i] range sequence into [1, j] and [j+1, i] range sequence (when i%30,ji, [j+1, i] will not be considered). [1, j] The range sequence sum must be an even number, and only the parity of the [j+1, i] range sequence sum is required.
Finally, because the upper limit of a and b is too large, we will use a char array to store the values.

Code

#include<stdio.h>
#include<string.h>
#define MaxSize 10000
char num[MaxSize];

int qy(int xh)
{
    
    
    scanf("%s", num);
    int a=0;
    for(int i=0; i<strlen(num); i++){
    
    
        a = a*10 + (int)num[i];
        a %= 3;
    }
    if(xh==1){
    
    
        if(a==1 || a==0)
            return 0;
        else
            return 1;
    }
    else{
    
    
        if(a==2 || a==0)
            return 0;
        else
            return 1;
    }
}

int main(void)
{
    
    
    int tests;

    scanf("%d", &tests);
    while(tests--){
    
    
        int a = qy(1);
        int b = qy(2);
        if((a+b)%2)
            printf("1\n");
        else
            printf("0\n");
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/m0_46198140/article/details/107861952
ZOJ
ZOJ