alexjercan/bugnet huggingface dataset:
Problem p03998 (Python) — buggy submission, verdict: Runtime Error

SA = list(input())
SB = list(input())
SC = list(input())

turn = SA.pop(0)
while True:
    if turn == "a":
        try:
            turn = SA.pop(0)
        except:
            print("A")
            break
    elif turn == "b":
        try:
            turn = SB.pop(0)
        except:
            print("B")
            break
    elif turn == "c":
        try:
            turn = SC.pop(0)
        except:
            print("C")
            break


Error: 0

Fixed submission:

SA = list(input())
SB = list(input())
SC = list(input())

turn = SA.pop(0)
while True:
    if turn == "a":
        turn = SA.pop(0)
    elif turn == "b":
        turn = SB.pop(0)
    elif turn == "c":
        turn = SC.pop(0)

    if turn == "a":
        if not SA:
            print("A")
            break
    elif turn == "b":
        if not SB:
            print("B")
            break
    elif turn == "c":
        if not SC:
            print("C")
            break
Problem p03998 (Python) — buggy submission, verdict: Runtime Error

Sa = list(input())
Sb = list(input())
Sc = list(input())
c = 0
for _ in range(10**9):
    if c == "a" and not Sa:
        print("A")
        break
    if c == "b" and not Sb:
        print("B")
        break
    if c == "c" and not Sc:
        print("C")
        break
    if _ == 0:
        c = Sa[0]
        del Sa[0]
    elif c == "a":
        c = Sa[0]
        del Sa[0]
    elif c == "b":
        c = Sb[0]
        del Sb[0]
    else:
        c = Sc[0]
        del Sc[0]


Error: 0

Fixed submission:

Sa = list(input())
Sb = list(input())
Sc = list(input())
for _ in range(10**8):
    if _ == 0:
        s = Sa[0]
        if s == "a":
            s = Sa[1]
            del Sa[0]
        elif s == "b":
            s = Sb[1]
            del Sb[0]
        else:
            s = Sc[1]
            del Sc[0]
    elif len(Sa) == 1 and s == "a":
        print("A")
        break
    elif len(Sb) == 1 and s == "b":
        print("B")
        break
    elif len(Sc) == 1 and s == "c":
        print("C")
        break
    else:
        if s == "a":
            s = Sa[1]
            del Sa[0]
        elif s == "b":
            s = Sb[1]
            del Sb[0]
        else:
            s = Sc[1]
            del Sc[0]
Problem p03998 (Python) — buggy submission, verdict: Runtime Error

def resolve():
    a = list(input())
    b = list(input())
    c = list(input())
    d = {"a": a, "b": b, "c": c}
    now = "a"
    while d[now]:
        now = d[now].pop(0)
    print(now.upper())


if __name__ == "__main__":
    resolve()


Error: 0

Fixed submission:

def resolve():
    a = input()
    b = input()
    c = input()
    d = {"a": a, "b": b, "c": c}
    now = d["a"][0]
    d["a"] = d["a"][1:]
    next = d[now][0]
    while True:
        if len(d[next]) == 0:
            print(next.upper())
            return 0
        next = d[next][0]
        d[next] = d[next][1:]


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

a = list(input())
b = list(input())
c = list(input())

n = a[0]
if len(a) < 1:
    del a[0]

while True:
    if n == "a":
        n = a[0]
        del a[0]
        if len(a) == 0:
            print("A")
            break
    elif n == "b":
        n = b[0]
        del b[0]
        if len(b) == 0:
            print("B")
            break
    elif n == "c":
        n = c[0]
        del c[0]
        if len(c) == 0:
            print("C")
            break


Error: WA

Fixed submission:

a = list(input())
b = list(input())
c = list(input())

n = a[0]
del a[0]
if len(a) == 0:
    print("A")
while True:
    if n == "a":
        n = a[0]
        del a[0]
        if len(a) == 0:
            print("A")
            break
    elif n == "b":
        n = b[0]
        del b[0]
        if len(b) == 0:
            print("B")
            break
    elif n == "c":
        n = c[0]
        del c[0]
        if len(c) == 0:
            print("C")
            break
Problem p04000 (Python) — buggy submission, verdict: Runtime Error

from collections import defaultdict

h, w, n = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(n)]

d = defaultdict(int)

