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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值