【zznu-夏季队内积分赛3-G】2333

题目描述

“别人总说我瓜,其实我一点也不瓜,大多数时候我都机智的一批“ 宝儿姐考察你一道很简单的题目。给你一个数字串,你能判断有多少个连续子串能整除3吗?

输入

多实例输入,以EOF结尾,每行一个数字串(长度<=1e6)

输出

每行一个数字串,表示能整除3的连续子串的个数

样例输入

2333

样例输出

6
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6+3;
int a[N], b[5];
char s[N];
typedef long long LL;
int main()
{
    while(~scanf("%s", s+1))
    {
        int l = strlen(s+1);
        for(int i = 1; i <= l; i++)
            a[i] = a[i-1] + s[i] - '0';
        LL ans = 0;
        memset(b, 0, sizeof b);
        b[0] = 1;
        for(int i = 1; i <= l; i++)
        {
            a[i] %= 3;
            ans += b[a[i]];
            b[a[i]] ++;
        }
        printf("%lld
", ans);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/lesroad/p/9438005.html