Buggy python submission (verdict: Time Limit Exceeded):
def n_gram(target,n):
    count = 0
    for idx in range(len(target) - n + 1):
        if int(target[idx:idx+n]) % 2019 == 0:
            count += 1
    return count

c = 0
s = input()
n = 4
num_list = ()
s_len = len(s)
while n <= s_len:
    c += n_gram(s,n)
    n += 1
print(c)

Fixed submission:
s = input()[::-1]

count = [0]*2019
count[0] = 1  # そもそもあまりが0ってことは2019の倍数確定なので

num, d = 0, 1  # 初期化

for char in s:
    num += int(char)*d
    num %= 2019
    d *= 10
    d %= 2019
    count[num] += 1  # 2019で割ったあまりをキーとして値を追加していく

ans = 0
for c in count:
    ans += c*(c-1)//2
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
def n_gram(target,n):
    a = (int(target[idx:idx + n]) for idx in range(len(target) - n + 1))
    return tuple(a)

count = 0
s = input()
n = 4
num_list = ()
s_len = len(s)
while n <= s_len:
    num_list = num_list + n_gram(s,n)
    n += 1
for num in num_list:
    if num % 2019 == 0:
        count += 1
print(count)

Fixed submission:
s = input()[::-1]

count = [0]*2019
count[0] = 1  # そもそもあまりが0ってことは2019の倍数確定なので

num, d = 0, 1  # 初期化

for char in s:
    num += int(char)*d
    num %= 2019
    d *= 10
    d %= 2019
    count[num] += 1  # 2019で割ったあまりをキーとして値を追加していく

ans = 0
for c in count:
    ans += c*(c-1)//2
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
def n_gram(target,n):
    return [int(target[idx:idx + n]) for idx in range(len(target) - n + 1)]

count = 0
s = input()
n = 4
num_list = []
while n <= len(s):
    num_list.extend(n_gram(s,n))
    n += 1

for num in num_list:
    if num % 2019 == 0:
        count += 1
print(count)

Fixed submission:
s = input()[::-1]

count = [0]*2019
count[0] = 1  # そもそもあまりが0ってことは2019の倍数確定なので

num, d = 0, 1  # 初期化

for char in s:
    num += int(char)*d
    num %= 2019
    d *= 10
    d %= 2019
    count[num] += 1  # 2019で割ったあまりをキーとして値を追加していく

ans = 0
for c in count:
    ans += c*(c-1)//2
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
def n_gram(target,n):
    return [int(target[idx:idx + n]) for idx in range(len(target) - n + 1)]

count = 0
s = input()
n = 4
while n <= len(s):
    for num in n_gram(s,n):
        if int(num) % 2019 == 0:
            count += 1
    n += 1
print(count)

Fixed submission:
s = input()[::-1]

count = [0]*2019
count[0] = 1  # そもそもあまりが0ってことは2019の倍数確定なので

num, d = 0, 1  # 初期化

for char in s:
    num += int(char)*d
    num %= 2019
    d *= 10
    d %= 2019
    count[num] += 1  # 2019で割ったあまりをキーとして値を追加していく

ans = 0
for c in count:
    ans += c*(c-1)//2
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
def n_gram(target,n):
    return [target[idx:idx + n] for idx in range(len(target) - n + 1)]

count = 0
s = input()
n = 4
while n <= len(s):
    num_list = n_gram(s,n)
    for num in num_list:
        if int(num) % 2019 == 0:
            count += 1
    n += 1
print(count)

Fixed submission:
s = input()[::-1]

count = [0]*2019
count[0] = 1  # そもそもあまりが0ってことは2019の倍数確定なので

num, d = 0, 1  # 初期化

for char in s:
    num += int(char)*d
    num %= 2019
    d *= 10
    d %= 2019
    count[num] += 1  # 2019で割ったあまりをキーとして値を追加していく

ans = 0
for c in count:
    ans += c*(c-1)//2
print(ans)