for a, b in ab:
    for hi in range(a - 2, a + 1):
        for wi in range(b - 2, b + 1):
            if 1 <= hi <= h - 2 and 1 <= wi <= w - 2:
                d[(hi, wi)] += 1

cnt = [0] * 10
for v in d.values():
    cnt[v] += 1

cnt[0] = (h - 2) * (w - 2) - sum(cnt[1:])
print(*cnt, sep="\\n")


Error: WA

Fixed submission:

from collections import defaultdict

h, w, n = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(n)]

d = defaultdict(int)

for a, b in ab:
    for hi in range(a - 2, a + 1):
        for wi in range(b - 2, b + 1):
            if 1 <= hi <= h - 2 and 1 <= wi <= w - 2:
                d[(hi, wi)] += 1

cnt = [0] * 9
for v in d.values():
    cnt[v] += 1

cnt[0] = (h - 2) * (w - 2) - sum(cnt[1:])
print(*cnt, sep="\\n")
Problem p04001 (Python) — buggy submission, verdict: Runtime Error

import sys


def input():
    return sys.stdin.readline().strip()


s = input()
n = len(s)

total_sum = 0
# +を挿入する事象は
# sが2文字なら2通り，3文字なら4通り，n文字なら2^(n-1)通りある
for i in range(2 ** (n - 1)):
    # iは各桁に+を挿入するかどうかを決定する
    # 例えば s=12345, i=0b0111, ss=1+2+3+45
    now_sum = 0
    reg = int(s[0])
    for j in range(n - 1):
        if i & 1 << j:
            now_sum += reg
            reg = 0
        reg *= 10
        reg += int(s[j + 1])
    now_sum += reg

    total_sum += now_sum

print(total_sum)


Error: ValueError: invalid literal for int() with base 10: '\\n'
Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p04001/Python/s281925188.py", line 21, in <module>
    reg += int(s[j + 1])
ValueError: invalid literal for int() with base 10: '\\n'


Fixed submission:

import sys

input = sys.stdin.readline

s = input()
n = len(s)

total_sum = 0
# +を挿入する事象は
# sが2文字なら2通り，3文字なら4通り，n文字なら2^(n-1)通りある
for i in range(2 ** (n - 1)):
    # iは各桁に+を挿入するかどうかを決定する
    # 例えば s=12345, i=0b0111, ss=1+2+3+45
    now_sum = 0
    reg = int(s[0])
    for j in range(n - 1):
        if i & 1 << j:
            now_sum += reg
            reg = 0
        reg *= 10
        reg += int(s[j + 1])
    now_sum += reg

    total_sum += now_sum

print(total_sum)
Problem p04001 (Python) — buggy submission, verdict: Runtime Error

def dfs(formula, index):
    if index + 1 == max_index:
        return sum(list(map(int, formula.split("+"))))

    else:
        return dfs(formula + "+" + S[index + 1], index + 1) + dfs(
            formula + S[index + 1], index + 1
        )


S = input()
max_index = len(S)

print(dfs(S[0], 0))


Error: TypeError: 'int' object is not iterable
Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p04001/Python/s203029944.py", line 11, in <module>
    print(dfs(0, s[0]))
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p04001/Python/s203029944.py", line 5, in dfs
    return sum(dfs(i + 1, formula + "+" + s[i + 1]), dfs(i + 1, formula + s[i + 1]))
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p04001/Python/s203029944.py", line 5, in dfs
    return sum(dfs(i + 1, formula + "+" + s[i + 1]), dfs(i + 1, formula + s[i + 1]))
TypeError: 'int' object is not iterable


Fixed submission:

def dfs(i, formula):
    if i + 1 == num_digits:
        return sum(list(map(int, formula.split("+"))))

    return sum(dfs(i + 1, formula + "+" + s[i + 1]), dfs(i + 1, formula + s[i + 1]))


s = input()
num_digits = len(s)

print(dfs(0, s[0]))
Problem p04003 (Python) — buggy submission, verdict: Runtime Error

import sys
from collections import deque, defaultdict


def bfs01(s, t, links):
    q = deque([(0, s, -1)])  # cost, station, last-company
    visited = set()
    while q:
        d, v, e = q.popleft()
        if v == t:
            return d
        if (v, e) in visited:
            continue
        visited.add((v, e))
        if e == 0:
            for c, us in links[v].items():
                for u in us:
                    if (u, c) in visited:
                        continue
                    q.append((d + 1, u, c))
        else:
            for u in links[v][e]:
                if (u, e) in visited:
                    continue
                q.appendleft((d, u, e))
            if (v, 0) not in visited:
                q.appendleft((d, v, 0))

    return -1


