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

import sys
import numpy as np

input = sys.stdin.buffer.readline
N = int(input())
A = np.array(list(map(int, input().split())))

MOD = 10**9 + 7

answer = 0
for n in range(63):
    B = (A >> n) & 1
    x = np.count_nonzero(B)
    y = N - x
    x *= y
    for _ in range(n):
        x = x * 2 % MOD
    answer += x
answer %= MOD
print(answer)


Error: TLE

Fixed submission:

N = int(input())
A = list(map(int, input().split()))

mod = 10**9 + 7

ans = 0
for i in range(0, N - 1):
    for j in range(i + 1, N):
        # print(bin(A[i] ^ A[j]))
        # ans += int(bin(A[i] ^ A[j]), 0)
        ans += A[i] ^ A[j]
        ans %= mod
print(ans)
Problem p02838 (Python) — buggy submission, verdict: Time Limit Exceeded

MOD = 10**9 + 7
N = int(input())
A = list(map(int, input().split()))

ans = 0
m = 1
for i in range(60):
    bits = sum((a & m for a in A))
    bits //= m
    ans += bits * (N - bits) * m
    ans %= MOD
    m <<= 1
print(ans % MOD)


Error: TLE

Fixed submission:

MOD = 10**9 + 7
N = int(input())
A = list(map(int, input().split()))

ans = 0
for i in range(N - 1):
    for j in range(i + 1, N):
        ans += A[i] ^ A[j]
        ans %= MOD
print(ans)
Problem p02839 (Python) — buggy submission, verdict: Time Limit Exceeded

def main():
    # import sys
    # readline = sys.stdin.readline
    # readlines = sys.stdin.readlines
    H, W = map(int, input().split())
    A = [list(map(int, input().split())) for _ in range(H)]
    B = [list(map(int, input().split())) for _ in range(H)]
    M = 0
    grid = [[0] * W for _ in range(H)]
    for i in range(H):
        for j in range(W):
            m = abs(A[i][j] - B[i][j])
            grid[i][j] = m
            M = max(M, m)

    L = (H + W) * M
    dp = [[[0] * (L + 1) for _ in range(W)] for _ in range(H)]
    dp[0][0][grid[0][0]] = 1

    for i in range(H):
        for j in range(W):
            for k in range(L + 1):
                if dp[i][j][k] == 1:
                    if i < H - 1:
                        nk = grid[i + 1][j]
                        dp[i + 1][j][k + nk] |= 1
                        dp[i + 1][j][abs(k - nk)] |= 1
                    if j < W - 1:
                        nk = grid[i][j + 1]
                        dp[i][j + 1][k + nk] |= 1
                        dp[i][j + 1][abs(k - nk)] |= 1

    for k in range(L + 1):
        if dp[-1][-1][k] == 1:
            print(k)
            break


if __name__ == "__main__":
    main()


Error: TLE

Fixed submission:

def main():
    # import sys
    # readline = sys.stdin.readline
    # readlines = sys.stdin.readlines
    H, W = map(int, input().split())
    A = [list(map(int, input().split())) for _ in range(H)]
    B = [list(map(int, input().split())) for _ in range(H)]
    grid = [[0] * W for _ in range(H)]
    for i in range(H):
        for j in range(W):
            grid[i][j] = abs(A[i][j] - B[i][j])

    dp = [[set() for _ in range(W)] for _ in range(H)]
    dp[0][0].add(grid[0][0])

    for i in range(H):
        for j in range(W):
            for k in dp[i][j]:
                if i < H - 1:
                    nk = grid[i + 1][j]
                    dp[i + 1][j].add(k + nk)
                    dp[i + 1][j].add(abs(k - nk))
                if j < W - 1:
                    nk = grid[i][j + 1]
                    dp[i][j + 1].add(k + nk)
                    dp[i][j + 1].add(abs(k - nk))

    print(min(dp[-1][-1]))


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

H, W = map(int, input().split())

A = [list(map(int, input().split())) for _ in range(H)]
B = [list(map(int, input().split())) for _ in range(H)]
Diff = [[0] * (W + 1) for _ in range(H + 1)]
M = 0
for i in range(H):
    for j in range(W):
        m = abs(A[i][j] - B[i][j])
        Diff[i][j] = m
        M += m

dp = [[0] * (W + 1) for _ in range(H + 1)]
dp[0][0] |= 1 << Diff[i][j]

