Buggy python submission (verdict: Time Limit Exceeded):

from collections import defaultdict

S = input()
n = len(S)

mod = 2019
d = defaultdict(int)
d[0] = 1
num = 0
for i in reversed(range(n)):
    num += int(S[i]) * 10 ** (n - i - 1)
    num %= mod
    d[num] += 1

ans = 0
for v in d.values():
    ans += v * (v - 1) // 2
print(ans)


Fixed submission:

S = list(map(int, list(input())))

mod = 2019
ctr = [0] * mod
t = 0
for i, v in enumerate(reversed(S)):
    t = (t + int(v) * pow(10, i, mod)) % mod
    ctr[t] += 1

ans = ctr[0]
for v in ctr:
    ans += v * (v - 1) // 2
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):

from collections import defaultdict

S = input()
n = len(S)

mod = 2019
d = defaultdict(int)
d[0] = 1
num = 0
for i in reversed(range(n)):
    num += int(S[i]) * 10 ** (n - i - 1)
    d[num % mod] += 1

ans = 0
for v in d.values():
    ans += v * (v - 1) // 2
print(ans)


Fixed submission:

S = list(map(int, list(input())))

mod = 2019
ctr = [0] * mod
t = 0
for i, v in enumerate(reversed(S)):
    t = (t + int(v) * pow(10, i, mod)) % mod
    ctr[t] += 1

ans = ctr[0]
for v in ctr:
    ans += v * (v - 1) // 2
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):

S = list(map(int, list(input())))

mod = 2019
ctr = [0] * mod
p = 1
t = 0
for i, v in enumerate(reversed(S)):
    t = (t + int(v) * 10 ** i) % mod
    ctr[t] += 1

ans = ctr[0]
for v in ctr[1:]:
    ans += v * (v - 1) // 2
print(ans)


Fixed submission:

S = list(map(int, list(input())))

mod = 2019
ctr = [0] * mod
t = 0
for i, v in enumerate(reversed(S)):
    t = (t + int(v) * pow(10, i, mod)) % mod
    ctr[t] += 1

ans = ctr[0]
for v in ctr:
    ans += v * (v - 1) // 2
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):

S = input()

mod = 2019
ctr = [0] * mod
t = 0
for i, v in enumerate(reversed(S)):
    t = (t + int(v) * 10 ** i) % mod
    ctr[t] += 1

ans = ctr[0]
for v in ctr[1:]:
    ans += v * (v - 1) // 2
print(ans)


Fixed submission:

S = list(map(int, list(input())))

mod = 2019
ctr = [0] * mod
t = 0
for i, v in enumerate(reversed(S)):
    t = (t + int(v) * pow(10, i, mod)) % mod
    ctr[t] += 1

ans = ctr[0]
for v in ctr:
    ans += v * (v - 1) // 2
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):

S = input()

mod = 2019
ctr = [0] * mod
t = 0
for i, v in enumerate(S[::-1]):
    t = (t + int(v) * 10 ** i) % mod
    ctr[t] += 1

ans = ctr[0]
for v in ctr[1:]:
    ans += v * (v - 1) // 2
print(ans)


Fixed submission:

S = list(map(int, list(input())))

mod = 2019
ctr = [0] * mod
t = 0
for i, v in enumerate(reversed(S)):
    t = (t + int(v) * pow(10, i, mod)) % mod
    ctr[t] += 1

ans = ctr[0]
for v in ctr:
    ans += v * (v - 1) // 2
print(ans)

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

S = input()

mod = 2019
d = defaultdict(int)
t = 0
for i, v in enumerate(S[::-1]):
    t = (t + int(v) * 10 ** i) % mod
    d[t] += 1

ans = 0
for v, c in d.items():
    if v == 0:
        ans += c
    elif c > 1:
        ans += c * (c - 1) // 2
print(ans)


Fixed submission:

S = list(map(int, list(input())))

mod = 2019
ctr = [0] * mod
t = 0
for i, v in enumerate(reversed(S)):
    t = (t + int(v) * pow(10, i, mod)) % mod
    ctr[t] += 1

ans = ctr[0]
for v in ctr:
    ans += v * (v - 1) // 2
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
S = input()

n = len(S)
memo = set()
ans = 0
for i in range(n):
    for j in range(i, n):
        v = int(S[i:j + 1])
        if v in memo or v  % 2019 == 0:
            ans += 1
            memo.add(v)
            
print(ans)


Fixed submission:

S = list(map(int, list(input())))

mod = 2019
ctr = [0] * mod
t = 0
for i, v in enumerate(reversed(S)):
    t = (t + int(v) * pow(10, i, mod)) % mod
    ctr[t] += 1

ans = ctr[0]
for v in ctr:
    ans += v * (v - 1) // 2
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
S = input()

n = len(S)
ans = 0
for i in range(n):
    for j in range(i, n):
        if int(S[i:j + 1]) % 2019 == 0:
            ans += 1
            
print(ans)


Fixed submission:

S = list(map(int, list(input())))

mod = 2019
ctr = [0] * mod
t = 0
for i, v in enumerate(reversed(S)):
    t = (t + int(v) * pow(10, i, mod)) % mod
    ctr[t] += 1

ans = ctr[0]
for v in ctr:
    ans += v * (v - 1) // 2
print(ans)