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;
}

 

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