Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict

S = input()
N = len(S)

M = [0] * N
A = [0] * N
SUM = [0] * (N + 1)
MOD = 2019

x = 1
for i in range(N):
    M[i] = x % 2019
    x *= 10
for i, x in enumerate(S[::-1]):
    A[i] = M[i] * int(x) % MOD
for i in range(N):
    SUM[i + 1] = (SUM[i] + A[i]) % MOD

D = defaultdict(int)
ans = 0
for x in SUM:
    ans += D[x]
    D[x] += 1
print(ans)


Fixed submission:
S = input()
N = len(S)
SUM = [0] * (N + 1)
MOD = 2019

r = 1  # 10のi乗を2019で割った余り
S = S[::-1]
for i in range(N):
    A = r * int(S[i])
    SUM[i + 1] = (SUM[i] + A) % MOD
    r = r * 10 % MOD

cnt = [0] * 2019
ans = 0
for x in SUM:
    ans += cnt[x]
    cnt[x] += 1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict

S = input()
N = len(S)

M = [0] * N
A = [0] * N
SUM = [0] * (N + 1)
MOD = 2019

x = 1
for i in range(N):
    M[i] = x % 2019
    x *= 10
for i, x in enumerate(S[::-1]):
    A[i] = M[i] * int(x) % MOD
for i in range(N):
    SUM[i + 1] = (SUM[i] + A[i]) % MOD

D = defaultdict(int)
ans = 0
for x in SUM:
    ans += D[x]
    D[x] += 1
print(ans)


Fixed submission:
S = input()
N = len(S)
SUM = [0] * (N + 1)
MOD = 2019

r = 1  # 10のi乗を2019で割った余り
S = S[::-1]
for i in range(N):
    A = r * int(S[i])
    SUM[i + 1] = (SUM[i] + A) % MOD
    r = r * 10 % MOD

cnt = [0] * 2019
ans = 0
for x in SUM:
    ans += cnt[x]
    cnt[x] += 1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import itertools

s = input()
A = list(int(x) for x in s)
S = [0] * (len(s) + 1)
S[1:] = itertools.accumulate(A)

ans = 0
for j in range(4, len(s)):
    for i in range(len(s) - j + 1):
        x = int(s[i:i + j])
        if (S[i + j] - S[i]) % 3 == 0:
            if int(s[i:i + j]) % 673 == 0:
                ans += 1
                i = i + j - 1
print(ans)


Fixed submission:
S = input()
N = len(S)
SUM = [0] * (N + 1)
MOD = 2019

r = 1  # 10のi乗を2019で割った余り
S = S[::-1]
for i in range(N):
    A = r * int(S[i])
    SUM[i + 1] = (SUM[i] + A) % MOD
    r = r * 10 % MOD

cnt = [0] * 2019
ans = 0
for x in SUM:
    ans += cnt[x]
    cnt[x] += 1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
s = input()
ans = 0
for j in range(4, len(s)):
    for i in range(len(s) - j + 1):
        x = int(s[i:i + j])
        if x % 2019 == 0:
            ans += 1
            i = i + j - 1
print(ans)


Fixed submission:
S = input()
N = len(S)
SUM = [0] * (N + 1)
MOD = 2019

r = 1  # 10のi乗を2019で割った余り
S = S[::-1]
for i in range(N):
    A = r * int(S[i])
    SUM[i + 1] = (SUM[i] + A) % MOD
    r = r * 10 % MOD

cnt = [0] * 2019
ans = 0
for x in SUM:
    ans += cnt[x]
    cnt[x] += 1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
s = input()
ans = 0
for j in range(4, len(s)):
    for i in range(len(s) - j + 1):
        x = int(s[i:i + j])
        if x % 2019 == 0:
            ans += 1
print(ans)


Fixed submission:
S = input()
N = len(S)
SUM = [0] * (N + 1)
MOD = 2019

r = 1  # 10のi乗を2019で割った余り
S = S[::-1]
for i in range(N):
    A = r * int(S[i])
    SUM[i + 1] = (SUM[i] + A) % MOD
    r = r * 10 % MOD

cnt = [0] * 2019
ans = 0
for x in SUM:
    ans += cnt[x]
    cnt[x] += 1
print(ans)