readline = sys.stdin.buffer.readline
read = sys.stdin.buffer.read

n, m = map(int, readline().split())
links = [defaultdict(set) for _ in range(n)]
pqc = list(map(int, read().split()))
for p, q, c in zip(pqc[0::3], pqc[1::3], pqc[2::3]):
    p -= 1
    q -= 1
    links[p][c].add(q)
    links[q][c].add(p)

print(bfs01(0, n - 1, links))


Error: TypeError: cannot unpack non-iterable int object
Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p04003/Python/s188482754.py", line 44, in <module>
    print(bfs01(0, n - 1, links))
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p04003/Python/s188482754.py", line 16, in bfs01
    for c, us in links[v]:
TypeError: cannot unpack non-iterable int object


Fixed submission:

import sys
from collections import deque, defaultdict


def bfs01(s, t, links):
    q = deque([(0, s, -1)])  # cost, station, last-company
    visited = set()
    while q:
        d, v, e = q.popleft()
        if v == t:
            return d
        if (v, e) in visited:
            continue
        visited.add((v, e))
        if e == 0:
            for c, us in links[v]:
                for u in us:
                    if (u, c) in visited:
                        continue
                    q.append((d + 1, u, c))
        else:
            for u in links[v][e]:
                if (u, e) in visited:
                    continue
                q.appendleft((d, u, e))
            if (v, 0) not in visited:
                q.appendleft((d, v, 0))

    return -1


readline = sys.stdin.buffer.readline
read = sys.stdin.buffer.read

n, m = map(int, readline().split())
links = [defaultdict(set) for _ in range(n)]
pqc = list(map(int, read().split()))
for p, q, c in zip(pqc[0::3], pqc[1::3], pqc[2::3]):
    p -= 1
    q -= 1
    links[p][c].add(q)
    links[q][c].add(p)

print(bfs01(0, n - 1, links))
Problem p04005 (Python) — buggy submission, verdict: Runtime Error

A, B, C = map(int, input().split())

if any(i % 2 == 0 for i in [A, B, C]):
    print(0)
else:
    print(min(A * B, B * C, C * A))


Error: 0

Fixed submission:

A, B, C = map(int, input().split())

if any(i % 2 == 0 for i in [A, B, C]):
    print(0)
else:
    AB = A * B
    lis = [AB] * C
    mid = len(lis) // 2
    print(sum(lis[mid:]) - sum(lis[0:mid]))
Problem p04011 (Python) — buggy submission, verdict: Runtime Error

# x, y = [int(input()) for i in range(2)]
n, k, x, y = [int(input()) for i in range(4)]

if n > k:
    print(int(k * x + y * (n - k)))
else:
    print(int(n * x))


Error: ValueError: not enough values to unpack (expected 4, got 1)
Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p04011/Python/s781359504.py", line 1, in <module>
    n, k, x, y = map(int, input().split())
ValueError: not enough values to unpack (expected 4, got 1)


Fixed submission:

n, k, x, y = map(int, input().split())

print(int(k * x + y * (n - k)))
Problem p04011 (Python) — buggy submission, verdict: Runtime Error

n = int(input())
k = int(input())
x = int(input())
y = int(input())

if n > k:
    print(x * k + (n - k) * y)
else:
    print(n * x)


Error: ValueError: not enough values to unpack (expected 4, got 1)
Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p04011/Python/s526628482.py", line 1, in <module>
    N, K, X, Y = map(int, input().split())
ValueError: not enough values to unpack (expected 4, got 1)


Fixed submission:

N, K, X, Y = map(int, input().split())


print(abs(X * K + (N - K) * Y))
Problem p04011 (Python) — buggy submission, verdict: Runtime Error

# abc044_a
# https://atcoder.jp/contests/abc044/tasks/abc044_a

# 最初のK泊までは、X円
# K＋1泊目以降は、Y円

# N泊連続で宿泊した際の宿泊費

# 入力
n = int(input())
k = int(input())
x = int(input())
y = int(input())

# 処理

# x円とy円
if n > k:
    answer = k * x + (n - k) * y
# x円のみ
else:
    answer = n * x

print(answer)


