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

from collections import defaultdict, deque


def longest_path(n, edges):
    adj_list = defaultdict(list)
    for x, y in edges:  # x -> y
        adj_list[y].append(x)

    state = [0] * n
    distances = [0] * n
    stack = deque(range(n))

    while stack:
        x = stack.pop()
        if state[x] == 0:
            state[x] = 1
            stack.append(x)
            stack.extend(adj_list[x])
        elif state[x] == 1:
            state[x] = 2
            if adj_list[x]:
                distances[x] = max(distances[y] for y in adj_list[x]) + 1

    return max(distances)


def main():
    n, m = [int(x) for x in input().split()]
    edges = [None] * m
    for i in range(m):
        edges[i] = [int(x) - 1 for x in input().split()]

    return longest_path(n, edges)


print(main())


Error: 0

Fixed submission:

from collections import defaultdict


def longest_path(n, edges):
    adj_list = defaultdict(list)
    for x, y in edges:  # x -> y
        adj_list[y].append(x)

    distances = [None] * n

    def dfs(x):
        if distances[x] is None:
            if adj_list[x]:
                for y in adj_list[x]:
                    dfs(y)
                distances[x] = max((distances[y] + 1 for y in adj_list[x]))
            else:
                distances[x] = 0

    for x in range(n):
        dfs(x)

    return max(distances)


def main():
    n, m = [int(x) for x in input().split()]
    edges = [None] * m
    for i in range(m):
        edges[i] = [int(x) - 1 for x in input().split()]

    return longest_path(n, edges)


print(main())
Problem p03166 (Python) — buggy submission, verdict: Runtime Error

import sys

sys.setrecursionlimit(10**7)


def dfs(v):
    if d[v] != -1:
        return d[v]
    res = -1
    for u in e[v]:
        res = max(res, dfs(u))
    res += 1
    d[v] = res
    return res


n, m = map(int, input().split())
e = tuple(set() for _ in range(n))
for _ in range(m):
    x, y = map(int, input().split())
    x -= 1
    y -= 1
    e[x].add(y)

d = [-1] * n
res = -1
for v in range(n):
    res = max(res, dfs(v))
print(res)


Error: 0

Fixed submission:

def dfs(v):
    if d[v] != -1:
        return d[v]
    res = -1
    for u in e[v]:
        res = max(res, dfs(u))
    res += 1
    d[v] = res
    return res


n, m = map(int, input().split())
e = tuple(set() for _ in range(n))
for _ in range(m):
    x, y = map(int, input().split())
    x -= 1
    y -= 1
    e[x].add(y)

d = [-1] * n
res = -1
for v in range(n):
    res = max(res, dfs(v))
print(res)
Problem p03166 (Python) — buggy submission, verdict: Runtime Error

import sys

sys.setrecursionlimit(10**9)


def rec(vv):
    if p[vv] >= 0:
        return p[vv]

    res = 0
    for nv in g[vv]:
        res = max(res, rec(nv) + 1)
    p[vv] = res
    return res


N, M = map(int, input().split())
g = [[] for _ in range(N)]
p = [-1 for _ in range(N)]

for _ in range(M):
    x, y = map(int, input().split())
    x -= 1
    y -= 1
    g[x].append(y)

ans = 0
for v in range(N):
    ans = max(ans, rec(v))
print(ans)


Error: 0

Fixed submission:

def rec(vv):
    res = 0
    for nv in g[vv]:
        if p[nv] >= 0:
            res = max(res, p[nv] + 1)
        else:
            res = max(res, rec(nv) + 1)
    p[vv] = res
    return res


N, M = map(int, input().split())
g = [[] for _ in range(N)]
p = [-1 for _ in range(N)]

for _ in range(M):
    x, y = map(int, input().split())
    x -= 1
    y -= 1
    g[x].append(y)

ans = 0
for v in range(N):
    ans = max(ans, rec(v))
print(ans)
Problem p03166 (Python) — buggy submission, verdict: Time Limit Exceeded

# -*- coding: utf-8 -*-
from collections import deque