for i in range(0, H):
    for j in range(0, W):
        here = dp[i][j]
        s = Diff[i][j]
        ni = 0
        nj = 0
        ni |= here << s
        nj |= here << s
        ni |= here >> s
        nj |= here >> s
        ex = 0
        for k in range(1, s + 1):
            if (here >> k) & 1:
                ex |= 1 << (s - k)
        ni |= ex
        nj |= ex
        dp[i + 1][j] |= ni
        dp[i][j + 1] |= nj

a = dp[H - 1][W - 1]
ans = 0
for k in range(M + 1):
    if (a >> k) & 1:
        ans = k
        break

print(k)


Error: TLE

Fixed submission:

H, W = map(int, input().split())

A = [list(map(int, input().split())) for _ in range(H)]
B = [list(map(int, input().split())) for _ in range(H)]
Diff = [[False] * (W + 1) for _ in range(H + 1)]
M = 0
for i in range(H):
    for j in range(W):
        m = abs(A[i][j] - B[i][j])
        Diff[i][j] = m
        M += m

dp = [[[0] * (M + 1) for _ in range(W + 1)] for _ in range(H + 1)]
dp[0][0][Diff[0][0]] = 1

for i in range(0, H):
    for j in range(0, W):
        for k in range(M + 1):
            if dp[i][j][k]:
                dp[i + 1][j][k + Diff[i + 1][j]] |= True
                dp[i + 1][j][abs(k - Diff[i + 1][j])] |= True
                dp[i][j + 1][k + Diff[i][j + 1]] |= True
                dp[i][j + 1][abs(k - Diff[i][j + 1])] |= True

for k in range(M + 1):
    if dp[H - 1][W - 1][k] == 1:
        ans = k
        break

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

H, W = map(int, input().split())
A = [list(map(int, input().split())) for i in range(H)]
B = [list(map(int, input().split())) for i in range(H)]

dp = [[0] * W for i in range(H)]
dp[0][0] = 1 << (abs(A[0][0] - B[0][0]) + 6400)
for i in range(H):
    for j in range(W):
        d = abs(A[i][j] - B[i][j])
        if i > 0:
            dp[i][j] |= dp[i - 1][j] << d | dp[i - 1][j] >> d
        if j > 0:
            dp[i][j] |= dp[i][j - 1] << d | dp[i][j - 1] >> d
ans = 0
res = dp[H - 1][W - 1] >> 6400
while res & 1 == 0:
    res >>= 1
    ans += 1
print(ans)


Error: TLE

Fixed submission:

from collections import defaultdict

H, W = map(int, input().split())
A = [list(map(int, input().split())) for i in range(H)]
B = [list(map(int, input().split())) for i in range(H)]

dp = defaultdict(set)
dp[(0, 0)].add(abs(A[0][0] - B[0][0]))
for i in range(H):
    for j in range(W):
        d = abs(A[i][j] - B[i][j])
        if i > 0:
            for k in dp[(i - 1, j)]:
                dp[(i, j)].add(abs(k + d))
                dp[(i, j)].add(abs(k - d))
        if j > 0:
            for k in dp[(i, j - 1)]:
                dp[(i, j)].add(abs(k + d))
                dp[(i, j)].add(abs(k - d))
print(min(dp[(H - 1, W - 1)]))
Problem p02839 (Python) — buggy submission, verdict: Time Limit Exceeded

import numpy as np

h, w = list(map(int, input().split()))

aaa = [list(map(int, input().split())) for _ in range(h)]
bbb = [list(map(int, input().split())) for _ in range(h)]
aaa = np.array(aaa, dtype=np.int8)
bbb = np.array(bbb, dtype=np.int8)
diff = abs(aaa - bbb)


def merge_new_can(d, can, prev_can):
    if d == 0:
        can |= prev_can
    else:
        can[d:] |= prev_can[:-d]
        can[:-d] |= prev_can[d:]


prev_row = [None] * w

can = np.zeros(25601, dtype=np.int8)
can[diff[0][0] + 12800] = 1
prev_row[0] = can

for j in range(1, w):
    d = diff[0][j]
    can = np.zeros(25601, dtype=np.int8)
    prev_can = prev_row[j - 1]
    merge_new_can(d, can, prev_can)
    prev_row[j] = can

for i in range(1, h):
    curr_row = [None] * w

    d = diff[i][0]
    prev_can = prev_row[0]
    can = np.zeros(25601, dtype=np.int8)
    merge_new_can(d, can, prev_can)
    curr_row[0] = can

    for j in range(1, w):
        d = diff[i][j]
        can = np.zeros(25601, dtype=np.int8)
        prev_can1 = prev_row[j]
        prev_can2 = curr_row[j - 1]
        merge_new_can(d, can, prev_can1)
        merge_new_can(d, can, prev_can2)
        curr_row[j] = can

    prev_row = curr_row

