alexjercan/bugnet huggingface dataset:
Problem p02700 (Python) — buggy submission, verdict: Time Limit Exceeded

a, b, c, d = map(int, input().split())
while True:
    c -= b
    if c <= 0:
        print("Yes")
        break
    a -= d
    if a <= 0:
        print("No")
        break


Error: TLE

Fixed submission:

a, b, c, d = map(int, input().split())
while True:
    c -= b
    if c == 0:
        print("Yes")
        break
    a -= d
    if a == 0:
        print("No")
        break
Problem p02701 (Python) — buggy submission, verdict: Time Limit Exceeded

N = int(input())
L = set()
for _ in range(N):
    s = input()
    if s in L:
        pass
    else:
        L.add(s)

print(len(L))


Error: TLE

Fixed submission:

N = int(input())
L = []
for _ in range(N):
    s = input()
    if s in L:
        pass
    else:
        L.append(s)

print(len(L))
Problem p02701 (Python) — buggy submission, verdict: Time Limit Exceeded

n = int(input())

gacha_set = set()
for _ in range(n):
    s = input()
    gacha_set.add(s)

print(len(gacha_set))


Error: TLE

Fixed submission:

n = int(input())

unique_list = list()
for _ in range(n):
    s = input()
    if s not in unique_list:
        unique_list.append(s)

print(len(unique_list))
Problem p02701 (Python) — buggy submission, verdict: Runtime Error

import numpy as np

N = int(input())
S = [input() for i in range(N)]

print(len(np.unique(S)))


Error: TLE

Fixed submission:

import pandas as pd

N = int(input())
S = [input() for i in range(N)]

data = pd.Series(S)

print(data.nunique())
Problem p02701 (Python) — buggy submission, verdict: Time Limit Exceeded

n = int(input())
s = []
p = set()
ans = 0
for i in range(n):
    s.append(input())
    if s[i] in p:
        pass
    else:
        p.add(s[i])
        ans += 1
print(ans)


Error: TLE

Fixed submission:

n = int(input())
s = []
p = []
ans = 0
for i in range(n):
    s.append(input())
    if s[i] in p:
        pass
    else:
        p.append(s[i])
        ans += 1
print(ans)
Problem p02701 (Python) — buggy submission, verdict: Time Limit Exceeded

N = int(input())
D = {}
ans = 0

for _ in range(N):
    S = input()
    if S not in D:
        D[S] = True
        ans += 1

print(ans)


Error: TLE

Fixed submission:

N = int(input())
D = []
ans = 0

for _ in range(N):
    S = input()
    if S not in D:
        D.append(S)
        ans += 1

print(ans)
Problem p02701 (Python) — buggy submission, verdict: Runtime Error

a = int(input())
s = [input() for i in range(a)]

print(len(set(s)))


Error: ValueError: invalid literal for int() with base 10: 'apple'
Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02701/Python/s227226698.py", line 2, in <module>
    s = list(map(int, input().split()))
ValueError: invalid literal for int() with base 10: 'apple'


Fixed submission:

a = map(int, input().split())
s = list(map(int, input().split()))

print(len(set(s)))
Problem p02701 (Python) — buggy submission, verdict: Runtime Error

n = int(input())
s = [input() for _ in range(n)]
ans = set()

for i in s:
    ans.add(i)

print(len(ans))


Error: ValueError: invalid literal for int() with base 10: 'apple'
Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02701/Python/s962277082.py", line 2, in <module>
    s = [int(input()) for _ in range(n)]
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02701/Python/s962277082.py", line 2, in <listcomp>
    s = [int(input()) for _ in range(n)]
ValueError: invalid literal for int() with base 10: 'apple'


Fixed submission:

n = int(input())
s = [int(input()) for _ in range(n)]
ans = set()

for i in s:
    ans.add(i)

print(len(ans))
Problem p02701 (Python) — buggy submission, verdict: Time Limit Exceeded

import collections

N = int(input())
S_list = [input() for i in range(N)]
c = collections.Counter(S_list)
print(len(c))


Error: TLE

Fixed submission:

N = int(input())
S_list = [input() for i in range(N)]
count = 0
while len(S_list) > 0:
    S_list = [s for s in S_list if s != S_list[0]]
    count += 1
print(count)
Problem p02701 (Python) — buggy submission, verdict: Time Limit Exceeded

n = int(input())

l = [input() for _ in range(n)]
print(len(set(l)))


