2019牛客暑期多校训练营(第四场)- K number

思维

不难想到300的倍数就是既是3的倍数也是100的倍数。

3的倍数的特征是各位之和为3的倍数,100的倍数后两位一定是0。

我们可以预处理出前缀和模3的结果,然后在每一次末尾连续两个0的时候统计答案,也就是加上之前模数相同的前缀的数量

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define full(a, b) memset(a, b, sizeof a)
#define FAST_IO ios::sync_with_stdio(false)
using namespace std;
typedef long long LL;
inline int lowbit(int x){ return x & (-x); }
inline int read(){
    int ret = 0, w = 0; char ch = 0;
    while(!isdigit(ch)){
        w |= ch == '-', ch = getchar();
    }
    while(isdigit(ch)){
        ret = (ret << 3) + (ret << 1) + (ch ^ 48);
        ch = getchar();
    }
    return w ? -ret : ret;
}
inline int lcm(int a, int b){ return a / __gcd(a, b) * b; }
template <typename A, typename B, typename C>
inline A fpow(A x, B p, C lyd){
    A ans = 1;
    for(; p; p >>= 1, x = 1LL * x * x % lyd)if(p & 1)ans = 1LL * x * ans % lyd;
    return ans;
}
const int N = 100005;
char s[N];
int sum[N];
LL cnt[3];

int main(){

    scanf("%s", s + 1);
    int len = strlen(s + 1);
    LL ans = 0;
    for(int i = 1; i <= len; i ++){
        sum[i] = (sum[i - 1] + (s[i] - '0')) % 3;
        if(s[i] == '0') ans ++;
    }
    cnt[0] ++;
    for(int i = 1; i <= len; i ++){
        if(i + 1 <= len && s[i] == '0' && s[i + 1] == '0') ans += cnt[sum[i]];
        cnt[sum[i]] ++;
    }
    printf("%lld\n", ans);
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/onionQAQ/p/11256271.html