from sys import setrecursionlimit

setrecursionlimit(1000000)

INF = float("inf")
# Input#
# n = int(input())
n, m = map(int, input().split())
# a = list(map(int, input().split()))

d = deque()
d_tmp = deque()
x = [-1]
y = [[-1] for _ in range((n + 1))]

for _ in [0] * m:
    x_in, y_in = map(int, input().split())
    #    x.append(x_in)
    y[x_in].append(y_in)
    d.append((x_in, y_in))

memo = [-1] * (n + 1)
ans = -1


def dp(i):
    global ans
    if memo[i] == -1:
        ret = 0
        for x_in in y[i][1:]:
            tmp = dp(x_in) + 1
            if tmp > ret:
                ret = tmp
        memo[i] = ret
        if ret > ans:
            ans = ret
        return ret
    else:
        return memo[i]


for i, _ in d:
    if memo[i] == -1:
        dp(i)

print("{}".format(ans))


Error: TLE

Fixed submission:

# -*- coding: utf-8 -*-
from collections import deque

from sys import setrecursionlimit

setrecursionlimit(1000000)

INF = float("inf")
# Input#
# n = int(input())
n, m = map(int, input().split())
# a = list(map(int, input().split()))

d = deque()
d_tmp = deque()
x = [-1]
y = [-1]

for _ in [0] * m:
    x_in, y_in = map(int, input().split())
    #    x.append(x_in)
    #    y.append(y_in)
    d.append((x_in, y_in))

memo = [-1] * (n + 1)
ans = -1


def dp(i):
    global ans
    if memo[i] == -1:
        ret = 0
        for x_in, y_in in d:
            if x_in == i:
                tmp = dp(y_in) + 1
                if tmp > ret:
                    ret = tmp
        memo[i] = ret
        if ret > ans:
            ans = ret
        return ret
    else:
        return memo[i]


for i, _ in d:
    if memo[i] == -1:
        dp(i)

print("{}".format(ans))
Problem p03166 (Python) — buggy submission, verdict: Runtime Error

import sys

input = sys.stdin.readline
sys.setrecursionlimit(10**6)


def get_kyori(i):
    if kos[i] == []:
        kyori[i] = 0
        return 0
    if kyori[i] > -1:
        return kyori[i]
    re = 0
    for ko in kos[i]:
        new = get_kyori(ko) + 1
        if re < new:
            re = new
    kyori[i] = re
    return re


n, m = map(int, input().split())
kos = [[] for _ in range(n + 1)]
kyori = [-1] * (n + 1)
for _ in range(m):
    oya, ko = map(int, input().split())
    kos[oya] += [ko]

for i in range(1, n + 1):
    if kyori[i] == -1:
        kyori[i] = get_kyori(i)

print(max(kyori[1:]))


Error: 0

Fixed submission:

def get_kyori(i):
    if kos[i] == []:
        kyori[i] = 0
        return 0
    if kyori[i] > -1:
        return kyori[i]
    re = 0
    for ko in kos[i]:
        new = get_kyori(ko) + 1
        if re < new:
            re = new
    kyori[i] = re
    return re


n, m = map(int, input().split())
kos = [[] for _ in range(n + 1)]
kyori = [-1] * (n + 1)
for _ in range(m):
    oya, ko = map(int, input().split())
    kos[oya] += [ko]

for i in range(1, n + 1):
    if kyori[i] == -1:
        kyori[i] = get_kyori(i)

print(max(kyori[1:]))
Problem p03166 (Python) — buggy submission, verdict: Time Limit Exceeded

import sys

sys.setrecursionlimit(1000000)


def dfs(v):
    if cache[v] > -1:
        return cache[v]
    lv = links[v]
    cache[v] = ret = max(map(dfs, lv)) + 1 if lv else 0
    return ret


n, m = map(int, input().split())
sss = set(range(n))
links = [set() for _ in range(n)]
cache = [-1] * n
for line in sys.stdin.readlines():
    x, y = map(int, line.split())
    x -= 1
    y -= 1
    sss.discard(y)
    links[x].add(y)