goal = prev_row[-1]
goal[12801:] |= goal[:12800][::-1]
goal = goal[12800:]
print(np.argmax(goal))


Error: TLE

Fixed submission:

import numpy as np

h, w = list(map(int, input().split()))

field = [[None] * w for _ in range(h)]
aaa = [list(map(int, input().split())) for _ in range(h)]
bbb = [list(map(int, input().split())) for _ in range(h)]
aaa = np.array(aaa, dtype=np.int64)
bbb = np.array(bbb, dtype=np.int64)
diff = abs(aaa - bbb)

offset = 15000
can = np.zeros(30001, dtype=np.int64)
can[diff[0][0] + offset] = 1
field[0][0] = can


def merge_new_can(d, can, prev_can):
    if d == 0:
        can |= prev_can
    else:
        can[d:] |= prev_can[:-d]
        can[:-d] |= prev_can[d:]


for j in range(1, w):
    d = diff[0][j]
    prev_can = field[0][j - 1]
    can = np.zeros(30001, dtype=np.int64)
    merge_new_can(d, can, prev_can)
    field[0][j] = can

for i in range(1, h):
    d = diff[i][0]
    prev_can = field[i - 1][0]
    can = np.zeros(30001, dtype=np.int64)
    merge_new_can(d, can, prev_can)
    field[i][0] = can

for i in range(1, h):
    for j in range(1, w):
        d = diff[i][j]
        can = np.zeros(30001, dtype=np.int64)
        prev_can1 = field[i][j - 1]
        prev_can2 = field[i - 1][j]
        merge_new_can(d, can, prev_can1)
        merge_new_can(d, can, prev_can2)
        field[i][j] = can

goal = field[-1][-1]
goal[15001:] |= goal[:15000][::-1]
goal = goal[15000:]
print(np.argmax(goal))
Problem p02840 (Python) — buggy submission, verdict: Runtime Error

from fractions import gcd
from itertools import accumulate

n, x, d = map(int, input().split())

if d == 0:
    if x == 0:
        print(1)
    else:
        print(n + 1)
    exit()
elif d < 0:
    x, d = -x, -d

g = gcd(x, d)
back_k = d // g
offset = x // g

lowers = [0] + list(accumulate(range(n)))
uppers = [0] + list(accumulate(range(n - 1, -1, -1)))
min_memo = [10**18] * min(n + 1, back_k)
max_memo = [-(10**18)] * min(n + 1, back_k)
# print(n, x, d, g, back_k, offset)
# print(lowers)
# print(uppers)

ans = 0
for k in range(n + 1):
    min_acc = lowers[k]
    max_acc = uppers[k]

    if k < back_k:
        min_memo[k] = min_acc - offset
        max_memo[k] = max_acc - offset
        ans += max_acc - min_acc + 1
        # print(k, ans, min_acc, max_acc, max_acc - min_acc + 1)
        continue

    kk = k % back_k
    back_min = min_memo[kk]
    back_max = max_memo[kk]

    if back_max < min_acc or max_acc < back_min:
        min_memo[kk] = min_acc - offset
        max_memo[kk] = max_acc - offset
        ans += max_acc - min_acc + 1
    else:
        current = max_acc - min_acc + 1
        duplicated = min(max_acc, back_max) - max(min_acc, back_min) + 1
        ans += current - duplicated
        min_memo[kk] = min(min_acc, back_min) - offset
        max_memo[kk] = max(max_acc, back_max) - offset

    # print(k, ans, min_acc, max_acc, back_min, back_max)

print(ans)


Error: ImportError: cannot import name 'gcd' from 'fractions' (/usr/lib/python3.10/fractions.py)
Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02840/Python/s943701643.py", line 1, in <module>
    from fractions import gcd
ImportError: cannot import name 'gcd' from 'fractions' (/usr/lib/python3.10/fractions.py)


Fixed submission:

from fractions import gcd
from itertools import accumulate

n, x, d = map(int, input().split())
g = gcd(x, d)
back_k = d // g
offset = x // g

min_memo = [0] + list(accumulate(range(n)))
max_memo = [0] + list(accumulate(range(n - 1, -1, -1)))