Error: TLE

Fixed submission:

n = int(input())

ans = 0
u_l = []
for _ in range(n):
    s = input()
    if s not in u_l:
        u_l.append(s)
        ans += 1
print(ans)
Problem p02701 (Python) — buggy submission, verdict: Runtime Error

n = int(input())
s = {input() for _ in range(n)}
print(len(s))


Error: TypeError: object of type 'generator' has no len()
Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02701/Python/s709317664.py", line 3, in <module>
    print(len(s))
TypeError: object of type 'generator' has no len()


Fixed submission:

n = int(input())
s = (input() for _ in range(n))
print(len(s))
Problem p02701 (Python) — buggy submission, verdict: Runtime Error

N = int(input())
List = list(input() for i in range(N))
print(len(set(List)))


Error: TypeError: 'str' object cannot be interpreted as an integer
Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02701/Python/s903293880.py", line 2, in <module>
    List = list(input() for i in range(N))
TypeError: 'str' object cannot be interpreted as an integer


Fixed submission:

N = input()
List = list(input() for i in range(N))
print(len(set(List)))
Problem p02701 (Python) — buggy submission, verdict: Time Limit Exceeded

N = int(input())
S = []
for _ in range(N):
    S.append(input())

print(len(set(S)))


Error: TLE

Fixed submission:

N = int(input())
S = []
for _ in range(N):
    i = input()
    if i not in S:
        S.append(i)

print(len(S))
Problem p02701 (Python) — buggy submission, verdict: Runtime Error

n = int(input())
lis = [input() for i in range(n)]
print(len(set(lis)))


Error: TypeError: int() argument must be a string, a bytes-like object or a real number, not 'builtin_function_or_method'
Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02701/Python/s310001383.py", line 1, in <module>
    n = int(input)
TypeError: int() argument must be a string, a bytes-like object or a real number, not 'builtin_function_or_method'


Fixed submission:

n = int(input)
lis = [input() for i in range(n)]
print(len(set(lis)))
Problem p02701 (Python) — buggy submission, verdict: Time Limit Exceeded

n = int(input())
s = [""] * n
for i in range(n):
    s[i] = input()
s = set(s)
print(len(s))


Error: TLE

Fixed submission:

n = int(input())
s = [""] * n
t = []
for i in range(n):
    s[i] = input()
for i in range(n):
    if s[i] not in t:
        t.append(s[i])
print(len(t))
Problem p02701 (Python) — buggy submission, verdict: Runtime Error

import collections

n = int(input())
a = [input() for i in range(n)]
b = collections.Counter(a)
print(len(b))


Error: TypeError: unhashable type: 'list'
Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02701/Python/s293744193.py", line 5, in <module>
    b = collections.Counter(a)
  File "/usr/lib/python3.10/collections/__init__.py", line 577, in __init__
    self.update(iterable, **kwds)
  File "/usr/lib/python3.10/collections/__init__.py", line 670, in update
    _count_elements(self, iterable)
TypeError: unhashable type: 'list'


Fixed submission:

import collections

n = int(input())
a = [list(input() for i in range(n))]
b = collections.Counter(a)
print(len(b))
Problem p02701 (Python) — buggy submission, verdict: Time Limit Exceeded

import sys


def main():
    readline = sys.stdin.readline
    N = int(readline())
    se = set([readline() for _ in range(N)])
    print(len(se))


if __name__ == "__main__":
    main()


Error: TLE

Fixed submission:

import sys


def main():
    readline = sys.stdin.readline
    N = int(readline())
    li = []
    for _ in range(N):
        S = readline()
        if S not in li:
            li.append(S)
    print(len(li))


if __name__ == "__main__":
    main()
Problem p02702 (Python) — buggy submission, verdict: Time Limit Exceeded

def solve(s):
    dp = [0] * 2019
    dp[0] = 1
    r = 0
    for i in range(1, len(s) + 1):
        r += int(s[-i]) * pow(10, i - 1, 2019)
        r %= 2019
        dp[r] += 1
    res = 0
    for r in range(2019):
        res += dp[r] * (dp[r] - 1) // 2
    return res


def main():
    s = input()
    res = solve(s)
    print(res)


def test():
    assert solve("1817181712114") == 3
    assert solve("14282668646") == 2
    assert solve("2119") == 0


if __name__ == "__main__":
    test()
    main()


Error: TLE

Fixed submission:

def solve(s):
    dp = [0] * 2019
    dp[0] = 1
    r = 0
    for i in range(1, len(s) + 1):
        r += int(s[-i]) * (10 ** (i - 1))
        r %= 2019
        dp[r] += 1
    res = 0
    for r in range(2019):
        res += dp[r] * (dp[r] - 1) // 2
    return res


def main():
    s = input()
    res = solve(s)
    print(res)


def test():
    assert solve("1817181712114") == 3
    assert solve("14282668646") == 2
    assert solve("2119") == 0


if __name__ == "__main__":
    test()
    main()
Problem p02702 (Python) — buggy submission, verdict: Time Limit Exceeded

S = input()
digits = list(map(int, S))
divisor = 2019

rem_count = [0] * divisor
rem_count[0] = 1
num = 0
pow10 = 1
total = 0

for digit in reversed(digits):
    num = digit * pow10 + num
    num %= divisor
    total += rem_count[num % divisor]
    pow10 *= 10
    pow10 %= divisor
    rem_count[num % divisor] += 1

print(total)


Error: TLE

Fixed submission:

S = input()
digits = list(map(int, S))
divisor = 2019

rem_count = [0] * divisor
rem_count[0] = 1
num = 0
pow10 = 1
total = 0

for digit in reversed(digits):
    num = digit * pow10 + num
    total += rem_count[num % divisor]
    pow10 *= 10
    rem_count[num % divisor] += 1

print(total)
Problem p02702 (Python) — buggy submission, verdict: Time Limit Exceeded

s = str(input())

num = [int(s[-1])]
for i in range(1, len(s)):
    tmp = num[-1] + pow(10, i, 2019) * int(s[-i - 1])
    num.append(tmp % 2019)

mod = [1] + [0] * 2018
ans = 0
for i in num:
    m = i % 2019
    ans += mod[m]
    mod[m] += 1
print(ans)


Error: TLE

Fixed submission:

s = str(input())

num = [int(s[-1])]
for i in range(1, len(s)):
    num.append(num[-1] + pow(10, i) * int(s[-i - 1]))

mod = [1] + [0] * 2018
ans = 0
for i in num:
    m = i % 2019
    ans += mod[m]
    mod[m] += 1
print(ans)
Problem p02702 (Python) — buggy submission, verdict: Time Limit Exceeded

# D - Multiple of 2019

s = input()

# pow[k] = 10 ** k % 2019
pow = [1]
while len(pow) <= 1 or pow[-1] > 1:
    pow.append(pow[-1] * 10 % 2019)
n = len(pow) - 1  # 10 ** n == 1 mod 2019

# memo[i % n][m] = (int(s[:i+1]) % 2019 == mであるiの個数)
memo = [[0] * 2019 for i in range(n)]
memo[n - 1][0] = 1  # memo[-1][0] = 1

count = 0  # int(s[i:j+1]) == 0 mod 2019 である(i,j)の個数
mod = 0
for j, c in enumerate(s):
    mod = (mod * 10 + int(c)) % 2019  # mod = int(s[:j+1]) % 2019
    for i in range(1, n + 1):
        m = mod * pow[n - i] % 2019  # m == mod * 10 ** (-i) mod 2019
        count += memo[(j - i + n) % n][m]
    memo[j % n][mod] += 1

print(count)


Error: TLE

Fixed submission:

# D - Multiple of 2019

s = input()

# pow[k] = 10 ** k % 2019
pow = [1]
while len(pow) <= 1 or pow[-1] > 1:
    pow.append(pow[-1] * 10 % 2019)
n = len(pow) - 1  # 10 ** n == 1 mod 2019

# memo[i % n][m] = (int(s[:i+1]) % 2019 == mであるiの個数)
memo = [{} for i in range(n)]
memo[n - 1][0] = 1  # memo[-1][0] = 1

count = 0  # int(s[i:j+1]) == 0 mod 2019 である(i,j)の個数
mod = 0
for j, c in enumerate(s):
    mod = (mod * 10 + int(c)) % 2019  # mod = int(s[:j+1]) % 2019
    for i in range(1, n + 1):
        m = mod * pow[n - i] % 2019  # m == mod * 10 ** (-i) mod 2019
        if m in memo[(j - i + n) % n]:
            count += memo[(j - i + n) % n][m]
    if mod not in memo[j % n]:
        memo[j % n][mod] = 0
    memo[j % n][mod] += 1