Error: ValueError: not enough values to unpack (expected 4, got 1)
Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p04011/Python/s608595389.py", line 10, in <module>
    n, k, x, y = map(int, input().split())
ValueError: not enough values to unpack (expected 4, got 1)


Fixed submission:

# abc044_a
# https://atcoder.jp/contests/abc044/tasks/abc044_a

# 最初のK泊までは、X円
# K＋1泊目以降は、Y円

# N泊連続で宿泊した際の宿泊費

# 入力
n, k, x, y = map(int, input().split())

# 処理

# x円とy円
if n > k:
    answer = k * x + (n - k) * y
# x円のみ
else:
    answer = n * x

print(answer)
Problem p04012 (Python) — buggy submission, verdict: Runtime Error

li = list(input())
li.sort()
while True:
    if len(li) == 0:
        print("Yes")
        break
    elif not len(li) % 2 == 0:
        print("No")
        break
    elif li[0] == li[1]:
        li.pop(0)
        li.pop(0)
    else:
        print("No")
        break


Error: 0

Fixed submission:

li = list(input())
li.sort()
while True:
    if len(li) == 0:
        print("Yes")
        break
    elif li[0] == li[1]:
        li.pop(0)
        li.pop(0)
    else:
        print("No")
        break
Problem p04012 (Python) — buggy submission, verdict: Runtime Error

W = input()

r = {}
for w in W:
    r.setdefault(w, 0)
    r[w] += 1

print("Yes" if len(list(filter(lambda x: x % 2 == 1, r.values()))) == 0 else "No")


Error: TypeError: not all arguments converted during string formatting
Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p04012/Python/s253647568.py", line 8, in <module>
    print("Yes" if len(list(filter(lambda x: x % 2 == 1, r))) == 0 else "No")
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p04012/Python/s253647568.py", line 8, in <lambda>
    print("Yes" if len(list(filter(lambda x: x % 2 == 1, r))) == 0 else "No")
TypeError: not all arguments converted during string formatting


Fixed submission:

W = input()

r = {}
for w in W:
    r.setdefault(w, 0)
    r[w] += 1

print("Yes" if len(list(filter(lambda x: x % 2 == 1, r))) == 0 else "No")
Problem p04013 (Python) — buggy submission, verdict: Time Limit Exceeded

n, a = map(int, input().split())
x = list(map(int, input().split()))

dp = [[[0] * (50 * (n + 10)) for _ in range(n + 10)] for _ in range(n + 10)]
dp[0][0][0] = 1

for i in range(n):
    for use in range(n):
        for total in range(n * a):
            if dp[i][use][total]:
                dp[i + 1][use][total] += dp[i][use][total]
                dp[i + 1][use + 1][total + x[i]] += dp[i][use][total]

ans = 0
for i in range(1, n + 1):
    ans += dp[n][i][a * i]

print(ans)


Error: TLE

Fixed submission:

n, a = map(int, input().split())
x = list(map(int, input().split()))

dp = [[[0] * (50 * (n + 10)) for _ in range(n + 10)] for _ in range(n + 10)]
dp[0][0][0] = 1

for i in range(n):
    for use in range(n):
        for total in range(n * a):
            dp[i + 1][use][total] += dp[i][use][total]
            dp[i + 1][use + 1][total + x[i]] += dp[i][use][total]

ans = 0
for i in range(1, n + 1):
    ans += dp[n][i][a * i]

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

n, a = [int(i) for i in input().split()]
x = [int(i) for i in input().split()]

nx = n * max(max(x), a)
y = [xi - a for xi in x]
dp = [[0 for j in range(2 * nx + 1)] for i in range(n + 1)]
dp[0][nx] = 1

for i in range(n):
    for j in range(2 * nx + 1):
        if not 0 <= j - y[i] <= 2 * nx:
            dp[i + 1][j] = dp[i][j]
        elif j - y[i] <= 2 * nx:
            dp[i + 1][j] = dp[i][j] + dp[i][j - y[i]]

print(dp[n][nx] - 1)


Error: 0

Fixed submission:

n, a = [int(i) for i in input().split()]
x = [int(i) for i in input().split()]

nx = n * max(max(x), a)
dp = [[[0 for k in range(nx + 1)] for j in range(n + 1)] for i in range(n + 1)]
dp[0][0][0] = 1

