ZOJ 4108 Fibonacci in the Pocket (斐波那契 思维)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4108

斐波那契的奇偶周期为3:

1   1   2             3   5   8

奇 奇 偶(1 1 0) 奇 奇 偶(1 1 0)

3有一个数学规律,能够被3整除的数,各位数之和肯定能被3整除,所以这个题根本不需要高精度计算

因为偶数对奇偶变化无影响,所以我们只讨论奇数个数

对于[l,r],如果

l%3 = 1表示为斐波拉契周期中第1个数(奇数):

此时:

r%3=1时:\sum_{l}^{r}为奇数(l~r中包含奇数个奇数)

r%3!=1时:\sum_{l}^{r}为偶数(l~r中包含偶数个奇数)

 

l%3 = 2表示为斐波拉契周期中第2个数(奇数):

此时:

r%3=1时:\sum_{l}^{r}为偶数(l~r中包含偶数个奇数)

r%3!=1时:\sum_{l}^{r}为奇数(l~r中包含奇数个奇数)

 

l%3 = 0表示为斐波拉契周期中第3个数(偶数):

此时:

r%3=1时:\sum_{l}^{r}为奇数(l~r中包含奇数个奇数)

r%3!=1时:\sum_{l}^{r}为偶数(l~r中包含偶数个奇数)

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

其余情况均为偶数

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <cstring>
#define eps 1e-8
using namespace std;
typedef long long ll;
static const int MAX_N = 1e4 + 5;
static const ll Mod = 233;
static const int N = 105;
static const int INF = 0x3f3f3f3f;
char s1[MAX_N], s2[MAX_N];
int main(){
//    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);
    int T;
    scanf("%d", &T);
    while(T--){
        scanf("%s%s", s1, s2);
        int l = 0, r = 0;
        for(int i = 0; s1[i]; ++i) l += s1[i] - '0';
        for(int i = 0; s2[i]; ++i) r += s2[i] - '0';
        if(l % 3 == 2 && r % 3 != 1 || l % 3 != 2 && r % 3 == 1) puts("1");
        else puts("0");
    }
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章