print(count)
Problem p02702 (Python) — buggy submission, verdict: Time Limit Exceeded

def main():
    A = input()[::-1]
    A = "0" + A
    # lis_num = [int(s) for s in A]

    # print(lis_num)

    # S = [0] * len(lis_num)
    S = [0] * len(A)
    cnt = [0] * 2019
    cnt[0] = 1

    for i in range(len(A) - 1):
        S[i + 1] = (S[i] + int(A[i + 1]) * pow(10, i, 2019)) % 2019
        cnt[S[i + 1] % 2019] += 1

    # print(S)

    # for i in S:
    #     cnt[i % 2019] += 1

    ans = 0

    for i in cnt:
        ans += i * (i - 1) // 2

    print(ans)


if __name__ == "__main__":
    main()


Error: TLE

Fixed submission:

def main():
    A = input()[::-1]
    A = "0" + A
    # lis_num = [int(s) for s in A]

    # print(lis_num)

    # S = [0] * len(lis_num)
    S = [0] * len(A)
    cnt = [0] * 2019
    cnt[0] = 1

    for i in range(len(A) - 1):
        S[i + 1] = (S[i] + int(A[i + 1]) * pow(10, i)) % 2019
        cnt[S[i + 1] % 2019] += 1

    # print(S)

    # for i in S:
    #     cnt[i % 2019] += 1

    ans = 0

    for i in cnt:
        ans += i * (i - 1) // 2

    print(ans)


if __name__ == "__main__":
    main()
Problem p02702 (Python) — buggy submission, verdict: Time Limit Exceeded

from collections import defaultdict

S = input()

d = defaultdict(int)

d[0] += 1

mod = 0
R = 1
for i in range(len(S)):
    mod = (mod + R * int(S[len(S) - i - 1])) % 2019
    R = R * 10 % 2019
    d[mod] += 1
ans = 0
for i in d.values():
    if i > 1:
        ans += i * (i - 1) / 2

print(int(ans))


Error: TLE

Fixed submission:

from collections import defaultdict

S = input()

d = defaultdict(int)

d[0] += 1

mod = 0
for i in range(len(S))[::-1]:
    mod = (mod + int(S[i]) * 10 ** (len(S) - i - 1)) % 2019
    # mod = int(S[i:]) % 2019
    d[mod] += 1

ans = 0
for i in d.values():
    if i > 1:
        ans += i * (i - 1) / 2

print(int(ans))
Problem p02702 (Python) — buggy submission, verdict: Time Limit Exceeded

from collections import Counter


mod = 2019
S = map(int, reversed(input()))

mods = [0]
num = 0
digit = 1
for s in S:
    num += s * digit
    num %= mod
    mods.append(num)
    digit = digit * 10 % mod

counter = Counter(mods)
ans = 0
for count in counter.values():
    ans += count * (count - 1) // 2
print(ans)


Error: TLE

Fixed submission:

import numpy as np

mod = 2019
mods_count = np.zeros(2019, dtype=np.int64)
indexes = list(range(2019))
indexes_10 = [index * 10 % mod for index in indexes]

mod = 2019
ans = 0
S = input()
for s in S:
    num = int(s)
    new = np.roll(mods_count, num)
    new[num] += 1

    ans += new[0]
    mods_count = np.zeros(2019, dtype=np.int64)
    mods_count[indexes_10] += new[indexes]

print(ans)
Problem p02702 (Python) — buggy submission, verdict: Time Limit Exceeded

S = input()
N = len(S)

num = [0] * 2019
num[0] = 1
now, ans = 0, 0
_10 = 1

for i in range(N - 1, -1, -1):
    now = (now + int(S[i]) * _10) % 2019
    _10 *= 10
    _10 %= 2019
    ans += num[now]
    num[now] += 1

print(ans)


Error: TLE

Fixed submission:

S = input()
N = len(S)

cnt = 0
for i in range(N - 3):
    for j in range(i + 4, N + 1):
        if int(S[i:j]) % 2019 == 0:
            # print(i, j)
            cnt += 1
print(cnt)
Problem p02702 (Python) — buggy submission, verdict: Time Limit Exceeded

s = list(input())
sur_list = [0 for i in range(2019)]
sur = 0
keta = 1
ans = 0

s.reverse()

for i in range(len(s)):
    keta = (keta * 10) % 2019
    sur_list[sur] += 1
    sur = (int(s[i]) * keta + sur) % 2019
    ans += sur_list[sur]

