Harryxun/GPT-100-sft-dataset huggingface dataset:
Problem p02727 (python)

Fixed submission:
X, Y, A, B, C = map(int, input().split())
p = sorted([int(i) for i in input().split()], reverse=True)
q = sorted([int(i) for i in input().split()], reverse=True)
r = sorted([int(i) for i in input().split()])

p = p[:X]
q = q[:Y]
plus_r = []

while r != [] and ((p != [] and r[-1] > p[-1]) or (q != [] and r[-1] > q[-1])):
    if p == []:
        q.pop()
        plus_r.append(r.pop())
        continue
    elif q == []:
        p.pop()
        plus_r.append(r.pop())
        continue

    if p[-1] < q[-1]:
        p.pop()
    else:
        q.pop()
    plus_r.append(r.pop())

print(sum(p) + sum(q) + sum(plus_r))

Buggy submission (verdict: Runtime Error):
X, Y, A, B, C = map(int, input().split())
p = sorted([int(i) for i in input().split()], reverse=True)
q = sorted([int(i) for i in input().split()], reverse=True)
r = sorted([int(i) for i in input().split()])

p = p[:X]
q = q[:Y]
plus_r = []

while r != [] and (p != [] and r[-1] > p[-1]) or (q != [] and r[-1] > q[-1]):
    if p[-1] < q[-1]:
        p.pop()
    else:
        q.pop()
    plus_r.append(r.pop())

print(sum(p) + sum(q) + sum(plus_r))

Buggy submission (verdict: Runtime Error):
X, Y, A, B, C = map(int, input().split())
p = sorted([int(i) for i in input().split()], reverse=True)
q = sorted([int(i) for i in input().split()], reverse=True)
r = sorted([int(i) for i in input().split()])

p = p[:X]
q = q[:Y]
plus_r = []

while r != [] and ((p != [] and r[-1] > p[-1]) or (q != [] and r[-1] > q[-1])):
    if p[-1] < q[-1]:
        p.pop()
    else:
        q.pop()
    plus_r.append(r.pop())

print(sum(p) + sum(q) + sum(plus_r))
Problem p02972 (python)

Fixed submission:
N = int(input())
A = [int(i) for i in input().split()]
A.insert(0, 0)

ans = []
for i in reversed(range(1, N + 1)):
    if sum(A[i::i]) % 2 != A[i]:
        A[i] = (A[i] + 1) % 2
    if A[i] == 1:
        ans.append(i)

print(len(ans))
if len(ans) > 0:
    print(*ans)