ans = 0
for k in range(n + 1):
    min_acc = min_memo[k]
    max_acc = max_memo[k]

    if k < back_k:
        ans += max_acc - min_acc + 1
        continue

    back_max = max_memo[k - back_k] - offset
    ans += max_acc - max(min_acc - 1, back_max)

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

date_entry_1 = input()
M1, D1 = map(int, date_entry_1.split(" "))
date_entry_2 = input()
M2, D2 = map(int, date_entry_2.split(" "))

if D2 == 1:
    print(1)
else:
    print(0)


Error: ValueError: invalid literal for int() with base 10: '11 16'
Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02841/Python/s700613660.py", line 1, in <module>
    M1 = int(input())
ValueError: invalid literal for int() with base 10: '11 16'


Fixed submission:

M1 = int(input())
D1 = int(input())
M2 = int(input())
D2 = int(input())

if D2 == 1:
    print(1)
else:
    print(0)
Problem p02841 (Python) — buggy submission, verdict: Runtime Error

m1, d1 = map(int, input().split())
m2, d2 = map(int, input().split())

if m1 != m2:
    print("1")
else:
    print("0")


Error: 0

Fixed submission:

m1, d1 = map(int, input().split())
m2, d2 = map(int, input().split())

if m1 != m2:
    print("1")
    exit(1)

print("0")
Problem p02842 (Python) — buggy submission, verdict: Runtime Error

N = int(input())
for x in range(1, 50000):
    if int(x * 1.08) == N:
        print(x)
        exit()
print(":(")


Error: 0

Fixed submission:

N = int(input())
for x in range(1, 50_000):
    if int(x * 1.08) == N:
        print(x)
        exit()
print(":(")
Problem p02843 (Python) — buggy submission, verdict: Runtime Error

n = input()
if len(n) >= 3:
    num = int(n[0:-2])
    yen = int(n[-2::])
    if num * 5 >= yen:
        print(1)
    else:
        print(0)
else:
    print(0)


Error: 0

Fixed submission:

n = input()
num = int(n[0:-2])
yen = int(n[-2::])
if num * 5 >= yen:
    print(1)
else:
    print(0)
Problem p02843 (Python) — buggy submission, verdict: Runtime Error

x = int(input())
ans = 0
if x < 100:
    ans = 0