print(ans)


Error: TLE

Fixed submission:

s = list(input())
sur_list = [0 for i in range(2019)]
sur = 0
ans = 0

s.reverse()

for i in range(len(s)):
    sur_list[sur] += 1
    sur = (int(s[i]) * (10**i) + sur) % 2019
    ans += sur_list[sur]

print(ans)
Problem p02702 (Python) — buggy submission, verdict: Time Limit Exceeded

if __name__ == "__main__":

    s = list(input())
    s.reverse()
    n = len(s)
    now = 0
    mods = [0] * 2019
    mods[0] += 1
    ten = 1
    for i in range(n):
        now += ten * int(s[i])
        now %= 2019
        ten *= 10
        ten %= 2019
        mods[now] += 1
    ans = 0
    for i in mods:
        ans += (i * (i - 1)) // 2
    print(ans)


Error: TLE

Fixed submission:

s = list(input())
s.reverse()
n = len(s)
now = 0
mods = [0] * 2019
mods[0] += 1
for i in range(n):
    now += pow(10, i) * int(s[i])
    now %= 2019
    mods[now] += 1
ans = 0
for i in mods:
    ans += (i * (i - 1)) // 2
print(ans)
Problem p02702 (Python) — buggy submission, verdict: Time Limit Exceeded

from collections import defaultdict


def solve(input_s: str):
    ans = 0
    n = len(input_s)
    mod = 2019
    dp = defaultdict(int)
    cur = 0
    for i in range(n, -1, -1):
        if i == n:
            dp[0] += 1
        else:
            # cur += ((10 ** (n - i - 1)) * int(input_s[i])) % mod これはnがでかくなるとめちゃめちゃ遅い
            cur += pow(10, n - i - 1, 2019) * int(input_s[i]) % mod
            cur %= mod
            dp[cur] += 1
    for v in dp.values():
        if v > 1:
            ans += v * (v - 1) // 2
    return ans


if __name__ == "__main__":
    S = input()
    print(solve(S))


Error: TLE

Fixed submission:

from collections import defaultdict

S = input()
ans = 0
N = len(S)
mod = 2019
dp = defaultdict(int)
cur = 0
for i in range(N, -1, -1):
    if i == N:
        dp[0] += 1
    else:
        cur += ((10 ** (N - i - 1)) * int(S[i])) % mod
        cur %= mod
        dp[cur] += 1
for v in dp.values():
    if v > 1:
        ans += v * (v - 1) // 2
print(ans)
Problem p02702 (Python) — buggy submission, verdict: Time Limit Exceeded

S = input()

ans = 0
lists = [0 for i in range(2019)]
lists[0] += 1

num = 0
for i in range(len(S) - 1, -1, -1):
    num += pow(10, len(S) - 1 - i, 2019) * int(S[i])
    lists[num % 2019] += 1

for i in range(2019):
    if lists[i] >= 2:
        ans += (lists[i] * (lists[i] - 1)) // 2

print(ans)


Error: TLE

Fixed submission:

S = input()

ans = 0
lists = [0 for i in range(2019)]
lists[0] += 1

tmp = 1
num = 0
for i in range(len(S) - 1, -1, -1):
    num += tmp * int(S[i])
    lists[num % 2019] += 1
    tmp *= 10

for i in range(2019):
    if lists[i] >= 2:
        ans += (lists[i] * (lists[i] - 1)) // 2

print(ans)
Problem p02702 (Python) — buggy submission, verdict: Time Limit Exceeded

S = str(input())
# 余りのリスト
counts = [0] * 2019
n, d = 0, 1

for s in S[::-1]:
    n += int(s) * d
    d *= 10
    n %= 2019
    d %= 2019
    counts[n] += 1
ans = counts[0]
for count in counts:
    ans += count * (count - 1) // 2
print(ans)


Error: TLE

Fixed submission:

S = str(input())
# 余りのリスト
counts = [0] * 2019
n, d = 0, 1

for s in S[::-1]:
    n += int(s) * d
    d *= 10
    n %= 2019
    counts[n] += 1
ans = counts[0]
for count in counts:
    ans += count * (count - 1) // 2
print(ans)
Problem p02702 (Python) — buggy submission, verdict: Time Limit Exceeded

s = input()[::-1]

n = len(s)
cnt = [0] * 2019
cnt[0] = 1
total = 0
ans = 0
x = 1

