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

Fixed submission:
import sys
sys.setrecursionlimit(10**6)
n=int(input())
edge=[[] for i in range(n)]
for i in range(n-1):
    x,y,z=map(int,input().split())
    edge[x-1].append([y-1,z])
    edge[y-1].append([x-1,z])
q,k=map(int,input().split())
dist=[-1]*n
def dfs(node,d):
    dist[node]=d
    for i in edge[node]:
        if dist[i[0]]==-1:
            dfs(i[0],d+i[1])
dfs(k-1,0)
for i in range(q):
    x,y=map(int,input().split())
    print(dist[x-1]+dist[y-1])

Buggy submission (verdict: Runtime Error):
n=int(input())
edge=[[] for i in range(n)]
for i in range(n-1):
    x,y,z=map(int,input().split())
    edge[x-1].append([y-1,z])
    edge[y-1].append([x-1,z])
q,k=map(int,input().split())
dist=[-1]*n
def dfs(node,d):
    dist[node]=d
    for i in edge[node]:
        if dist[i[0]]==-1:
            dfs(i[0],d+i[1])
dfs(k-1,0)
for i in range(q):
    x,y=map(int,input().split())
    print(dist[x-1]+dist[y-1])
Problem p03834 (python)

Fixed submission:
s=list(input())
for i in range(5):
    print(s[i],end='')
print(' ',end='')
for i in range(7):
    print(s[i+6],end='')
print(' ',end='')
for i in range(5):
    print(s[i+14],end='')

Buggy submission (verdict: Runtime Error):
s=list(input())
for i in range(5):
    print(s[i],end='')
print(' ',end='')
for i in range(7):
    print(i+6,end='')
print(' ',end='')
for i in range(5):
    print(s[i+13],end'')
Problem p03986 (python)

Fixed submission:
s=input()
a,b,n=0,0,len(s)
for i in range(n):
    if s[i]=='T':
        if a==0:
            b+=1
        else:
            a-=1
            b-=1
    else:
        a+=1
        b+=1
print(b)

Buggy submission (verdict: Runtime Error):
s=input()
a,b,n=0,0,len(s)
for i in range(n):
    if s[i]==T:
        if a==0:
            b+=1
        else:
            a-=1
    else:
        a+=1
print(n-2*a)
Problem p02598 (python)

Fixed submission:
import math
N, K = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
l = 0
r = a[-1] + 1

while r - l > 1:
    mid = (r + l) // 2
    cnt = 0
    for i in a:
        cnt += (i - 1) // mid
    if cnt <= K:
        r = mid
    else:
        l = mid
print(r)

Buggy submission (verdict: Runtime Error):
N, K = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
l = 0
r = a[-1]

while r - l > 0:
    mid = (r + l) // 2
    cnt = 0
    for i in a:
        cnt += i // mid
    if cnt <= K:
        r = mid
    else:
        l = mid + 1

print(r)
Problem p02757 (python)

Fixed submission:
N, P = map(int, input().split())
S = input()
ans = 0
rui = [0] * (N + 1)
if 10 % P == 0:
    for i in range(len(S)):
        if int(S[i]) % P == 0:
            ans += i + 1
    print(ans)
    exit()

t = 1
for i in reversed(range(N)):
    tmp = int(S[i]) * t % P
    rui[i] = (rui[i + 1] + tmp) % P
    t *= 10
    t %= P

cnt = [0] * P
for i in reversed(range(N + 1)):
    ans += cnt[rui[i]]
    cnt[rui[i]] += 1
print(ans)

Buggy submission (verdict: Runtime Error):
N, P = map(int, input().split())
S = input()
ans = 0
rui = [0] * (N + 1)
if 10 % P == 0:
    for i in range(len(S)):
        if int(S[i]) % P == 0:
            ans += i + 1
    print(ans)
    exit()