print(max(map(dfs, sss)))


Error: TLE

Fixed submission:

import sys
from functools import lru_cache

sys.setrecursionlimit(1000000)


@lru_cache(maxsize=None)
def dfs(v):
    lv = links[v]
    return max(map(dfs, lv)) + 1 if lv else 0


n, m = map(int, input().split())
sss = set(range(n))
links = [set() for _ in range(n)]
for line in sys.stdin.readlines():
    x, y = map(int, line.split())
    x -= 1
    y -= 1
    sss.discard(y)
    links[x].add(y)

print(max(map(dfs, sss)))
Problem p03166 (Python) — buggy submission, verdict: Runtime Error

import sys
from functools import lru_cache

sys.setrecursionlimit(1000000)


@lru_cache(maxsize=None)
def dfs(v):
    lv = links[v]
    return max(map(dfs, lv)) + 1 if lv else 0


n, m = map(int, input().split())
sss = set(range(n))
links = [set() for _ in range(n)]
for line in sys.stdin.readlines():
    x, y = map(int, line.split())
    x -= 1
    y -= 1
    sss.discard(y)
    links[x].add(y)

print(max(map(dfs, sss)))


Error: 0

Fixed submission:

import sys
from functools import lru_cache

sys.setrecursionlimit(100000)


@lru_cache(maxsize=None)
def dfs(v):
    lv = links[v]
    return max(map(dfs, lv)) + 1 if lv else 0


n, m = map(int, input().split())
sss = set(range(n))
links = [set() for _ in range(n)]
for line in sys.stdin.readlines():
    x, y = map(int, line.split())
    x -= 1
    y -= 1
    sss.discard(y)
    links[x].add(y)

print(max(map(dfs, sss)))
Problem p03168 (Python) — buggy submission, verdict: Time Limit Exceeded

N = int(input())
P = list(map(float, input().split()))
# dp = [[0] * (N + 1) for i in range(N + 1)]
# dp[0][0] = 1.0
# for i in range(1, N + 1):
#     for j in range(N):
#         if j < N:
#             dp[i][j + 1] += dp[i - 1][j] * P[i - 1]
#         dp[i][j] += dp[i - 1][j] * (1 - P[i - 1])
#
# num = 0
# for i in range(N // 2 + 1, N + 1):
#     num += dp[N][i]
#
# print(num)


dp = [0] * (N + 1)
dp[0] = 1
for i in range(1, N + 1):
    tmp = dp
    dp = [0] * (N + 1)
    for j in range(N):
        if j < N:
            dp[j + 1] += tmp[j] * P[i - 1]
        dp[j] += tmp[j] * (1 - P[i - 1])