for i in range(n):
    total += int(s[i]) * x
    total %= 2019
    x *= 10
    x %= 2019
    ans += cnt[total]
    cnt[total] += 1

print(ans)


Error: TLE

Fixed submission:

s = input()[::-1]

cnt = [0] * 2019
cnt[0] = 1
total = 0
ans = 0
x = 1

for i in s:
    total += int(i) * x
    total %= 2019
    x *= 10 % 2019
    ans += cnt[total]
    cnt[total] += 1

print(ans)
Problem p02702 (Python) — buggy submission, verdict: Time Limit Exceeded

from collections import Counter

S = input()

ans = 0
mod_list = [0] * len(S)
mod_list[-1] = int(S[-1])
m = 1
for i in range(2, len(S) + 1):
    m = (10 * m) % 2019
    mod_list[-i] = (mod_list[-i + 1] + (int(S[-i]) * m)) % 2019

mod_counter = Counter(mod_list)
mod_counter[0] += 1

ans = 0
for _, v in mod_counter.items():
    ans += v * (v - 1) // 2

print(ans)


Error: TLE

Fixed submission:

S = input()

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

print(ans)
Problem p02702 (Python) — buggy submission, verdict: Time Limit Exceeded

S = input()
N = len(S)

map_ = [-1] * 2019
map_[0] = 0
ans = 0
now = 0
for i in range(0, N):
    now = (now + int(S[N - i - 1]) * pow(10, i, 2019)) % 2019
    map_[now] += 1
    ans += map_[now]

print(ans)


Error: TLE

Fixed submission:

S = input()
N = len(S)

map_ = [-1] * 2019
map_[0] = 0
ans = 0
now = 0
for i in range(0, N + 1):
    now += (int(S[N - i - 1]) * (10**i)) % 2019
    map_[now % 2019] += 1
    ans += map_[now % 2019]

print(ans)
Problem p02702 (Python) — buggy submission, verdict: Time Limit Exceeded

s = input()
s = s[::-1]
n = len(s)
m = [0] * 2019
m[0] = 1
a = 0
d = 1
for i in range(n):
    a = (a + int(s[i]) * d) % 2019
    d = (d * 10) % 2019
    m[a] += 1
print(sum([int(x * (x - 1) / 2) for x in m if x > 1]))


Error: TLE

Fixed submission:

s = input()
m = [0 for _ in range(2019)]
m[0] = 1
for i in range(len(s)):
    t = int(s[i:])
    m[t % 2019] += 1
print(sum([int(x * (x - 1) / 2) for x in m if x > 1]))
Problem p02702 (Python) — buggy submission, verdict: Time Limit Exceeded

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)


Error: TLE

Fixed submission:

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)
Problem p02702 (Python) — buggy submission, verdict: Time Limit Exceeded

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

t = 1
r = 0
d = [0] * 2019
d[0] += 1
for n in reversed(S):
    r = (r + n * t) % 2019
    t = t * 10 % 2019
    d[r] += 1

ans = 0
for m in d:
    ans += m * (m - 1) // 2

print(ans)


Error: TLE

Fixed submission:

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

nums = [0] * (len(S) + 1)
pow10 = 1
counter = dict({0: 1})
for i in range(1, len(S) + 1):
    nums[i] = (nums[i - 1] + pow10 * S[-i]) % 2019
    if nums[i] not in counter:
        counter[nums[i]] = 1
    else:
        counter[nums[i]] += 1

    pow10 *= 10

ans = 0
for count in counter.values():
    if count >= 2:
        ans += count * (count - 1) // 2

print(ans)
Problem p02702 (Python) — buggy submission, verdict: Time Limit Exceeded

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

r = 0
t = 1
d = [0] * 2019
d[0] = 1
for i in range(1, len(S) + 1):
    r = (r + t * S[-i]) % 2019
    d[r] += 1
    t = t * 10 % 2019

ans = 0
for m in d:
    if m >= 2:
        ans += m * (m - 1) // 2

print(ans)


Error: TLE

Fixed submission:

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

r = 0
pow10 = 1
counter = [0] * 2019
counter[0] = 1
for i in range(1, len(S) + 1):
    r = (r + pow10 * S[-i]) % 2019
    counter[r] += 1
    pow10 *= 10

ans = 0
for count in counter:
    if count >= 2:
        ans += count * (count - 1) // 2

print(ans)