t = 1
for i in reversed(range(N)):
    tmp = int(S[i]) * t % P
    rui[i] = (rui[i + 1] + tmp) % P
    t *= 10
    t %= P

cnt = [0] * (N + 1)
for i in reversed(range(N + 1)):
    ans += cnt[rui[i]]
    cnt[rui[i]] += 1
print(ans)

Buggy submission (verdict: Runtime Error):
N, P = map(int, input().split())
S = input()
ans = 0
rui = [0] * (N + 1)
if 10 % P == 0:
    for i in range(len(S)):
        if int(S[i]) % P == 0:
            ans += i + 1
    print(ans)
    exit()

t = 1
for i in reversed(range(N)):
    tmp = int(S[i]) * t % P
    rui[i] = (rui[i + 1] + tmp) % P
    t *= 10
    t %= P

cnt = [0] * N
for i in reversed(range(N + 1)):
    ans += cnt[rui[i]]
    cnt[rui[i]] += 1
print(ans)
Problem p02862 (python)

Fixed submission:
MOD = 10 ** 9 + 7
MAX = 10 ** 6 + 1
fac = [0] * MAX
finv = [0] * MAX
inv = [0] * MAX

def CoMinit():
    fac[0] = fac[1] = 1
    finv[0] = finv[1] = 1
    inv[1] = 1
    for i in range(2, MAX):
        fac[i] = fac[i - 1] * i % MOD
        inv[i] = MOD - inv[MOD % i] * (MOD // i) % MOD
        finv[i] = finv[i - 1] * inv[i] % MOD

def COM(n, k):
   if n < k:
       return 0
   if n < 0 or k < 0:
       return 0

   return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD

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

if (X + Y) % 3:
    print(0)
    exit()
n = (X + Y) // 3
X -= n
Y -= n
if X < 0 or Y < 0:
    print(0)
    exit()
CoMinit()
ans = COM(X + Y, X)
print(ans)

Buggy submission (verdict: Runtime Error):
from sympy import Symbol, solve
x = Symbol('x')
y = Symbol('y')

tmp1 = 2*x + 3*y - 6
tmp2 = 3*x + 2*y - 12

ans = solve((tmp1, tmp2))
print(ans[x])
print(x)
Problem p02873 (python)

Fixed submission:
S = input()
slst = [[] for _ in range(len(S) + 1)]
tmp = S[0]
cnt = 0
index = 0
for i in range(len(S)):
    if tmp == S[i]:
        cnt += 1
    else:
        slst[index].append(tmp)
        slst[index].append(cnt)
        index += 1
        tmp = S[i]
        cnt = 1

slst[index].append(tmp)
slst[index].append(cnt)

dp = [0] * (len(S) + 1)
if slst[0][0] == '<':
    dp[0] = 0
else:
    dp[0] = slst[0][1]

index = 1
for i in range(len(S)):
    if slst[i] == []:
        break

    if slst[i][0] == '>':
        dp[index] = dp[index - 1] + ((slst[i][1]) * ((slst[i][1] - 1))) // 2
        index += 1
    else:
        if slst[i + 1] == []:
            dp[index] = dp[index - 1] + ((slst[i][1] + 1) * ((slst[i][1]))) // 2
            index += 1
        else:
            if slst[i][1] < slst[i + 1][1]:
                tdp = slst[i + 1][1]
                for k in range(1, slst[i][1]):
                    tdp += k
                dp[index] = dp[index - 1] + tdp
                index += 1

            else:
                dp[index] = dp[index - 1] + ((slst[i][1] + 1) * ((slst[i][1]))) // 2
                index += 1


print(dp[index - 1])

Buggy submission (verdict: Runtime Error):
S = input()
slst = [[] for _ in range(len(S))]
tmp = '<'
cnt = 0
index = 0
for i in range(len(S)):
    if tmp == S[i]:
        cnt += 1
    else:
        slst[index].append(tmp)
        slst[index].append(cnt)
        index += 1
        tmp = S[i]
        cnt = 1

slst[index].append(tmp)
slst[index].append(cnt)

ans = 0
for i in range(0, len(S), 2):
    if slst[i] == []:
        break
    elif slst[i + 1] == []:
        ans += ((slst[i][1] + 1) * ((slst[i][1]))) // 2
        break
    tmp = ((slst[i + 1][1]) * ((slst[i + 1][1] - 1))) // 2
    ans += tmp
    if slst[i + 1][1] >= slst[i][1]:
        ans += slst[i + 1][1]
    else:
        ans += ((slst[i][1] + 1) * ((slst[i][1]))) // 2

print(ans)

Buggy submission (verdict: Runtime Error):
S = input()
slst = [[] for _ in range(len(S))]
tmp = '<'
cnt = 0
index = 0
for i in range(len(S)):
    if tmp == S[i]:
        cnt += 1
    else:
        slst[index].append(tmp)
        slst[index].append(cnt)
        index += 1
        tmp = S[i]
        cnt = 1

slst[index].append(tmp)
slst[index].append(cnt)

ans = 0
for i in range(1, len(S), 2):
    if slst[i] == []:
        break
    tmp = ((slst[i][1]) * ((slst[i][1] - 1))) // 2
    ans += tmp
    if slst[i][1] >= slst[i - 1][1]:
        ans += tmp + 1
    else:
        ans += ((slst[i - 1][1] + 1) * ((slst[i - 1][1]))) // 2

print(ans)
Problem p02891 (python)

Fixed submission:
import math

S = input()
K = int(input())
cnt = 0
tmp = 1
if K == 1:
    for i in range(1, len(S)):
        if S[i - 1] == S[i]:
            tmp += 1
        elif tmp >= 2:
            cnt += tmp // 2
            tmp = 1
        else:
            tmp = 1
    if tmp >= 2:
        cnt += tmp // 2
else:
    g1 = 0
    g2 = 0
    jlst = S + S
    for i in range(1, len(S) * 2):
        if jlst[i - 1] == jlst[i]:
            tmp += 1
        elif tmp >= 2:
            cnt += tmp // 2
            tmp = 1
        else:
            tmp = 1

    if tmp >= 2:
        cnt += tmp // 2

    if cnt % 2 == 0:
        cnt = K * (cnt // 2)
    else:
        g1 = cnt // 2
        g2 = cnt - g1
        v = math.ceil(K / 2)
        if tmp >= 3 and tmp % 2 != 0 and S[0] == S[-1]:
            cnt = g1 + (g2 * (K - 1))
        else:
            cnt = (g1 * v) + (g2 * (K - v))

print(cnt)

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

cnt = 0
tmp = 1
for i in range(1, len(S)):
    if S[i - 1] == S[i]:
       tmp += 1
    elif tmp >= 2:
        cnt += tmp // 2
        tmp = 1
    else:
        tmp = 1

if tmp >= 2:
    cnt += 1

tmp1 = cnt
if S[0] == S[-1]:
    if S[-2] != S[-1]:
        cnt += 1

print((cnt * (K - 1) + tmp1))
Problem p02912 (python)

Fixed submission:
import heapq
import sys

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

if N == 1:
    for i in range(M):
        A[0] = A[0] // 2
    print(sum(A))
    sys.exit()
h = []
for i in range(N):
    heapq.heappush(h, -A[i])

for i in range(M):
    tmp = heapq.heappop(h)
    tmp = -(-tmp // 2)
    heapq.heappush(h, tmp)


print(-sum(h))

Buggy submission (verdict: Runtime Error):
from bisect import bisect_left
import sys

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

if N == 1:
    for i in range(M):
        A[0] = A[0] // 2
    print(sum(A))
    sys.exit()

A.sort(reverse=True)

index = 0
for i in range(M):
    A[index] = A[index] // 2
    aMax = A[index]
    for j in range(N):
        if aMax < A[i]:
            aMax = A[i]
            index = i
            break

print(sum(A))

Buggy submission (verdict: Runtime Error):
from bisect import bisect_left
import sys

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

if N == 1:
    for i in range(M):
        A[0] = A[0] // 2
    print(sum(A))
    sys.exit()

A.sort(reverse=True)

index = 0
for i in range(M):
    A[index] = A[index] // 2
    aMax = A[index]
    for j in range(N):
        if aMax < A[i]:
            aMax = A[i]
            index = i
            break

print(sum(A))
Problem p02935 (python)

Fixed submission:
from heapq import heappush, heappop

N = int(input())
vLst = list(map(int, input().split()))
v = []
for i in range(N):
    heappush(v, vLst[i])

ans = 0
for i in range(N - 1):
    tmp1 = heappop(v)
    tmp2 = heappop(v)
    ans = float((tmp1 + tmp2) / 2)
    heappush(v, ans)

print(v[0])

Buggy submission (verdict: Runtime Error):
from heapq import heappush, heappop N = int(input()) vLst = list(map(int, input().split())) v = [] for i in range(N): heappush(v, vLst[i]) ans = 0 for i in range(N - 1): tmp1 = heappop(v) tmp2 = heappop(v) ans = float((tmp1 + tmp2) / 2) heappush(v, ans) print(v[0])
Problem p02990 (python)

Fixed submission:
import sys
input = sys.stdin.readline
import math

def combnations(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

INF = 10 ** 9 + 7
N, K = map(int, input().split())
for i in range(1, K + 1):
    if N - K + 1 < i:
        print(0)
        continue
    ans = combnations(N - K + 1, i) * combnations(K - 1, i - 1)
    ans %= INF
    print(ans)

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
import math

def combnations(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

INF = 10 ** 9 + 7
N, K = map(int, input().split())
for i in range(1, K + 1):
    if N - K + 1 < i:
        print(0)
        complex
    ans = (combnations(N - K + 1, i) % INF) * (combnations(K - 1, i - 1) % INF)
    print(ans)
Problem p03042 (python)

Fixed submission:
S = input()
if int(S[2:]) >= 1 and int(S[2:]) <= 12:
    if int(S[:2]) <= 12 and int(S[:2]) >= 1:
        print('AMBIGUOUS')
    else:
        print('YYMM')
elif int(S[2:]) >= 13:
    if int(S[:2]) >= 1 and int(S[:2]) <= 12:
        print('MMYY')
    else:
        print('NA')
else:
    if int(S[2:]) == 0:
        if int(S[:2]) >= 1 and int(S[:2]) <= 12:
            print('MMYY')
        else:
            print('NA')
    else:
        print('NA')

Buggy submission (verdict: Runtime Error):
S = input()
if int(S[2:]) >= 1 and int(S[2:]) <= 12:
    if int(S[:2]) <= 12 and int(S[:2]) >= 1:
        print('AMBIGUOUS')
    else:
        print('YYMM')
elif int(S[2:]) >= 13:
    if int(S[:2]) >= 1 and int(S[:2]) <= 12:
        print('MMYY')
    else:
        print('NA')
elif int(S[2:]) == 0:
    if int(S[:2]) >= 1 and int(S[:2] <= 12):
        print('MMYY')
    else:
        print('NA')
else:
    print('NA')

Buggy submission (verdict: Runtime Error):
S = input()
if int(S[2:]) >= 1 and int(S[2:]) <= 12:
    if int(S[:2]) <= 12 and int(S[:2]) >= 1:
        print('AMBIGUOUS')
    else:
        print('YYMM')
elif int(S[2:]) >= 13:
    if int(S[:2]) >= 1 and int(S[:2]) <= 12:
        print('MMYY')
    else:
        print('NA')
else:
    if int(S[:2]) >= 1 and int(S[:2] <= 12):
        print('MMYY')
    else:
        print('NA')
Problem p03107 (python)

Fixed submission:
inData = str(input())
ansList = []
dataLen = len(inData)
zeroCnt = 0
oneCnt = 0
for i in range(0, dataLen):
    if inData[i] == '0':
        zeroCnt += 1
    else:
        oneCnt += 1

print(2 * min([zeroCnt, oneCnt]))

Buggy submission (verdict: Runtime Error):
inData = str(input())
ansList = []
dataLen = len(inData)
rpLen = len(inData)
for i in range(0, dataLen):
    for j in range(1, rpLen + 1):
        if inData[-j] != inData[-j - 1]:
            ansList.append(inData[-j])
            ansList.append(inData[-j - 1])
            tmp1 = str(inData[0:-j - 1])
            tmp2 = ''
            if -j + 1 != 0:
                tmp2 = str(inData[-j + 1:])
            reData = tmp1 + tmp2
            if reData == '[]':
                inData = '[]'
            else:
                inData = reData
            break
        else:
            if rpLen == j + 1:
                break
    if inData == '[]':
        break
    rpLen = len(inData)
    if rpLen < 2:
        break

print(len(ansList))
Problem p03127 (python)

Fixed submission:
def yuk(a, b):
    ans = int(a / b)
    amari = a % b
    if amari != 0:
        ans = yuk(b, amari)
    else:
        ans = b
    return ans

N = int(input())
alst = list(map(int, input().split()))
ans = alst[0]
for i in range(1, N):
    if ans > alst[i]:
        ans = yuk(ans, alst[i])
    else:
        ans = yuk(alst[i], ans)
print(ans)

Buggy submission (verdict: Runtime Error):
import math
def yuk(a, b):
    ans = int(a / b)
    amari = a % b
    if amari != 0:
        ans = yuk(b, amari)
    else:
        ans = b
    return ans


N = int(input())
alst = list(map(int, input().split()))
ans = alst[0]
for i in range(1, N):
    if ans > alst[i]:
        ans = math.gcd(ans, alst[i])
    else:
        ans = math.gcd(alst[i], ans)
print(ans)
Problem p03221 (python)

Fixed submission:
N, M = map(int, input().split())
pLst = [list(map(int, input().split())) for i in range(M)]

for i in range(len(pLst)):
    pLst[i].append(i)
nLst = [''] * N

for i in range(N):
    nLst[i] = str(i + 1).zfill(6)
tLst = sorted(pLst, key=lambda x: (x[0], x[1]))
ansLst = [''] * M
tmp = nLst[tLst[0][0] - 1]
index = 1
for i in range(M):
    if tmp == nLst[tLst[i][0] - 1]:
        ansLst[tLst[i][2]] = nLst[tLst[i][0] - 1] + str(index).zfill(6)
        index += 1
    else:
        index = 1
        tmp = nLst[tLst[i][0] - 1]
        ansLst[tLst[i][2]] = nLst[tLst[i][0] - 1] + str(index).zfill(6)
        index += 1

for i in ansLst:
    print(i)

Buggy submission (verdict: Runtime Error):
N, M = map(int, input().split())
pLst = [list(map(int, input().split())) for i in range(M)]

for i in range(len(pLst)):
    pLst[i].append(i)
nLst = [''] * N

for i in range(N):
    nLst[i] = str(i + 1).zfill(6)
tLst = sorted(pLst, key=lambda x: (x[0], x[1]))
ansLst = [''] * M
tmp = nLst[tLst[i][0] - 1]
index = 1

Buggy submission (verdict: Runtime Error):
N, M = map(int, input().split())
pLst = [list(map(int, input().split())) for i in range(M)]

for i in range(len(pLst)):
    pLst[i].append(i)
nLst = [''] * N

for i in range(N):
    nLst[i] = str(i + 1).zfill(6)
tLst = sorted(pLst, key=lambda x: (x[0], x[1]))
ansLst = [''] * M
tmp = nLst[tLst[i][0] - 1]
index = 1
for i in range(M):
    if tmp == nLst[tLst[i][0] - 1]:
        ansLst[tLst[i][2]] = nLst[tLst[i][0] - 1] + str(index).zfill(6)
        index += 1
    else:
        index = 1
        tmp == nLst[tLst[i][0] - 1]
        ansLst[tLst[i][2]] = nLst[tLst[i][0] - 1] + str(index).zfill(6)

for i in ansLst:
    print(i)
Problem p03805 (python)

Fixed submission:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

N, M = map(int, input().split())
tLst = [[] for _ in range(N)]
vLst = [0] * N

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

def dfs(n, index, v, ans):
    vLst[index] = 1
    if n == N - 1:
        ans += 1
        return ans
    for i in tLst[index]:
        if v == i:
            continue
        elif vLst[i] == 1:
            continue
        ans = dfs(n + 1, i, index, ans)
        vLst[i] = 0
    return ans
print(dfs(0, 0, 0, 0))

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

N, M = map(int, input().split())
tLst = [[] for _ in range(M)]
vLst = [0] * N

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

def dfs(n, index, v, ans):
    vLst[index] = 1
    if n == N - 1:
        ans += 1
        return ans
    for i in tLst[index]:
        if v == i:
            continue
        elif vLst[i] == 1:
            continue
        ans = dfs(n + 1, i, index, ans)
        vLst[i] = 0
    return ans
print(dfs(0, 0, 0, 0))
Problem p02623 (python)

Fixed submission:
def main():
    N, M, K = map(int, input().split())
    *A, = map(int, input().split())
    *B, = map(int, input().split())

    def accumulate(a):
        s = 0
        yield s
        for x in a:
            s += x
            yield s

    *P, = accumulate(A)
    *Q, = accumulate(B)

    ans = 0
    j = len(Q) - 1
    for i, x in enumerate(P):
        while j > 0 and x + Q[j] > K:
            j -= 1
        if x + Q[j] <= K:
            ans = max(ans, i + j)
    print(ans)


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
def main():
    N, M, K = map(int, input().split())
    *A, = map(int, input().split())
    *B, = map(int, input().split())

    ai = 0
    t = 0
    cnt = 0
    while True:
        if ai < len(A) and t + A[ai] <= K:
            t += A[ai]
            ai += 1
            cnt += 1
            continue
        break

    ans = cnt
    bi = 0
    while True:
        if bi < len(B):
            while ai >= 0 and t + B[bi] > K:
                t -= A[ai]
                ai -= 1
                cnt -= 1
            if t + B[bi] <= K:
                t += B[bi]
                bi += 1
                cnt += 1
                ans = max(ans, cnt)
                continue
        break

    print(ans)


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

Fixed submission:
def main():
    from collections import defaultdict, deque
    import sys
    input = sys.stdin.readline

    N, M, K = map(int, input().split())

    g = tuple(set() for _ in range(N))
    direct_friend = [0] * N
    for _ in range(M):
        a, b = (int(x) - 1 for x in input().split())

        g[a].add(b)
        g[b].add(a)

        direct_friend[a] += 1
        direct_friend[b] += 1

    bk = tuple(set() for _ in range(N))
    for _ in range(K):
        c, d = (int(x) - 1 for x in input().split())
        bk[c].add(d)
        bk[d].add(c)

    dq = deque()
    checked = [-1] * N
    for v in range(N):
        if ~checked[v]:
            continue

        dq.append((v, v))  # v,root
        checked[v] = v
        while dq:
            v, root = dq.pop()
            for u in g[v]:
                if ~checked[u]:
                    continue
                checked[u] = root
                dq.append((u, root))

    ctr = defaultdict(int)
    for v in range(N):
        root = checked[v]
        ctr[root] += 1

    ret = []
    for v in range(N):
        root = checked[v]
        t = ctr[root] - direct_friend[v] - 1
        for u in bk[v]:
            if checked[u] == root:
                # 同じ連結成分に属するがブロック関係
                # for二重なので間に合わないと思っていたが総数K<=10**5で間に合う
                t -= 1
        ret.append(t)

    print(*ret)


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
def main():
    import sys
    input = sys.stdin.readline

    N, M, K = map(int, input().split())

    fs = tuple(set() for _ in range(N))
    for _ in range(M):
        a, b = (int(x) - 1 for x in input().split())
        fs[a].add(b)
        fs[b].add(a)

    bs = tuple(set() for _ in range(N))
    for _ in range(K):
        a, b = (int(x) - 1 for x in input().split())
        bs[a].add(b)
        bs[b].add(a)

    roots = [-1] * N
    sizes = [-1] * N

    def dfs(v):
        """
        return: 連結成分のサイズを返す
        roots: 連結成分の各成分に根のindexを割り当てる
        """
        sz = 1
        for u in fs[v]:
            if ~roots[u]: continue
            roots[u] = roots[v]
            sz += dfs(u)
        return sz

    for v in range(N):
        if ~roots[v]: continue
        roots[v] = v
        sizes[v] = dfs(v)

    ans = []
    for v in range(N):
        r = roots[v]
        res = sizes[r] - 1 - len(fs[v]) - sum(1 for u in bs[v] if roots[u] == r)
        ans.append(res)

    print(*ans)


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

Fixed submission:
def main():
    from functools import reduce

    def gcd(a, b):
        return a if b == 0 else gcd(b, a % b)

    def lcm(a, b):
        return a * b // gcd(a, b)

    mod = 10 ** 9 + 7

    n = int(input())
    *a, = map(int, input().split())

    L = reduce(lcm, a)

    ret = 0
    for x in a:
        ret = (ret + pow(x, mod - 2, mod)) % mod
    ret = ret * L % mod
    print(ret)


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
class Sieve:
    """区間[2,n]の値を素因数分解する"""

    def __init__(self, n=1):
        primes = []
        f = [0] * (n + 1)
        f[0] = f[1] = -1
        for i in range(2, n + 1):  # 素数を探す
            if f[i]: continue
            primes.append(i)
            f[i] = i  # 素数には自身を代入
            for j in range(i * i, n + 1, i):  # 合成数に素因数を書き込む
                if not f[j]:
                    f[j] = i  # 最小の素因数を代入
        self.primes = primes
        self.f = f

    def is_prime(self, x) -> bool:  # 素数判定
        return self.f[x] == x

    def factor_list(self, x) -> list:  # 素因数分解の昇順リスト, [2,2,3,5,...]
        res = []
        while x != 1:
            res.append(self.f[x])
            x //= self.f[x]
        return res

    def factor(self, x) -> list:  # 素因数分解の頻度リスト, [[2,x],[3,y],[5,z],...]
        fl = self.factor_list(x)
        if not fl: return []
        res = [[fl[0], 0]]
        for p in fl:
            if res[-1][0] == p:
                res[-1][1] += 1
            else:
                res.append([p, 1])
        return res


def main():
    from collections import defaultdict

    MOD = 10 ** 9 + 7
    A_MX = 10 ** 6

    N = int(input())
    *a, = map(int, input().split())

    s = Sieve(n=A_MX)

    lcm = defaultdict(int)

    factors = []
    for x in a:
        f = [0] * (A_MX + 1)
        for prime, cnt in s.factor(x):
            lcm[prime] = max(lcm[prime], cnt)
            f[prime] = cnt
        factors.append(f)

    ans = 0
    for f in factors:
        t = 1
        for prime, cnt in lcm.items():
            power = max(0, cnt - f[prime])
            t *= pow(prime, power, MOD)
            t %= MOD
        ans += t
        ans %= MOD
    print(ans)


if __name__ == '__main__':
    main()