【題解】AtCoder ABC 164D – Multiple of 2019

【題目敘述】https://atcoder.jp/contests/abc164/tasks/abc164_d

#include <iostream>
using namespace std;

int a[2][2019], ans;
string s;

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> s;
    for (int i = 0; i < s.length(); i++){
        int n = s[i]-'0';
        a[i%2][n] += 1;
        for (int j = 0; j < 2019; j++){
            a[i%2][(j*10+n)%2019] += a[(i+1)%2][j];
            a[(i+1)%2][j] = 0;
        }
        ans += a[i%2][0];
    }
    cout << ans << "\n";
}

分享本文 Share with friends