Buggy submission (verdict: Runtime Error):
def make_divisors(n: int) -> list:
    divisors = []

    for i in range(1, int(n**0.5) + 1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n // i)

    return divisors


N = int(input())
A = [int(i) for i in input().split()]

ans = []
for i in reversed(range(N)):
    if A[i] == 1:
        ans.append(i + 1)
        divisors = make_divisors(i + 1)
        for j in divisors:
            A[j + 1] = (A[j + 1] + 1) % 2

if A.count(1) != 0:
    print(len(ans))
    for i in reversed(range(len(ans))):
        print(ans[i])
else:
    print(-1)

Buggy submission (verdict: Runtime Error):
def make_divisors(n: int) -> list:
    divisors = []

    for i in range(1, int(n**0.5) + 1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n // i)

    return divisors


N = int(input())
A = [int(i) for i in input().split()]

ans = []
for i in reversed(range(N)):
    if A[i] == 1:
        ans.append(i + 1)
        divisors = make_divisors(i + 1)
        for j in divisors:
            A[j + 1] = (A[j + 1] + 1) % 2

print(len(ans))
for i in reversed(range(len(ans))):
    print(ans[i])
Problem p03030 (python)

Fixed submission:
N = int(input())
SP = [(i + 1, input().split()) for i in range(N)]
P = [[0, 0, 0] for _ in range(N)]
for i in range(N):
    P[i][0] = SP[i][0]
    P[i][1] = SP[i][1][0]
    P[i][2] = int(SP[i][1][1])
P.sort(key=lambda x: x[2], reverse=True)
P.sort(key=lambda x: x[1])

for p in P:
    print(p[0])

Buggy submission (verdict: Runtime Error):
N = int(input())
SP = [(i + 1, input().split()) for i in range(N)]
P = [[0, 0, 0] for _ in range(N)]
for i in range(N):
    P[i][0] = SP[i][0]
    P[i][1] = SP[i][1][0]
    P[i][2] = int(SP[i][1][1])
P.sort(key=lambda x: x[1][1], reverse=True)
P.sort(key=lambda x: x[1][0])

for p in P:
    print(p[0])
Problem p03101 (python)

Fixed submission:
H, W = map(int, input().split())
h, w = map(int, input().split())

print((H - h) * (W - w))

Buggy submission (verdict: Runtime Error):
H, W = int(input().split())
h, w = int(input().split())

print((H - h) * (W - w))
H, W = map(int, input().split())
h, w = map(int, input().split())

print((H - h) * (W - w))

Buggy submission (verdict: Runtime Error):
H, W = int(input().split())
h, w = int(input().split())

print((H - h) * (W - w))
Problem p03105 (python)

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

if C <= B / A:
    print(C)
else:
    print(int(B / A))

Buggy submission (verdict: Runtime Error):
K = int(input())

if K % 2:
    print((K // 2) * (K // 2 + 1))
else:
    print((K // 2)**2)
Problem p03219 (python)

Fixed submission:
X, Y = map(int, input().split())

print(X + int(Y / 2))

Buggy submission (verdict: Runtime Error):
X, Y = map(int, input())

print(X + int(Y / 2))
Problem p03238 (python)

Fixed submission:
N = int(input())

if N == 1:
    print("Hello World")
else:
    A = int(input())
    B = int(input())
    print(A + B)

Buggy submission (verdict: Runtime Error):
N = int(input())

if N == 1:
    print("Hello World")
else:
    A, B = map(int, input().split())
    print(A + B)

Buggy submission (verdict: Runtime Error):
N = input()

if N == 1:
    print("Hello World")
else:
    A, B = map(int, input().split())
    print(A + B)
Problem p03292 (python)

Fixed submission:
A = [int(x) for x in input().split()]

print(max(A) - min(A))

Buggy submission (verdict: Runtime Error):
A = [map(int, input().split())]

print(max(A) - min(A))
Problem p03359 (python)

Fixed submission:
a, b = map(int, input().split())
print(a if a <= b else a - 1)

Buggy submission (verdict: Runtime Error):
a, b = int(input().split())
print(a if a <= b else a - 1)
Problem p02817 (python)

Fixed submission:
S, T = input().split()
print(T+S)

Buggy submission (verdict: Runtime Error):
A, B, K = map(int, input().split())
print(max(0, A - K), max(0, min(A + B -K, B)))
Problem p02818 (python)

Fixed submission:
A, B, K = map(int, input().split())
print(A - K, B) if A > K else print(0, A + B -K) if A + B > K else print(0, 0)

Buggy submission (verdict: Runtime Error):
A, B, K = map(int, input().split())
print(max(0, A - K), max(0, min(A + B -K, B))
Problem p02898 (python)

Fixed submission:
N, K = map(int, input().split())
lst = list(map(int, input().split()))
i = 0
for h_i in lst:
    if h_i < K:
        continue
    else:
        i += 1
print(i)

Buggy submission (verdict: Runtime Error):
N, K = map(int, input().split())
lst = [map(int, input().split())]
i = 0
for h_i in lst:
    if h_i < K:
        continue
    i += 1
print(i)
Problem p02921 (python)

Fixed submission:
S = input()
T = input()
i = 0
for j in range(3):
    if S[j] == T[j]:
        i += 1
print(i)

Buggy submission (verdict: Runtime Error):
S = input()
T = input()
i = 1
for j in range(3):
    if S[j] == T[J]:
        i += 1
print(i)

Buggy submission (verdict: Runtime Error):
S = input()
T = input()
i = 1
for j in range(3):
    if S[j] = T[J]:
        i += 1
print(i)
Problem p03101 (python)

Fixed submission:
H, W = map(int, input().split()) 
h, w = map(int, input().split())
print((H-h) * (W - w))

Buggy submission (verdict: Runtime Error):
H, W = map(int, input().split()) 
h, w = map(int, input(.split()))
print((H-h) * (W - w))
Problem p03109 (python)

Fixed submission:
S = input()
print('Heisei' if S[5:7] < '04' or (S[6] == '4' and S[8:] < '31') else 'TBD')

Buggy submission (verdict: Runtime Error):
S = input()
print('Heisei' if S[5, 7] < '04' or (S[6] == 4 and S[8,] < '31') else 'TBD')
Problem p03136 (python)

Fixed submission:
N = int(input())
l = list(map(int, input().split()))
print('Yes' if max(l) < sum(l) - max(l) else 'No')

Buggy submission (verdict: Runtime Error):
N = int(input())
l = list(int, input().split())
print('Yes' if max(l) < sum(l) - max(l) else 'No')

Buggy submission (verdict: Runtime Error):
N = int(input())
l = [int(input()) for i in range(N)]
print('Yes' if max(l) < sum(l) - max(l) else 'No')
Problem p03219 (python)

Fixed submission:
X, Y = map(int, input().split())
print(X+(Y//2))

Buggy submission (verdict: Runtime Error):
X, Y = map(int, input())
print(X+(Y/2))
Problem p03447 (python)

Fixed submission:
X = int(input())
A = int(input())
B = int(input())
print((X-A)%B)

Buggy submission (verdict: Runtime Error):
A, B = map(int, input().split())
print((A+B)%24)
Problem p03455 (python)

Fixed submission:
a = list(map(int, input().split()))

if a[0] * a[1] % 2 == 0:
    print('Even')

else:
    print('Odd')

Buggy submission (verdict: Runtime Error):
a = list(map(int, input().split()))

if a[0] * b[1] % 2 == 0:
    print('Even')

else:
    print('Odd')

Buggy submission (verdict: Runtime Error):
a = map(int, input().split())

if a[0] * b[1] % 2 == 0:
    print('Even')

else:
    print('Odd')

Buggy submission (verdict: Runtime Error):
a = int(input())
b = int(input())

if a * b % 2 == 0:
    print('Even')

else:
    print('Odd')
Problem p03477 (python)

Fixed submission:
A, B, C, D = map(int, input().split())
print('Left' if (A + B) > C + D else 'Balanced' if (A + B) == (C + D) else 'Right')

Buggy submission (verdict: Runtime Error):
A, B, C, D = map(int, input().split())
print('Left' if (A + B) > C + D else 'Balanced' if (A + B) = (C + D) else 'Right')

Buggy submission (verdict: Runtime Error):
A, B, C, D = map(int, input().split())
print('Left' if A + B > C + D else 'Balanced' if A + B = C + D else 'Right')
Problem p03635 (python)

Fixed submission:
n, m = map(int, input().split())
print((n - 1) * (m - 1))

Buggy submission (verdict: Runtime Error):
n = int(input())
m = int(input())
print((n - 1) * (m - 1))
Problem p03729 (python)

Fixed submission:
A, B, C = input().split()
print('YES'  if A[-1] == B[0] and B[-1] == C[0] else 'NO')

Buggy submission (verdict: Runtime Error):
A, B, C = input().split()
print('YES'  if A[-1] == B[0] and B[-1] == C[0])
Problem p03796 (python)

Fixed submission:
N = int(input())
import math
N = math.factorial(N)
print(N % (10**9+7))

Buggy submission (verdict: Runtime Error):
N = int(input())
import math
N! = math.factorial(N)
print(N!)
Problem p03943 (python)

Fixed submission:
lst = list(map(int, input().split()))
lst = sorted(lst)
print('Yes' if lst[0] + lst[1] == lst[2] else 'No')

Buggy submission (verdict: Runtime Error):
lst = list(map(int, input().split()))
lst = lst.sorted()
print('Yes' if lst[0] + lst[1] == lst[2] else 'No')

Buggy submission (verdict: Runtime Error):
lst = [map(int, input().split())]
lst = lst.sorted()
print('Yes' if lst[0] + lst[1] == lst[2] else 'No')
Problem p02802 (python)

Fixed submission:
n, m = list(map(int, input().split()))
p = [input().split() for i in range(m)]

s = {i + 1 for i in range(n)}

ac = 0
wa = {i + 1:[0, False] for i in range(n)}


for i in range(m):
  if int(p[i][0]) in s:
    if p[i][1] == 'WA':
      wa[int(p[i][0])][0] += 1
    else:
      ac += 1
      s.discard(int(p[i][0]))
      wa[int(p[i][0])][1] = True
      
wa_wa = 0
for i in range(1, n + 1):
  if wa[i][1]:
    wa_wa += wa[i][0]
print(ac, wa_wa)

Buggy submission (verdict: Runtime Error):
n, m = list(map(int, input().split()))
p = [input().split() for i in range(m)]

s = {i + 1 for i in range(n)}

ac = 0
wa = {i + 1:[0, False] for i in range(n)}


for i in range(m):
  if int(p[i][0]) in s:
    if p[i][1] == 'WA':
      wa[int(p[i][0])][0] += 1
    else:
      ac += 1
      s.discard(int(p[i][0]))
      wa[int(p[i][0])][1] = True
      
wa_wa = 0
for i in range(1, n + 1):
  if wa[i][1]:
    wa_wa += wa[i][0]
print(ac, wa_wa)
n, m = list(map(int, input().split()))
p = [input().split() for i in range(m)]

s = {i + 1 for i in range(n)}

ac = 0
wa = {i + 1:[0, False] for i in range(n)}


for i in range(m):
  if int(p[i][0]) in s:
    if p[i][1] == 'WA':
      wa[int(p[i][0])][0] += 1
    else:
      ac += 1
      s.discard(int(p[i][0]))
      wa[int(p[i][0])][1] = True
      
wa_wa = 0
for i in range(1, n + 1):
  if wa[i][1]:
    wa_wa += wa[i][0]
print(ac, wa_wa)
Problem p03164 (python)

Fixed submission:
n, W = list(map(int,input().split()))
w_v = [list(map(int,input().split())) for i in range(n)]
w = [w_v[i][0] for i in range(n)]
v = [w_v[i][1] for i in range(n)]
inf = 10**9+1
dp = [[inf] * (sum(v)+1) for i in range(n+1)]
dp[0][0] = 0

for i in range(n):
  for j in range(sum(v)+1):
    if j - v[i] >= 0:
      dp[i+1][j] = min(dp[i+1][j], dp[i][j-v[i]] + w[i])

    dp[i+1][j] = min(dp[i+1][j], dp[i][j])


for i in range(sum(v)+1):
  if dp[n][i] > W:
    pass
  else:
    ans = i
print(ans)

Buggy submission (verdict: Runtime Error):
import numpy as np

n, W = list(map(int,input().split()))
w_v = [list(map(int,input().split())) for i in range(n)]
w = [w_v[i][0] for i in range(n)]
v = [w_v[i][1] for i in range(n)]
inf = 10**9+1
dp = [[inf] * (sum(v)+1) for i in range(n+1)]
#dp = np.zeros((n+1,sum(v)+1),dtype=np.int32)
dp[0][0] = 0

for i in range(n):
  for j in range(sum(v)+1):
    if j - v[i] >= 0:
      dp[i+1][j] = min(dp[i+1][j], dp[i][j-v[i]] + w[i])

    dp[i+1][j] = min(dp[i+1][j], dp[i][j])


for i in range(sum(v)+1):
  if dp[n][i] > W:
    pass
  else:
    ans = i
print(ans)

Buggy submission (verdict: Runtime Error):
import numpy as np

n, w = list(map(int,input().split()))
w_v = [list(map(int,input().split())) for i in range(n)]
#dp = [[0] * (10**9+1) for i in range(101)]
dp = np.zeros((100,10**9+1),dtype=np.int64)

for i in range(n):
  for j in range(w+1):
    if j - w_v[i][0] >= 0:
      dp[i+1][j] = max(dp[i+1][j], dp[i][j-w_v[i][0]] + w_v[i][1])

    dp[i+1][j] = max(dp[i+1][j], dp[i][j])

print(dp[n][w])

Buggy submission (verdict: Runtime Error):
import numpy as np

n, w = list(map(int,input().split()))
w_v = [list(map(int,input().split())) for i in range(n)]
#dp = [[0] * (10**9+1) for i in range(101)]
dp = np.zeros((100,10**9+1),dtype=np.int64)

for i in range(n):
  for j in range(w+1):
    if j - w_v[i][0] >= 0:
      dp[i+1][j] = max(dp[i+1][j], dp[i][j-w_v[i][0]] + w_v[i][1])

    dp[i+1][j] = max(dp[i+1][j], dp[i][j])

print(dp[n][w])
Problem p03597 (python)

Fixed submission:
n = int(input())
a = int(input())

print(n*n-a)

Buggy submission (verdict: Runtime Error):
n = input()
a = input()

print(n*n-a)
Problem p02749 (python)

Fixed submission:
import queue

N = int(input())

e = [[] for i in range(N+1)]
for i in range(N-1):
    a, b = map(int, input().split())
    e[a].append(b)
    e[b].append(a)

even_v = []
odd_v = []

is_reached = [False] * (N+1)
q = queue.Queue()
# [start vertex, total distance]
q.put([1, 0])

# BFS
while not q.empty():
    v, d = q.get()
    if is_reached[v]:
        continue
    is_reached[v] = True
    
    if d%2 == 0:
        even_v.append(v)
    else:
        odd_v.append(v)

    for tv in e[v]:
        q.put([tv, d+1])

r1 = list(range(1, N+1, 3))
r2 = list(range(2, N+1, 3))
r3 = list(range(3, N+1, 3))

ans = [0] * N
if len(even_v) <= len(r3):
    for v in even_v:
        ans[v-1] = r3.pop()
elif len(odd_v) <= len(r3):
    for v in odd_v:
        ans[v-1] = r3.pop()
else:
    for v in even_v:
        if not r1:
            break
        ans[v-1] = r1.pop()
    for v in odd_v:
        if not r2:
            break
        ans[v-1] = r2.pop()
rv = []
rv.extend(r1)
rv.extend(r2)
rv.extend(r3)
for i, p in enumerate(ans):
    if p == 0:
        ans[i] = rv.pop()

print(' '.join(list(map(str, ans))))

Buggy submission (verdict: Runtime Error):
import queue

N = int(input())

# Adjacency matrix
e = [[] for i in range(N+1)]
for i in range(N-1):
    a, b = map(int, input().split())
    e[a].append(b)
    e[b].append(a)

even_v = []
odd_v = []

is_reached = [False] * (N+1)
q = queue.Queue()
# [start vertex, total distance]
q.put([1, 0])

# BFS
while not q.empty():
    v, d = q.get()
    if is_reached[v]:
        continue
    is_reached[v] = True
    
    if d%2 == 0:
        even_v.append(v)
    else:
        odd_v.append(v)

    for tv in e[v]:
        q.put([tv, d+1])

# Remainder lists [1..N] mod 3
r1 = list(range(1, N+1, 3))
r2 = list(range(2, N+1, 3))
r3 = list(range(3, N+1, 3))

p = [0] * N
if len(even_v) <= len(r3):
    # Embed r3 in even_v
    for v in even_v:
        p[v-1] = r3.pop()
elif len(odd_v) <= len(r3):
    # Embed r3 in odd_v
    for v in odd_v:
        p[v-1] = r3.pop()
else:
    # Embed r1 in even_v
    for v in even_v:
        if not r1:
            break
        p[v-1] = r1.pop()
    # Embed r2 in odd_v
    for v in odd_v:
        if not r2:
            break
        p[v-1] = r2.pop()

# Collect unused vertexes
rv = []
rv.extend(r1)
rv.extend(r2)
rv.extend(r3)
for i in enumerate(p):
    if p[i] == 0:
        p[i] = rv.pop()

ans = ' '.join(list(map(str, p)))
print(ans)
Problem p02824 (python)

Fixed submission:
def check_pickedup(N: int, M: int, V: int, P: int, A: list, accum_score: list, i: int) -> bool:
    th_score = A[P-1]
    if A[i] >= th_score:
        return True
    sum_surplus = (accum_score[i-1] - (accum_score[P-2] if P-2 >=0 else 0)) - ((i-1)-(P-2))*A[i]
    #print('i: ', i)
    #print('th_score: ', th_score)
    #print('sum_surplus: ', sum_surplus)
    #print('th_score - A[i]: ', th_score - A[i])
    if th_score - A[i] <= M and sum_surplus <= M*(N-V):
        return True
    return False

def main():
    N, M, V, P = map(int, input().split())
    A = list(map(int, input().split()))
    
    A.sort(reverse=True)
    
    accum_score = [A[0]] * N
    for i in range(1, N):
        accum_score[i] = accum_score[i-1] + A[i]
    
    # Reduce N-V scores each judge
    ans = 0
    was_checked = {}
    for i, a in enumerate(A):
        # Check a will be picked up
        if a in was_checked:
            ans += 1
            continue
        if check_pickedup(N, M, V, P, A, accum_score, i):
            ans += 1
            was_checked[a] = True
        else:
            break
    print(ans)

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
def check_pickedup(M: int, V: int, P: int, A: list, accum_score: list, i: int) -> bool:
    th_score = A[P-1]
    if A[i] >= th_score:
        return True
    sum_surplus = accum_score[i-1] - accum_score[P-2]
    if th_score - A[i] <= M and sum_surplus <= M*V:
        return True
    return False

def main():
    N, M, V, P = map(int, input().split())
    A = list(map(int, input().split()))
    
    A.sort(reverse=True)
    
    accum_score = [A[0]] * N
    for i in (1, N):
        accum_score[i] = accum_score[i-1] + A[i]
    
    # Reduce N-V scores each judge
    ans = 0
    was_checked = {}
    for i, a in enumerate(A):
        # Check a will be picked up
        if a in was_checked:
            ans += 1
            continue
        if check_pickedup(M, V, P, A, accum_score, i):
            ans += 1
            was_checked[a] = True
        else:
            break
    print(ans)

if __name__ == '__main__':
    main()
Problem p02971 (python)

Fixed submission:
N = int(input())
A = []
sA = []
for i in range(N):
    a = int(input())
    A.append(a)
    sA.append(a)

sA.sort(reverse=True)

f = sA[0]
s = sA[1]

for i in range(N):
    if A[i] == f:
        print(s)
    else:
        print(f)

Buggy submission (verdict: Runtime Error):
N = int(input())
A = []
sA = []
for i in range(N):
    a = int(input())
    A.append(a)
    sA.append(a)

sA.sort(reverse=True)

f = sA[0]
s = sA[1]

for i in range(N):
    if A[i] == f:
        print(s)
    else:
Problem p02985 (python)

Fixed submission:
MOD = 1000000007

N, K = map(int, input().split())
uv = {}
for i in range(N-1):
    a, b = map(int, input().split())
    if a in uv:
        uv[a].append(b)
    else:
        uv[a] = [b]

    if b in uv:
        uv[b].append(a)
    else:
        uv[b] = [a]

f = 1
for u in uv:
    if len(uv[u]) == 1:
        f = u
        break
#print(uv)

ans = 1
b = [False] * (N+1)
ulist = [[f, 0]]
while ulist:
    u, pNum = ulist[-1]
    ulist.pop()
    b[u] = True

    uPtn = K - pNum
    ans = ans*uPtn % MOD

    #print(u)
    cnt = 1
    if pNum == 0:
        cnt = 0
    if u in uv:
        for v in uv[u]:
            if b[v]:
                continue
            ulist.append([v, 1 + cnt])
            cnt += 1

print(ans)

Buggy submission (verdict: Runtime Error):
MOD = 1000000007

N, K = map(int, input().split())
uv = {}
for i in range(N-1):
    a, b = map(int, input().split())
    if a in uv:
        uv[a].append(b)
    else:
        uv[a] = [b]

    if b in uv:
        uv[b].append(a)
    else:
        uv[b] = [a]

f = 1
for u in uv:
    if len(uv[u]) == 1:
        f = u
        break
#print(uv)

ans = 1
b = [False] * (N+1)
ulist = [[f, 0]]
while ulist:
    u, pNum = ulist[-1]
    ulist.pop()
    b[u] = True

    uPtn = K - pNum
    ans = ans*uPtn % MOD

    #print(u)
    cnt = 1
    if pNum == 0:
        cnt = 0
    for v in uv[u]:
        if b[v]:
            continue
        ulist.append([v, 1 + cnt])
        cnt += 1

print(ans)

Buggy submission (verdict: Runtime Error):
MOD = 1000000007

N, K = map(int, input().split())
uv = {}
for i in range(N-1):
    a, b = map(int, input().split())
    if a in uv:
        uv[a].append(b)
    else:
        uv[a] = [b]

    if b in uv:
        uv[b].append(a)
    else:
        uv[b] = [a]

f = 1
for u in uv:
    if len(uv[u]) == 1:
        f = u
        break
#print(uv)

ans = 1
b = [False] * (N+1)
ulist = [[f, 0]]
while ulist:
    u, pNum = ulist[-1]
    ulist.pop()
    b[u] = True

    uPtn = K - pNum
    ans = ans*uPtn % MOD

    #print(u)
    cnt = 1
    if pNum == 0:
        cnt = 0
    for v in uv[u]:
        if b[v]:
            continue
        ulist.append([v, 1 + cnt])
        cnt += 1

print(ans)

Buggy submission (verdict: Runtime Error):
MOD = 1000000007

N, K = map(int, input().split())
uv = {}
for i in range(N-1):
    a, b = map(int, input().split())
    if a in uv:
        uv[a].append(b)
    else:
        uv[a] = [b]

    if b in uv:
        uv[b].append(a)
    else:
        uv[b] = [a]

f = None
for u in uv:
    if len(uv[u]) == 1:
        f = u
        break
#print(uv)

ans = 1
b = [False] * (N+1)
ulist = [[f, 0]]
while ulist:
    u, pNum = ulist[-1]
    ulist.pop()
    b[u] = True

    uPtn = K - pNum
    ans = ans*uPtn % MOD

    #print(u)
    cnt = 1
    if pNum == 0:
        cnt = 0
    for v in uv[u]:
        if b[v]:
            continue
        ulist.append([v, 1 + cnt])
        cnt += 1

print(ans)
Problem p02995 (python)

Fixed submission:
A, B, C, D = map(int, input().split())

Cprimes = {}
Dprimes = {}

tmpC = C
d = 2
while tmpC > 1:
    if tmpC % d == 0:
        if d in Cprimes:
            Cprimes[d] += 1
        else:
            Cprimes[d] = 1
        tmpC //= d
    else:
        d += 1

tmpD = D
d = 2
while tmpD > 1:
    if tmpD % d == 0:
        if d in Dprimes:
            Dprimes[d] += 1
        else:
            Dprimes[d] = 1
        tmpD //= d
    else:
        d += 1

comPrimes = {}
for k in Cprimes:
    comPrimes[k] = Cprimes[k]
for k in Dprimes:
    if k in comPrimes:
        if comPrimes[k] < Dprimes[k]:
            comPrimes[k] = Dprimes[k]
    else:
        comPrimes[k] = Dprimes[k]

comVal = 1
for k in comPrimes:
    comVal *= k**comPrimes[k]

#print(Cprimes)
#print(Dprimes)
#print(comPrimes)
#print(comVal)

CdivNum = B//C - (A-1)//C
DdivNum = B//D - (A-1)//D
comDivNum = B//comVal - (A-1)//comVal
ans = B-(A-1)
ans -= CdivNum
ans -= DdivNum
ans += comDivNum

print(ans)

Buggy submission (verdict: Runtime Error):
import math
A, B, C, D = map(int, input().split())

## b = qa + r
#b = C
#a = D
#r = C%D
#while r != 0:
#    b = a
#    a = r
#    r = b%a
#gcd = a
gcd = math.gcd(C, D)
CDlcm = C*D//gcd

total = B-(A-1)
Cmuls = B//C - (A-1)//C
Dmuls = B//D - (A-1)//D
CDmuls = B//CDlcm - (A-1)//CDlcm
ans = total - Cmuls - Dmuls + CDmuls
print(ans)

Buggy submission (verdict: Runtime Error):
import math
A, B, C, D = map(int, input().split())

## b = qa + r
#b = C
#a = D
#r = C%D
#while r != 0:
#    b = a
#    a = r
#    r = b%a
#gcd = a
gcd = math.gcd(C, D)
CDlcm = C*D//gcd

total = B-(A-1)
Cmuls = B//C - (A-1)//C
Dmuls = B//D - (A-1)//D
CDmuls = B//CDlcm - (A-1)//CDlcm
ans = total - Cmuls - Dmuls + CDmuls
print(ans)