num = 0
for i in range(N // 2 + 1, N + 1):
    num += dp[i]

print(num)


Error: TLE

Fixed submission:

N = int(input())
P = list(map(float, input().split()))

dp = [0] * (N + 1)
dp[0] = 1
for i in range(1, N + 1):
    tmp = dp
    dp = [0] * (N + 1)
    for j in range(N):
        if j < N:
            dp[j + 1] += tmp[j] * P[i - 1]
        dp[j] += tmp[j] * (1 - P[i - 1])

num = 0
for i in range(N // 2 + 1, N + 1):
    num += dp[i]

print(num)
Problem p03168 (Python) — buggy submission, verdict: Time Limit Exceeded

# -*- coding: utf-8 -*-
# from collections import deque
from sys import setrecursionlimit

setrecursionlimit(1000000)


# INF = float("inf")

# d = deque()
# ---Input---#
n = int(input())
# n, k = map(int, input().split())
p = list(map(float, input().split()))
p = [-1] + p

memo = [[-1 for _ in [0] * (n + 1)] for _ in [0] * (n + 1)]
memo[0][0] = 1
for i in range(len(p))[1:]:
    memo[0][i] = 0
    memo[i][0] = memo[i - 1][0] * (1 - p[i])
memo[0][0] = 1


def dp(i, j):
    if memo[i][j] == -1:
        memo[i][j] = dp(i - 1, j) * (1 - p[i]) + dp(i - 1, j - 1) * p[i]
    return memo[i][j]


ans = 0
tar = int(n / 2) + 1
for i in range(n + 1):
    dp(i, i)
    dp(i, n)

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


print("{}".format(ans))


Error: TLE

Fixed submission:

# -*- coding: utf-8 -*-
# from collections import deque
from sys import setrecursionlimit

setrecursionlimit(1000000)


# INF = float("inf")

# d = deque()
# ---Input---#
n = int(input())
# n, k = map(int, input().split())
p = list(map(float, input().split()))
p = [-1] + p

memo = [[-1 for _ in [0] * (n + 1)] for _ in [0] * (n + 1)]
memo[0][0] = 1
for i in range(len(p))[1:]:
    memo[0][i] = 0
    memo[i][0] = memo[i - 1][0] * (1 - p[i])
memo[0][0] = 1


def dp(i, j):
    if memo[i][j] == -1:
        memo[i][j] = dp(i - 1, j) * (1 - p[i]) + dp(i - 1, j - 1) * p[i]
    return memo[i][j]


ans = 0
tar = int(n / 2) + 1
for i in range(n + 1)[tar:]:
    ans += dp(n, i)


print("{}".format(ans))
Problem p03169 (Python) — buggy submission, verdict: Time Limit Exceeded

import numpy as np
from numba import njit


@njit(cache=True)
def solve(dp, N, n1, n2, n3):

    if dp[n1][n2][n3] >= 0.0:
        return dp[n1][n2][n3]
    if n1 == n2 == n3 == 0:
        return 0.0

    cnt = n1 + n2 + n3
    ret = 0
    if n3 >= 1:
        ret += solve(dp, N, n1, n2 + 1, n3 - 1) * n3
    if n2 >= 1:
        ret += solve(dp, N, n1 + 1, n2 - 1, n3) * n2
    if n1 >= 1:
        ret += solve(dp, N, n1 - 1, n2, n3) * n1

    ret = (ret + N) / cnt
    dp[n1][n2][n3] = ret
    return ret


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

    dp = np.full((N + 1, N + 1, N + 1), -1, dtype=np.float64)

    a1, a2, a3 = 0, 0, 0
    for i in range(N):
        if A[i] == 1:
            a1 += 1
        elif A[i] == 2:
            a2 += 1
        elif A[i] == 3:
            a3 += 1

    print(solve(dp, N, a1, a2, a3))


if __name__ == "__main__":
    main()


Error: TLE

Fixed submission:

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


dp = [[[-1.0 for _ in range(N + 1)] for _ in range(N + 1)] for _ in range(N + 1)]


def solve(n1, n2, n3):

    if dp[n1][n2][n3] >= 0.0:
        return dp[n1][n2][n3]
    if n1 == n2 == n3 == 0:
        return 0.0

    cnt = n1 + n2 + n3
    ret = 0
    if n3 >= 1:
        ret += solve(n1, n2 + 1, n3 - 1) * n3
    if n2 >= 1:
        ret += solve(n1 + 1, n2 - 1, n3) * n2
    if n1 >= 1:
        ret += solve(n1 - 1, n2, n3) * n1

    ret = (ret + N) / cnt
    dp[n1][n2][n3] = ret
    return ret


a1, a2, a3 = 0, 0, 0
for i in range(N):
    if A[i] == 1:
        a1 += 1
    elif A[i] == 2:
        a2 += 1
    elif A[i] == 3:
        a3 += 1

print(solve(a1, a2, a3))
Problem p03171 (Python) — buggy submission, verdict: Time Limit Exceeded

def main():
    n = int(input())
    a = list(map(int, input().split()))
    dp = [0] * (n * n)
    for i in range(n):
        dp[i + i * n] = a[i]
    for i in range(n - 2, -1, -1):
        for j in range(i + 1, n):
            dp[i + j * n] = max(a[i] - dp[i + 1 + j * n], a[j] - dp[i + (j - 1) * n])
    print(dp[(n - 1) * n])


main()


Error: TLE

Fixed submission:

n = int(input())
a = list(map(int, input().split()))
dp = [[0] * n for _ in range(n)]
for i in range(n):
    dp[i][i] = a[i]

for i in range(n - 2, -1, -1):
    for j in range(i + 1, n):
        dp[i][j] = max(a[i] - dp[i + 1][j], a[j] - dp[i][j - 1])

print(dp[0][n - 1])
Problem p03171 (Python) — buggy submission, verdict: Time Limit Exceeded

def kukan(n, a):
    dp = [[0] * n for _ in range(n)]
    for i in range(n):
        dp[i][i] = a[i]
    for i in range(n - 2, -1, -1):
        for j in range(i + 1, n):
            L = a[i] - dp[i + 1][j]
            R = a[j] - dp[i][j - 1]
            dp[i][j] = L if L > R else R
    return dp[0][n - 1]


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


Error: TLE

Fixed submission:

def kukan(n, a):
    dp = [[0] * n for _ in range(n)]
    for i in range(n):
        dp[i][i] = a[i]
    for i in range(n - 2, -1, -1):
        for j in range(i + 1, n):
            dp[i][j] = max(a[i] - dp[i + 1][j], a[j] - dp[i][j - 1])
    return dp[0][n - 1]


n = int(input())
a = list(map(int, input().split()))
print(kukan(n, a))
Problem p03173 (Python) — buggy submission, verdict: Time Limit Exceeded

# https://atcoder.jp/contests/dp/tasks/dp_n

import itertools as it

INF = 1 << 62
N = int(input())
xs = [int(s) for s in input().split()]
acc = list(it.accumulate([0] + xs))

dp = [[INF] * (N + 1) for _ in range(N)]

for i in range(N):
    dp[i][i + 1] = 0

for w in range(2, N + 1):
    for i in range(N - w + 1):
        j = i + w
        subcost = min(dp[i][k] + dp[k][j] for k in range(i + 1, j))
        dp[i][j] = subcost + acc[j] - acc[i]

print(dp[0][N])


Error: TLE

Fixed submission:

# https://atcoder.jp/contests/dp/tasks/dp_n

import itertools as it

INF = 1000000007
N = int(input())
xs = [int(s) for s in input().split()]
acc = list(it.accumulate([0] + xs))

dp = [[INF] * (N + 1) for _ in range(N)]

for i in range(N):
    dp[i][i + 1] = 0

for w in range(2, N + 1):
    for i in range(N - w + 1):
        j = i + w
        dp[i][j] = min(dp[i][k] + dp[k][j] + acc[j] - acc[i] for k in range(i + 1, j))

print(dp[0][N])
Problem p03176 (Python) — buggy submission, verdict: Time Limit Exceeded

n = int(input())
heights = [int(c) for c in input().split()]
beauty = [int(c) for c in input().split()]


base = 1
while base <= n:
    base *= 2

tree = [0] * (2 * base)

dp = [0] * (n + 1)
for i in range(n):
    x = heights[i] + base
    best = 0
    while x > 1:
        if x % 2 == 1:
            best = max(best, tree[x - 1])
        x //= 2

    dp[heights[i]] = best + beauty[i]

    j = base + heights[i]
    while j >= 1:
        tree[j] = max(tree[j], dp[heights[i]])
        j //= 2

    # for j in range(i):
    #     if heights[i] > heights[j] and beauty[i] + dp[j] > max_beauty:
    #         max_beauty = beauty[i] + dp[j]


ans = max(dp)
print(ans)


Error: TLE

Fixed submission:

n = int(input())
heights = [int(c) for c in input().split()]
beauty = [int(c) for c in input().split()]

dp = list(beauty)

for i in range(n):
    max_beauty = beauty[i]
    for j in range(i):
        if heights[i] > heights[j] and beauty[i] + dp[j] > max_beauty:
            max_beauty = beauty[i] + dp[j]

    dp[i] = max_beauty

ans = max(dp)
print(ans)
Problem p03176 (Python) — buggy submission, verdict: Runtime Error

def get(i):
    mx = 0
    while i > 0:
        if bit[i] > mx:
            mx = bit[i]
        i -= i & -i
    return mx


def update(i, x):
    while i < n + 1:
        if x > bit[i]:
            bit[i] = x
        i += i & -i


def f(hs, a_s):
    for h, a in zip(hs, a_s):
        update(h, get(h - 1) + a)
    print(max(bit))


n = int(input())
hs = list(map(int, input().split()))
a_s = list(map(int, input().split()))
bit = [0] * (n + 1)
f(hs, a_s)


Error: TypeError: f() takes 2 positional arguments but 3 were given
Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p03176/Python/s612498010.py", line 27, in <module>
    f(n, hs, a_s)
TypeError: f() takes 2 positional arguments but 3 were given


Fixed submission:

def get(i):
    mx = 0
    while i > 0:
        if bit[i] > mx:
            mx = bit[i]
        i -= i & -i
    return mx


def update(i, x):
    while i < n + 1:
        if x > bit[i]:
            bit[i] = x
        i += i & -i


def f(hs, a_s):
    for h, a in zip(hs, a_s):
        update(h, get(h - 1) + a)
    print(max(bit))


n = int(input())
hs = list(map(int, input().split()))
a_s = list(map(int, input().split()))
bit = [0] * (n + 1)
f(n, hs, a_s)
Problem p03176 (Python) — buggy submission, verdict: Time Limit Exceeded

def get(i):
    mx = 0
    while i > 0:
        if bit[i] > mx:
            mx = bit[i]
        i -= i & -i
    return mx


def update(i, x):
    while i < n + 1:
        if x > bit[i]:
            bit[i] = x
        i += i & -i


n = int(input())
hs = list(map(int, input().split()))
a_s = list(map(int, input().split()))

bit = [0] * (n + 1)
for h, a in zip(hs, a_s):
    update(h, get(h - 1) + a)
print(max(bit))


Error: TLE

Fixed submission:

def f(n, hs, a_s):
    sum_a = [0] * n
    sum_a[0] = a_s[0]
    for ai, a in enumerate(a_s[1:]):
        mx = 0
        h = hs[ai + 1]
        for i in range(ai + 1):
            if h > hs[i]:
                if sum_a[i] > mx:
                    mx = sum_a[i]
        sum_a[ai + 1] = a + mx
    return sum_a[-1]


n = int(input())
hs = list(map(int, input().split()))
a_s = list(map(int, input().split()))
print(f(n, hs, a_s))
Problem p03194 (Python) — buggy submission, verdict: Time Limit Exceeded

N, P = list(map(int, input().split()))
if N == 1:
    print(P)
    exit()
elif P == 1:
    print("1")
    exit()

ans = 1
tmp = P

for i in range(2, int(pow(P, 1 / N)) + 1):
    po = pow(i, N)
    while tmp % po == 0:
        tmp = tmp // po
        ans = ans * i
        # print(i, tmp, ans)

    if tmp < po:
        break

print(ans)


Error: TLE

Fixed submission:

N, P = list(map(int, input().split()))
if N == 1:
    print(P)
    exit()
elif P == 1:
    print("1")
    exit()

ans = 1
tmp = P

for i in range(2, P):
    po = pow(i, N)
    while tmp % po == 0:
        tmp = tmp // po
        ans = ans * i
        # print(i, tmp, ans)

    if tmp < po:
        break

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

def usage():
    c, n = map(int, input().split())
    if c == 1:
        print(n)
        return 0
    ans = 1
    fct = factorize(n)
    for i in range(len(fct)):
        if fct[i][1] >= c:
            ans *= fct[i][0] ** (fct[i][1] // c)
    print(ans)


def factorize(n):
    fct = []
    b, e = 2, 0
    while b * b <= n:
        while n % b == 0:
            n = n // b
            e = e + 1
        if e > 0:
            fct.append((b, e))
        b, e = b + 1, 0
    if n > 1:
        fct.append((n, 1))
    return fct


if __name__ == "__main__":
    usage()


Error: 0

Fixed submission:

import sympy

n, p = map(int, input().split())
a = sympy.factorint(p)
ans = 1
for i, j in a.items():
    if j >= n:
        ans *= i
print(ans)
Problem p03195 (Python) — buggy submission, verdict: Runtime Error

N = int(input())
a = [int(input()) % 2 for _ in range(N)]
res = sum(a)
print("first" if res > 0 else "second")


Error: TypeError: can't multiply sequence by non-int of type 'list'
Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p03195/Python/s025451632.py", line 2, in <module>
    a = [int(input()) for _ in range(N)] * [0, 0]
TypeError: can't multiply sequence by non-int of type 'list'


Fixed submission:

N = int(input())
a = [int(input()) for _ in range(N)] * [0, 0]
a.sort()
if len(a) >= 2:
    res = a[-2]
else:
    res = a[-1]
res += sum(a[:-2])
print("second" if res % 2 == 0 else "first")
Problem p03196 (Python) — buggy submission, verdict: Time Limit Exceeded

def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-(n**0.5) // 1)) + 1):
        if temp % i == 0:
            cnt = 0
            while temp % i == 0:
                cnt += 1
                temp //= i
            arr.append([i, cnt])

    if temp != 1:
        arr.append([temp, 1])

    if arr == []:
        arr.append([n, 1])

    return arr


def main():
    N, P = map(int, input().split())
    A = factorization(P)

    B = []
    for a, b in A:
        if b >= N:
            B.append((a, b // N))

    ans = 1
    for a, b in B:
        ans *= a**b
    print(ans)


main()


Error: TLE

Fixed submission:

def main():
    N, P = map(int, input().split())

    left = 0
    right = 10**12
    while left + 1 < right:
        mid = (left + right) // 2
        if mid**N <= P:
            left = mid
        else:
            right = mid

    cnt = 0
    for j in range(left, 0, -1):
        if P % (j**N) == 0:
            print(j)
            break
        else:
            cnt += 1

    j = left
    cnt = 0
    while P % (j**N) != 0:
        cnt += 1
        j = left // cnt


main()
Problem p03196 (Python) — buggy submission, verdict: Runtime Error

import collections as cl

N, P = map(int, input().split())
if P == 1:
    print(1)
    exit()
P_prime = []
while P % 2 == 0:
    P_prime.append(2)
    P //= 2
odd = 3
while odd * odd <= P:
    if P % odd == 0:
        P_prime.append(odd)
        P //= odd
    else:
        odd += 2
if P != 1:
    P_prime.append(P)

ans = 1
dic_P = cl.Counter(P_prime)
for prime, num in dic_P.items():
    ans *= pow(prime, num // N)
print(ans)


Error: WA

Fixed submission:

N, P = map(int, input().split())
P_prime = []
while P % 2 == 0:
    P_prime.append(2)
    P //= 2
odd = 3
while odd * odd <= P:
    if P % odd == 0:
        P_prime.append(odd)
        P //= odd
    else:
        odd += 2
if P != 1:
    P_prime.append(P)
edge = [0]
for i in range(len(P_prime) - 1):
    if P_prime[i] != P_prime[i + 1]:
        edge.append(i)
edge.append(len(P_prime) - 1)
ans = 1
for i in range(len(edge) - 1):
    ans *= pow(P_prime[edge[i + 1]], (edge[i + 1] - edge[i]) // N)
print(ans)
Problem p03196 (Python) — buggy submission, verdict: Time Limit Exceeded

N, P = map(int, input().split())

if N == 1:
    print(P)
    exit()

div = 2
res = 1
R = P

while div * div <= R and div <= P:
    cnt = 0
    while P % div == 0:
        P /= div
        cnt += 1

        if cnt == N:
            res *= div
            cnt = 0
    div += 1

print(res)


Error: TLE

Fixed submission:

N, P = map(int, input().split())

div = 2
res = 1
while div * div <= P:
    cnt = 0
    tmp = P
    while P % div == 0:
        tmp /= div
        cnt += 1

        if cnt == N:
            res *= div
            cnt = 0
    div += 1

print(res)