for i in range(n):
    for j in range(n):
        for k in range(nx):
            if k + x[j] <= n * a:
                dp[i + 1][j + 1][k + x[i]] += dp[i][j][k]
            dp[i + 1][j][k] += dp[i][j][k]

ans = sum([dp[n][k][k * a] for k in range(1, n + 1)])
print(ans)
Problem p04013 (Python) — buggy submission, verdict: Time Limit Exceeded

def tak_and_cards(N: int, A: int, X: list) -> int:
    #  部分点解法
    # :---------:
    # count = 0
    # for b in range(1, 1 << N):
    #     n, s = 0, 0
    #     for i in range(N):
    #         if (b >> i) & 1 == 0:
    #             continue
    #         n += 1
    #         s += X[i]

    #     if s == n * A:
    #         count += 1
    # return count
    max_X = max(X)
    dp = [[0] * (2 * max_X * N + 1) for _ in range(N + 1)]
    dp[0][max_X * N] = 1

    for k, x in enumerate(X):
        for t in range(2 * max_X * N + 1):
            y = x - A

            dp[k + 1][t] = dp[k][t]
            if 0 <= t - y and t - y <= 2 * max_X * N:
                dp[k + 1][t] += dp[k][t - y]

    return dp[N][max_X * N] - 1


if __name__ == "__main__":
    N, A = map(int, input().split())
    x = [int(s) for s in input().split()]
    ans = tak_and_cards(N, A, x)
    print(ans)


Error: TLE

Fixed submission:

def tak_and_cards(N: int, A: int, x: list) -> int:
    count = 0
    for b in range(1, 1 << N):
        n, s = 0, 0
        for i in range(N):
            if (b >> i) & 1 == 0:
                continue
            n += 1
            s += x[i]

        if s == n * A:
            count += 1
    return count


if __name__ == "__main__":
    N, A = map(int, input().split())
    x = [int(s) for s in input().split()]
    ans = tak_and_cards(N, A, x)
    print(ans)
Problem p04013 (Python) — buggy submission, verdict: Time Limit Exceeded

n, a = map(int, input().split())
x = list(map(int, input().split()))
x_max = max(x)
x_sum = sum(x)
ans = 0
if x_max >= a:
    dp = [[0 for j in range(2 * x_sum + 1)] for i in range(2)]
    dp[0][x_sum] = 1
    for i in range(1, n + 1):
        idx = i % 2
        y = x[i - 1] - a
        for j in range(2 * x_sum + 1):
            if 0 <= j - y <= 2 * x_sum:
                dp[idx][j] = dp[1 - idx][j] + dp[1 - idx][j - y]
            else:
                dp[idx][j] = dp[1 - idx][j]
    ans = dp[n % 2][x_sum] - 1
print(ans)


Error: TLE

Fixed submission:

n, a = map(int, input().split())
x = list(map(int, input().split()))
x_max = max(x)
x_sum = sum(x)
ans = 0
if x_max >= a:
    dp = [[[0 for k in range(x_sum + 1)] for j in range(n + 1)] for i in range(2)]
    dp[0][0][0] = 1
    for i in range(1, n + 1):
        idx = i % 2
        for j in range(n + 1):
            for k in range(x[i - 1]):
                dp[idx][j][k] = dp[1 - idx][j][k]
            if j > 0:
                for k in range(x[i - 1], x_sum + 1):
                    dp[idx][j][k] = dp[1 - idx][j][k] + dp[1 - idx][j - 1][k - x[i - 1]]

    for j in range(1, min(x_sum // a + 1, n + 1)):
        ans += dp[idx][j][j * a]
print(ans)
Problem p04013 (Python) — buggy submission, verdict: Runtime Error

N, A = map(int, input().split())
X = list(map(lambda x: int(x) - A, input().split()))
maxn = max(max(X), A)

dp = [[0 for _ in range(2 * N * maxn + 1)] for _ in range(N + 1)]
dp[0][N * maxn] = 1

for i in range(N):
    for k in range(2 * N * maxn + 1):
        dp[i + 1][k] = dp[i][k]
        if 0 <= k - X[i] <= 2 * N * maxn:
            dp[i + 1][k] += dp[i][k - X[i]]

print(dp[N][N * maxn] - 1)


Error: 0

Fixed submission:

N, A = map(int, input().split())
# X = list(map(lambda x: int(x) - A, input().split()))
X = list(map(int, input().split()))

dp = [[[0 for _ in range(max(X) * N + 1)] for _ in range(N + 1)] for _ in range(N + 1)]
dp[0][0][0] = 1

for i in range(N):
    for j in range(N):
        for k in range(max(X) * N + 1):
            dp[i + 1][j][k] += dp[i][j][k]
            if k >= X[i]:
                dp[i + 1][j + 1][k] += dp[i][j][k - X[i]]

ans = 0
for j in range(1, N + 1):
    ans += dp[N][j][j * A]

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

def func(b, n):
    res = 0
    while n > 0:
        res += n % b
        n //= b
    return res


def solve():
    n = int(input())
    s = int(input())

    if n == s:
        return n + 1
    elif n < s:
        return -1

    for b in range(2, n + 1):
        if b**2 > n:
            break
        if func(b, n) == s:
            return b

    b_cand = []
    for p in range(1, n + 1):
        if p**2 > n:
            break
        b = (n - s) // p + 1
        if b > 1:
            b_cand.append(b)
    b_cand = b_cand[::-1]

    for b in b_cand:
        # print(b)
        if func(b, n) == s:
            return b

    return -1


ans = solve()
print(ans)


Error: TLE

Fixed submission:

def func(b, n):
    res = 0
    while n > 0:
        res += n % b
        n //= b
    return res


def solve():
    n = int(input())
    s = int(input())

    if n == s:
        return n + 1
    elif n < s:
        return -1

    for b in range(2, n + 1):
        if b**2 > n:
            break
        if func(b, n) == s:
            return b

    b_cand = []
    for p in range(1, n + 1):
        if p**2 > n:
            break
        b = (n - s) // p + 1
        b_cand.append(b)
    b_cand = b_cand[::-1]

    for b in b_cand:
        if func(b, n) == s:
            return b

    return -1


ans = solve()
print(ans)
Problem p04014 (Python) — buggy submission, verdict: Runtime Error

from math import floor, sqrt, ceil


def f(b, n):
    if b < 2:
        return 0

    if n < b:
        return n

    return f(b, floor(n / b)) + n % b


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

if n == s:
    print(n + 1)
    exit()

for b in range(2, floor(sqrt(n)) + 1):
    if f(b, n) == s:
        print(b)
        exit()

for p in range(1, ceil(sqrt(n)))[::-1]:
    b = (n - s) / p + 1
    if b.is_integer():
        b = int(b)
        if f(b, n) == s:
            print(b)
            exit()

print(-1)


Error: 0

Fixed submission:

from math import floor, sqrt, ceil


def f(b, n):
    if n < b:
        return n

    return f(b, floor(n / b)) + n % b


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

if n == s:
    print(n + 1)
    exit()

for b in range(2, floor(sqrt(n)) + 1):
    if f(b, n) == s:
        print(b)
        exit()

for p in range(1, ceil(sqrt(n)))[::-1]:
    b = (n - s) / p + 1
    if b.is_integer():
        b = int(b)
        if f(b, n) == s:
            print(b)
            exit()

print(-1)
Problem p04015 (Python) — buggy submission, verdict: Time Limit Exceeded

import numpy as np

N, A = map(int, input().split())
X = [0] + list(map(int, input().split()))
NX = N * max(X + [A])

dp = np.zeros((N + 1, 2 * NX + 1), dtype=int)

dp[0][NX] = 1

for j in range(1, N + 1):
    y = X[j] - A
    for t in range(2 * NX + 1):
        if 0 <= t - y < 2 * NX:
            dp[j][t] = dp[j - 1][t] + dp[j - 1][t - y]
        else:
            dp[j][t] = dp[j - 1][t]

print(dp[N][NX] - 1)


Error: TLE

Fixed submission:

import numpy as np

N, A = map(int, input().split())
X = [0] + list(map(int, input().split()))
NX = N * max(X + [A])

dp = np.zeros((N + 1, N + 1, NX + 1), dtype=int)

dp[0][0][0] = 1

for j in range(N + 1):
    for k in range(N + 1):
        for s in range(NX + 1):
            if j >= 1 and s < X[j]:
                dp[j][k][s] = dp[j - 1][k][s]
            elif j >= 1 and k >= 1 and s >= X[j]:
                dp[j][k][s] = dp[j - 1][k][s] + dp[j - 1][k - 1][s - X[j]]

ret = 0

for k in range(1, N + 1):
    ret += dp[N][k][k * A]

print(ret)