else:
    n = (x // 100) * 5
    k = x % 100
    if k <= n:
        ans = 1
    else:
        ans = 0

print(ans)


Error: 0

Fixed submission:

x = input()
n = int(x[0]) * 5
k = int(x[1:3])
if k <= n:
    print(1)
else:
    print(0)
Problem p02843 (Python) — buggy submission, verdict: Runtime Error

x = input()
if len(x) < 3:
    print(0)
    exit()
k = int(x[0:-2])
x = int(x[-2:])

k -= x / 5
if x % 5 == 0 and k >= 0:
    print(1)
elif k > 0:
    print(1)
else:
    print(0)


Error: 0

Fixed submission:

x = input()
k = int(x[0:-2])
x = int(x[-2:])

k -= x / 5
if x % 5 == 0 and k >= 0:
    print(1)
elif k > 0:
    print(1)
else:
    print(0)
Problem p02843 (Python) — buggy submission, verdict: Runtime Error

X = int(input())

dp = [0] * (100200)
dp[0] = 1

for i in range(X + 1):
    if dp[i] == 1:
        for j in range(6):
            dp[i + 100 + j] = 1
            if i + 100 + j == X:
                print(1)
                exit()
print(0)


Error: 0

Fixed submission:

X = int(input())

dp = [0] * (100000)
dp[0] = 1

for i in range(X + 1):
    if dp[i] == 1:
        for j in range(6):
            dp[i + 100 + j] = 1
            if i + 100 + j == X:
                print(1)
                exit()
print(0)
Problem p02843 (Python) — buggy submission, verdict: Time Limit Exceeded

x = int(input())
dp = [0] * 100010
dp[100] = 1
dp[101] = 1
dp[102] = 1
dp[103] = 1
dp[104] = 1
dp[105] = 1

for i in range(106, 100010):
    if (
        dp[i - 100]
        or dp[i - 101]
        or dp[i - 102]
        or dp[i - 103]
        or dp[i - 104]
        or dp[i - 105]
    ):
        dp[i] = 1

print(1 if dp[x] else 0)


Error: TLE

Fixed submission:

x = int(input())

num = set()
for a in range(20):
    for b in range(20):
        for c in range(20):
            for d in range(20):
                for e in range(20):
                    for f in range(20):
                        num.add(
                            100 * a + 101 * b + 102 * c + 103 * d + 104 * e + 105 * f
                        )

# print(1 if x in num else 0)
if x < 100 or x > 2000:
    print(1)
elif x in num:
    print(1)
else:
    print(0)
Problem p02843 (Python) — buggy submission, verdict: Time Limit Exceeded

# C - 100 to 105
def main():
    X = int(input())
    check = lambda x: x * 100 <= X <= x * 105
    is_possible = any(check(i) for i in range(X))
    print(int(is_possible))


if __name__ == "__main__":
    main()


Error: TLE

Fixed submission:

# C - 100 to 105
from collections import defaultdict


def main():
    X = int(input())
    items = tuple(range(100, 106))
    dp = defaultdict(bool)
    dp[0] = True
    while max(dp.keys()) < X:
        for possible_price in tuple(dp.keys()):
            for item in items:
                dp[possible_price + item] = True
    print(int(X in dp))


if __name__ == "__main__":
    main()
Problem p02843 (Python) — buggy submission, verdict: Runtime Error

from itertools import combinations_with_replacement

X = list(input())
tmp = X
num = int("".join(tmp))

if num < 100:
    print(0)
    exit()

X.pop()
X.pop()
X = int("".join(X))

food = [100, 101, 102, 103, 104, 105]

for i in combinations_with_replacement(food, X):
    if sum(i) == num:
        print(1)
        exit()

print(0)


Error: 0

Fixed submission:

from itertools import combinations_with_replacement

X = list(input())
tmp = X
num = int("".join(tmp))

X.pop()
X.pop()
X = int("".join(X))

food = [100, 101, 102, 103, 104, 105, 106]

for i in combinations_with_replacement(food, X):
    if sum(i) == num:
        print(1)
        exit()

print(0)
Problem p02844 (Python) — buggy submission, verdict: Time Limit Exceeded

from collections import defaultdict

n = int(input())
S = input()

d = defaultdict(list)
for i, s in enumerate(S):
    d[s].append(i)

ans = 0
for p in range(1000):
    p1, p2, p3 = str(p).zfill(3)

    i0 = -1
    for i in d[p1]:
        if i > i0:
            i1 = i
            break
    else:
        continue

    for i in d[p2]:
        if i > i1:
            i2 = i
            break
    else:
        continue

    for i in d[p3]:
        if i > i2:
            ans += 1
            break


print(ans)


Error: TLE

Fixed submission:

n = int(input())
S = input()

ans = 0
for i in range(1000):
    pin = str(i).zfill(3)

    ind = 0
    for s in S:
        if pin[ind] == s:
            ind += 1
        if ind >= 3:
            ans += 1
            break

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

def password():
    for i in range(10):
        for j in range(10):
            for k in range(10):
                yield [str(i), str(j), str(k)]


n = int(input())
s = input()

ans = 0
for perm in password():
    if perm[0] in s:
        a = s.find(perm[0])
        if perm[1] in s[a + 1 :]:
            b = s[a + 1 :].find(perm[1])
            if perm[2] in s[a + 1 :][b + 1 :]:
                ans += 1

print(ans)


Error: TLE

Fixed submission:

def password():
    for i in range(10):
        for j in range(10):
            for k in range(10):
                yield [str(i), str(j), str(k)]


n = int(input())
s = input()

ans = 0
for perm in password():
    j = 0
    for i in range(n):
        if perm[j] == s[i]:
            j += 1
            if j >= len(perm):
                break
    if j >= len(perm):
        ans += 1

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

from itertools import product

n = int(input())
s = input()
nums = [str(i) for i in range(10)]

cnt = 0
for num in product(nums, repeat=3):
    i = 0
    for e in s:
        if num[i] == e:
            i += 1
        if i == 3:
            break

    if i == 3:
        cnt += 1

print(cnt)


Error: TLE

Fixed submission:

from itertools import product

n = int(input())
s = input()

cnt = 0
for num in product(range(10), repeat=3):
    i = 0
    for e in s:
        if str(num[i]) == e:
            i += 1
        if i == 3:
            break

    if i == 3:
        cnt += 1

print(cnt)
Problem p02844 (Python) — buggy submission, verdict: Time Limit Exceeded

N = int(input())
S = input()
ans = 0
d = [set() for _ in range(3)]
for i in range(N - 2):
    if S[i] in d[0]:
        continue
    d[0].add(S[i])
    for j in range(i + 1, N - 1):
        if S[i] + S[j] in d[1]:
            continue
        d[1].add(S[i] + S[j])
        for k in range(j + 1, N):
            if S[i] + S[j] + S[k] in d[2]:
                continue
            d[2].add(S[i] + S[j] + S[k])
            ans += 1
print(ans)


Error: TLE

Fixed submission:

N = int(input())
S = input()
ans = 0
for i in range(1000):
    T = str(i).zfill(3)
    j = 0
    for c in S:
        if c == T[j]:
            j += 1
            if j > 2:
                ans += 1
                break
print(ans)
Problem p02844 (Python) — buggy submission, verdict: Time Limit Exceeded

import itertools
import collections

n = int(input())
s = input()

password_l = [str(i).zfill(3) for i in range(1000)]

c = 0
for password in password_l:
    idx = 0
    ok = 0
    while idx != n:
        if s[idx] == password[ok]:
            ok += 1

        if ok == 3:
            c += 1
            break

        idx += 1

print(c)


Error: TLE

Fixed submission:

import itertools
import collections

n = int(input())
s = input()

comb = list(itertools.combinations(list(s), 3))

print(len(collections.Counter(comb)))
Problem p02844 (Python) — buggy submission, verdict: Time Limit Exceeded

n = int(input())
s = input()

pins = [str(i).zfill(3) for i in range(1000)]

ans = 0
for pin in pins:
    index = -1
    for c in pin:
        index = s.find(c, index + 1)
        if index < 0:
            break
    if index > 0:
        ans += 1

print(ans)


Error: TLE

Fixed submission:

n = int(input())
s = input()

pin = [str(i).zfill(3) for i in range(1000)]

ans = 0
for i in pin:
    c = 0
    for j in s:
        if j == i[c]:
            c += 1
        if c == 3:
            ans += 1
            break
print(ans)
Problem p02844 (Python) — buggy submission, verdict: Time Limit Exceeded

n = int(input())
s = input()
cnt = 0

for i in range(1000):
    p = str(i).zfill(3)
    p1 = s.find(p[0])
    if p1 == -1:
        continue
    p2 = s.find(p[1], p1 + 1)
    if p2 == -1:
        continue
    p3 = s.find(p[2], p2 + 1)
    if p3 == -1:
        continue
    cnt += 1
print(cnt)


Error: TLE

Fixed submission:

n = int(input())
s = input()
pattern = [tuple(s[:3])]
for c in s[3:]:
    for i in range(len(pattern)):
        p1, p2, p3 = pattern[i]
        if (p1, p2, c) not in pattern:
            pattern.append((p1, p2, c))
        if (p1, p3, c) not in pattern:
            pattern.append((p1, p3, c))
        if (p2, p3, c) not in pattern:
            pattern.append((p2, p3, c))
print(len(pattern))
Problem p02844 (Python) — buggy submission, verdict: Time Limit Exceeded

n = int(input())
s = input()

ans = 0
# 000 ～　999 がsから作成できるか確認していく
for i in range(1000):
    p = str(i).zfill(3)

    s_index_100 = s.find(p[2])
    if s_index_100 == -1:
        continue

    s_index_10 = s.find(p[1], s_index_100 + 1)
    if s_index_10 == -1:
        continue

    s_index_1 = s.find(p[0], s_index_10 + 1)
    if s_index_1 == -1:
        continue
    ans += 1

print(ans)


Error: TLE

Fixed submission:

n = int(input())
s = input()

ans = 0
for i in range(1000):
    p = str(i).zfill(3)
    p_index = 0
    for j in range(n):
        if p[p_index] == s[j]:
            p_index += 1
            if p_index == 3:
                break
    if p_index == 3:
        ans += 1

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

n, s = int(input()), str(input())

num1, num2, num3 = set(), set(), set()
for i in range(n):
    if s[i] in num1:
        continue
    num1.add(s[i])
    for j in range(i + 1, n):
        tmp2 = s[i] + s[j]
        if tmp2 in num2:
            continue
        num2.add(tmp2)
        for k in range(j + 1, n):
            num3.add(tmp2 + s[k])
print(len(num3))


Error: TLE

Fixed submission:

n, s = int(input()), str(input())

num1, num2, num3 = set([]), set([]), set([])
for i in range(n):
    if s[i] in num1:
        continue
    num1.add(s[i])
    for j in range(i + 1, n):
        for k in range(j + 1, n):
            num3.add(s[i] + s[j] + s[k])
print(len(num3))