AtCoder Beginner Contest 164 D.Multiple of 2019(DP)_ci


AtCoder Beginner Contest 164 D.Multiple of 2019(DP)_ci_02


从后往前 AtCoder Beginner Contest 164 D.Multiple of 2019(DP)_ci_03

AC代码:

const int N = 2e5 + 10;
int n, m;
string s;
ll ans, res, now;
int cnt[N];
int main()
{
cin >> s;
int len = s.size();
mem(cnt, 0);
cnt[0] = 1;
res = 1;
ans = 0;
now = 0;
per(i, len-1, 0)
{
now = (now + ll(s[i] - '0') * res) % 2019;
ans += cnt[now]++;
res = res * 10 % 2019;
}
pld(ans);
return 0;
}