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

Fixed submission:

n = int(input())
S = [list(input()) for i in range(n)]

M = [[] for i in range(n)]
for i in range(n):
    for j in range(n):
        if S[i][j] == "1":
            M[i].append(j)
ans = -1
owa = 0
for i in range(n):
    a0 = -1
    Q = [[i, 0]]
    st = 0
    D = [-1] * n
    while st < len(Q):
        a = Q[st][0]
        d = Q[st][1]
        D[a] = d
        for x in M[a]:
            if D[x] == -1:
                Q.append([x, d+1])
                D[x] = d+1
            elif D[x] != d-1 and D[x] != d+1:
                owa = 1
                break
        if owa == 1:
            break
        st += 1
    if owa == 0:
        a0 = max(D)+1
    ans = max(ans, a0)

print(ans)

Buggy submission (verdict: Runtime Error):

n = int(input())
S = [list(input()) for i in range(n)]

M = [[] for i in range(n)]
for i in range(n):
    for j in range(n):
        if S[i][j] == "1":
            M[i].append(j)
ans = -1
owa = 0
for i in range(n):
    a0 = -1
    Q = [[0] * 2 for i in range(n*n+3)]
    Q[0] = [i, 0]
    st = 0
    ri = 1
    D = [-1] * n
    while st < ri:
        a = Q[st][0]
        d = Q[st][1]
        D[a] = d
        for x in M[a]:
            if D[x] == -1:
                Q[ri] = [x, d+1]
                ri += 1
            elif D[x] != d-1 and D[x] != d+1:
                owa = 1
                break
        if owa == 1:
            break
        st += 1
    if owa == 0:
        a0 = max(D)+1
    ans = max(ans, a0)

print(ans)

Buggy submission (verdict: Runtime Error):

n = int(input())
S = [list(input()) for i in range(n)]

M = [[] for i in range(n)]
for i in range(n):
    for j in range(n):
        if S[i][j] == "1":
            M[i].append(j)
ans = -1
owa = 0
for i in range(n):
    a0 = -1
    Q = [[0] * 2 for i in range(n+3)]
    Q[0] = [i, 0]
    st = 0
    ri = 1
    D = [-1] * n
    while st < ri:
        a = Q[st][0]
        d = Q[st][1]
        D[a] = d
        for x in M[a]:
            if D[x] == -1:
                Q[ri] = [x, d+1]
                ri += 1
            elif D[x] != d-1 and D[x] != d+1:
                owa = 1
                break
        if owa == 1:
            break
        st += 1
    if owa == 0:
        a0 = max(D)+1
    ans = max(ans, a0)

print(ans)
Problem p02948 (python)

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

C = [[] for i in range(10**5+10)]

for i in range(n):
    C[B[i][0]].append(B[i][1])
for i in range(len(C)):
    C[i].sort(reverse=1)

#print(C[:10])
notst=1
st=0
ans = 0

Bl = [0 for i in range(10**4+2)]
for i in range(1, m+1):
    for j in range(len(C[i])):
        Bl[C[i][j]] += 1
    if notst == 1 and len(C[i]) > 0:
        notst = 0
        st = C[i][0]
    if notst == 0 and len(C[i]) > 0:
        if st < C[i][0]:
            st = C[i][0]
    while st > 0 and Bl[st] == 0:
        st -= 1
    ans += st
    # print(ans)
    # print(Bl[:10])
    Bl[st] -= 1

print(ans)

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

C = [[] for i in range(10**4+10)]

for i in range(n):
    C[B[i][0]].append(B[i][1])
for i in range(len(C)):
    C[i].sort(reverse=1)

#print(C[:10])
notst=1
st=0
ans = 0

Bl = [0 for i in range(10**4+2)]
for i in range(1, m+1):
    for j in range(len(C[i])):
        Bl[C[i][j]] += 1
    if notst == 1 and len(C[i]) > 0:
        notst = 0
        st = C[i][0]
    if notst == 0 and len(C[i]) > 0:
        if st < C[i][0]:
            st = C[i][0]
    while st > 0 and Bl[st] == 0:
        st -= 1
    ans += st
    # print(ans)
    # print(Bl[:10])
    Bl[st] -= 1

print(ans)

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

C = [[] for i in range(10*5+2)]

for i in range(n):
    C[B[i][0]].append(B[i][1])
for i in range(len(C)):
    C[i].sort(reverse=1)

#print(C[:10])
notst=1
st=0
ans = 0

Bl = [0 for i in range(10**4+2)]
for i in range(1, m+1):
    for j in range(len(C[i])):
        Bl[C[i][j]] += 1
    if notst == 1 and len(C[i]) > 0:
        notst = 0
        st = C[i][0]
    if notst == 0 and len(C[i]) > 0:
        if st < C[i][0]:
            st = C[i][0]
    while st > 0 and Bl[st] == 0:
        st -= 1
    ans += st
    # print(ans)
    # print(Bl[:10])
    Bl[st] -= 1

print(ans)
Problem p03033 (python)

Fixed submission:
import sys
input = sys.stdin.readline
n,q = map(int,input().split())
A = [list(map(int,input().split())) for i in range(n)]
D = [int(input()) for i in range(q)]

E = []
F = {}
for i in range(n):
    s = A[i][0]
    t = A[i][1]
    x = A[i][2]
    F[x] = 0
    E.append((s-x, x, 1))
    E.append((t-x, x, -1))
E.append((10**10, 10**10, 1))

E.sort()

# print(E)

from heapq import heappush, heappop
Q = []

j = 0
for i in range(q):
    # print(F,j)
    while E[j][0] <= D[i]:
        if E[j][2] == 1:
            F[E[j][1]] += 1
            heappush(Q, E[j][1])
        else:
            F[E[j][1]] -= 1
        j += 1
    
    while len(Q) > 0 and F[Q[0]] == 0:
        _ = heappop(Q)
    if len(Q) == 0:
        print(-1)
    else:
        print(Q[0])

Buggy submission (verdict: Runtime Error):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
 
from heapq import heappush, heappop
from collections import defaultdict
 
n,q = map(int,readline().split())
data = list(map(int,read().split()))
A = data[:3*n]
D = data[3*n:]

E = []
F = {}
for i in range(n):
    s = A[i][0]
    t = A[i][1]
    x = A[i][2]
    F[x] = 0
    E.append((s-x, x, 1))
    E.append((t-x, x, -1))
E.append((10**10, 10**10, 1))

E.sort()

# print(E)

from heapq import heappush, heappop
Q = []

j = 0
for i in range(q):
    # print(F,j)
    while E[j][0] <= D[i]:
        if E[j][2] == 1:
            F[E[j][1]] += 1
            heappush(Q, E[j][1])
        else:
            F[E[j][1]] -= 1
        j += 1
    
    while len(Q) > 0 and F[Q[0]] == 0:
        _ = heappop(Q)
    if len(Q) == 0:
        print(-1)
    else:
        print(Q[0])

Buggy submission (verdict: Runtime Error):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
 
from heapq import heappush, heappop
from collections import defaultdict
 
n,q = map(int,readline().split())
data = list(map(int,read().split()))
A = data[:3*n]
D = data[3*n:]

E = []
F = {}
for i in range(n):
    s = A[i][0]
    t = A[i][1]
    x = A[i][2]
    F[x] = 0
    E.append((s-x, x, 1))
    E.append((t-x, x, -1))
E.append((10**10, 10**10, 1))

E.sort()

# print(E)

from heapq import heappush, heappop
Q = []

j = 0
for i in range(q):
    # print(F,j)
    while E[j][0] <= D[i]:
        if E[j][2] == 1:
            F[E[j][1]] += 1
            heappush(Q, E[j][1])
        else:
            F[E[j][1]] -= 1
        j += 1
    
    while len(Q) > 0 and F[Q[0]] == 0:
        _ = heappop(Q)
    if len(Q) == 0:
        print(-1)
    else:
        print(Q[0])
Problem p03137 (python)

Fixed submission:
n,m=map(int,input().split())
X=list(map(int,input().split()))
X.sort()

if m==1 or n>=m :
    print(0)
else :

    sa=[]
    for i in range(m-1):
        sa.append(X[i+1]-X[i])


    sa.sort(reverse=1)


    jo=[]
    for i in range(n-1):
        jo.append(sa[i])


    print(X[m-1]-X[0]-sum(jo))

Buggy submission (verdict: Runtime Error):
n,m=map(int,input().split())
X=list(map(int,input().split()))
X.sort()

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

    sa=[]
    for i in range(m-1):
        sa.append(X[i+1]-X[i])


    sa.sort(reverse=1)


    jo=[]
    for i in range(n-1):
        jo.append(sa[i])


    print(X[m-1]-X[0]-sum(jo))
Problem p03242 (python)

Fixed submission:
n=input()

ans=""
for i in range(len(n)):
  if n[i]=="1" :
    ans+="9"
  elif n[i]=="9" :
    ans+="1"

print(ans)

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

A=[[0,i] for i in range(10**5+3)]
B=[[0,i] for i in range(10**5+3)]

for i in range(0,n,2):
    A[V[i]][0]+=1
for i in range(1,n,2):
    B[V[i]][0]+=1

A.sort(reverse=1)
B.sort(reverse=1)

#print(A[:10])
#print(B[:10])

Ma=[]
Mb=[]

ma=A[0][0]
mb=B[0][0]
i=0
j=0
while A[i][0]==ma :
    Ma.append(A[i][1])
    i+=1

while B[j][0]==mb :
    Mb.append(B[j][1])
    j+=1

if Ma==Mb :
    print(min(n-ma-B[j][0],n-mb-A[i][0]))
else :
    print(n-ma-mb)
Problem p03745 (python)

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

# i = 0
# def w(i):
#     # print(i)
#     if A[i] - A[i-1] == 0:
#         return 0
#     else:
#         return (A[i]-A[i-1]) / abs(A[i]-A[i-1])
# v = w(1)
# ans = 1

# i = 2
# while i < n:
#     print(i)
#     nv = w(i)
#     if nv * v < 0:
#         ans += 1
#         i += 2
#         v = 0
#         if i == n:
#             print("p")
#             ans += 1
#     else:
#         i += 1
#     if nv != 0:
#         v = nv

B = []
for i in range(1, n):
    if A[i] != A[i-1]:
        B.append(int((A[i]-A[i-1]) / abs(A[i]-A[i-1])))
# print(B)

ans = 1
t = 0
m = 0
for i in range(len(B)):
    # print(i)
    if B[i] * t < 0:
        # print("p")
        ans += 1
        t = 0
        if i == len(B):
            ans += 1
    else:
        t = B[i]

print(ans)

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

i = 0
def w(i):
    # print(i)
    if A[i] - A[i-1] == 0:
        return 0
    else:
        return (A[i]-A[i-1]) / abs(A[i]-A[i-1])
v = w(1)
ans = 1

i = 2
while i < n:
    nv = w(i)
    if nv * v < 0:
        ans += 1
        i += 2
        if i == n+1:
            ans += 1
    else:
        i += 1
    if nv != 0:
        v = nv
print(ans)

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

v = (A[1]-A[0]) / abs(A[1]-A[0])
ans = 1
i = 2
while i < n:
    if (A[i] - A[i-1]) * v < 0:
        v *= -1
        ans += 1
        i += 2
        if i == n+1:
            ans += 1
    else:
        i += 1
print(ans)
Problem p03805 (python)

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

import itertools

M = [[0] * (n+1) for i in range(n+1)]
for i in range(m):
    M[A[i][0]][A[i][1]] = 1
    M[A[i][1]][A[i][0]] = 1

B = list(itertools.permutations(range(2, n+1)))

#print(B)

ans = 0
for i in range(len(B)):
    pt = 0
    if M[1][B[i][0]] == 1:
        pt += 1
    else:
        continue

    for j in range(len(B[i])-1):
        if M[B[i][j]][B[i][j+1]] == 1:
            pt += 1
    # print(B[i],pt)
    if pt == n-1:
        ans += 1
print(ans)

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

import itertools

M = [[0] * (n+1) for i in range(n+1)]
for i in range(m):
    M[A[i][0]][A[i][1]] = 1
    M[A[i][1]][A[i][0]] = 1

B = list(itertools.permutations(range(2, n+1)))

#print(B)

ans = 0
for i in range(len(B)):
    pt = 0
    if M[1][B[i][0]] == 1:
        pt += 1
    else:
        continue

    for j in range(len(B[i])-1):
        if M[B[i][j]][B[i][j+1]] == 1:
            pt += 1
    # print(B[i],pt)
    if pt == n-1:
        ans += 1
print(ans)
Problem p02550 (python)

Fixed submission:


def solve(N,X,M):
    if X == 0:
        return 0
    memo = [-1]*(M+3)
    acc = [0]*(M+4)
    x = X
    for i in range(M):
        if memo[x] >= 0:
            break
        acc[i+1] = acc[i]+x
        memo[x] = i
        x = (x*x)%M


    end = i
    start = memo[x]
    cycle = end - start

    if N < end:
        return acc[N]

    cycle_sum = acc[end]-acc[start]

    res = acc[start]

    N -= start
    res += (N//cycle)*cycle_sum
    res += acc[start+(N%cycle)]-acc[start]
    return res


N,X,M = map(int,input().split())
print(solve(N,X,M))

Buggy submission (verdict: Runtime Error):


def solve(N,X,M):
    memo = [-1]*(M+3)
    acc = [0]*(M+4)
    x = X
    for i in range(M):
        if memo[x] >= 0:
            break
        acc[i+1] = acc[i]+x
        memo[x] = i
        x = (x*x)%M


    end = i
    start = memo[x]
    cycle = end - start

    if N < end:
        return acc[N]

    cycle_sum = acc[end]-acc[start]

    res = acc[start]

    N -= start
    res += (N//cycle)*cycle_sum
    res += acc[start+(N%cycle)]-acc[start]
    return res


N,X,M = map(int,input().split())
print(solve(N,X,M))

Buggy submission (verdict: Runtime Error):


def solve(N,X,M):
    memo = [-1]*M
    acc = [0]*(M+1)
    x = X%M
    for i in range(M):
        if memo[x] >= 0:
            break
        acc[i+1] = acc[i]+x
        memo[x] = i
        x = (x*x)%M


    end = i
    start = memo[x]
    cycle = end - start

    if N < end:
        return acc[N]+(X - X%M)

    cycle_sum = acc[end]-acc[start]

    res = acc[start]+(X - X%M)

    N -= start
    res += (N//cycle)*cycle_sum
    res += acc[start+(N%cycle)]-acc[start]
    return res

N,X,M = map(int,input().split())
print(solve(N,X,M))
Problem p02625 (python)

Fixed submission:

def factorials_with_inv(k,mod):
    """
    0! ... k! とそれらのmod逆元を求めて返す
    """
    fac = [1]*(k+1)
    inv = [1]*(k+1)
    t = 1
    for i in range(1,k+1):
        t *= i
        t %= mod
        fac[i] = t

    t = pow(t,-1,mod)
    for i in reversed(range(1,k+1)):
        inv[i] = t
        t *= i
        t %= mod

    return fac, inv

MOD = 10**9+7

def solve(N,M):
    fac,inv = factorials_with_inv(max(N,M), MOD)

    # (M-k)!/(k!(N-k)!) * N!M!/((M-N)!^2)

    res = 0
    sign = True
    for k in range(N+1):
        t = fac[M-k]*inv[k]*inv[N-k]
        if sign:
            res += t
        else:
            res -= t
        res %= MOD
        sign = not sign

    res *= fac[N]*fac[M]*inv[M-N]**2
    res %= MOD
    return res

from itertools import permutations

def naive(N,M):
    res = 0
    for A in permutations(range(M),N):
        for B in permutations(range(M),N):
            res += all(a != b for a,b in zip(A,B))
    return res%MOD

if __name__ == '__main__':
    N,M = map(int,input().split())

    # print(naive(N,M))
    print(solve(N,M))

Buggy submission (verdict: Runtime Error):

def factorials_with_inv(k,mod):
    """
    0! ... k! とそれらのmod逆元を求めて返す
    """
    fac = [1]*(k+1)
    inv = [1]*(k+1)
    t = 1
    for i in range(1,k+1):
        t *= i
        t %= mod
        fac[i] = t

    t = pow(t,-1,mod)
    for i in reversed(range(1,k+1)):
        inv[i] = t
        t *= i
        t %= mod

    return fac, inv

MOD = 10**9+7

def solve(N,M):
    fac,inv = factorials_with_inv(max(N,M), MOD)

    # (M-k)!/(k!(N-k)!) * N!M!/((M-N)!^2)

    res = 0
    sign = True
    for k in range(N+1):
        t = fac[M-k]*inv[k]*inv[N-k]
        if sign:
            res += t
        else:
            res -= t
        res %= MOD
        sign = not sign

    res *= fac[N]*fac[M]*inv[M-N]**2
    res %= MOD
    return res

from itertools import permutations

def naive(N,M):
    res = 0
    for A in permutations(range(M),N):
        for B in permutations(range(M),N):
            res += all(a != b for a,b in zip(A,B))
    return res%MOD

if __name__ == '__main__':
    N,M = map(int,input().split())

    # print(naive(N,M))
    print(solve(N,M))
Problem p02793 (python)

Fixed submission:
from collections import Counter, defaultdict
# 拡張ユークリッド互除法
# ax + by = gcd(a,b)の最小整数解を返す
def egcd(a, b):
    if a == 0:
        return (b, 0, 1)
    else:
        g, y, x = egcd(b % a, a)
        return (g, x - (b // a) * y, y)


# mを法とするaの乗法的逆元
def modinv(a, m):
    g, x, y = egcd(a, m)
    if g != 1:
        raise Exception('modular inverse does not exist')
    else:
        return x % m


# 素因数分解
def prime_factors(n):
    i = 2
    while i * i <= n:
        if n % i:
            i += 1
        else:
            n //= i
            yield i
    if n > 1:
        yield n

# mを法とするときのa^n
def modpow(a,n,m):
    res = 1
    t = a
    while n:
        if n%2:
            res = (res*t)%m
        t = (t*t)%m
        n //= 2
    return res

def lcm_mod(nums, mod):
    p = defaultdict(int)
    for n in nums:
        c = Counter(prime_factors(n))
        for v,cnt in c.items():
            p[v] = max(p[v],cnt)

    res = 1
    for v,cnt in p.items():
        res *= modpow(v,cnt,mod)
        res %= mod
    return res


MOD = 10**9+7

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

s = sum(modinv(a,MOD) for a in A)%MOD

s *= lcm_mod(A, MOD)
print(s%MOD)

Buggy submission (verdict: Runtime Error):
# 拡張ユークリッド互除法
# ax + by = gcd(a,b)の最小整数解を返す
def egcd(a, b):
    if a == 0:
        return (b, 0, 1)
    else:
        g, y, x = egcd(b % a, a)
        return (g, x - (b // a) * y, y)


# mを法とするaの乗法的逆元
def modinv(a, m):
    g, x, y = egcd(a, m)
    if g != 1:
        raise Exception('modular inverse does not exist')
    else:
        return x % m

def lcm(num1, num2): 
    if(num1>num2): 
        num = num1 
        den = num2 
    else: 
        num = num2 
        den = num1 
    rem = num % den 
    while(rem != 0): 
        num = den 
        den = rem 
        rem = num % den 
    gcd = den 
    lcm = int(int(num1 * num2)/int(gcd)) 
    return lcm 


MOD = 10**9+7

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

s = sum(modinv(a,MOD) for a in A)%MOD


m = A[0]
for a in A:
    m = lcm(m,a)

s *= m
print(s%MOD)
Problem p02834 (python)

Fixed submission:
N,U,V = map(int,input().split())
U,V = U-1, V-1

adj = [list() for _ in range(N)]

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



dist_u = [None]*N
dist_u[U] = 0
stack = [U]

while stack:
    x = stack.pop()
    d = dist_u[x]+1
    for y in adj[x]:
        if dist_u[y] is None:
            dist_u[y] = d
            stack.append(y)


dist_v = [None]*N
dist_v[V] = 0
stack = [V]

while stack:
    x = stack.pop()
    d = dist_v[x]+1
    for y in adj[x]:
        if dist_v[y] is None:
            dist_v[y] = d
            stack.append(y)

n = max(b for a,b in zip(dist_u, dist_v) if a < b)
m = min((b for a,b in zip(dist_u, dist_v) if a <= b), default=n)

print(max(n,m)-1)

Buggy submission (verdict: Runtime Error):
N,U,V = map(int,input().split())
U,V = U-1, V-1

adj = [list() for _ in range(N)]

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



dist_u = [None]*N
dist_u[U] = 0
stack = [U]

while stack:
    x = stack.pop()
    d = dist_u[x]+1
    for y in adj[x]:
        if dist_u[y] is None:
            dist_u[y] = d
            stack.append(y)


dist_v = [None]*N
dist_v[V] = 0
stack = [V]

while stack:
    x = stack.pop()
    d = dist_v[x]+1
    for y in adj[x]:
        if dist_v[y] is None:
            dist_v[y] = d
            stack.append(y)

m,n = max((a,b) for a,b in zip(dist_u, dist_v) if a < b)
print(n-(m-n)%2)N,U,V = map(int,input().split())
U,V = U-1, V-1

adj = [list() for _ in range(N)]

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



dist_u = [None]*N
dist_u[U] = 0
stack = [U]

while stack:
    x = stack.pop()
    d = dist_u[x]+1
    for y in adj[x]:
        if dist_u[y] is None:
            dist_u[y] = d
            stack.append(y)


dist_v = [None]*N
dist_v[V] = 0
stack = [V]

while stack:
    x = stack.pop()
    d = dist_v[x]+1
    for y in adj[x]:
        if dist_v[y] is None:
            dist_v[y] = d
            stack.append(y)

m,n = max((a,b) for a,b in zip(dist_u, dist_v) if a < b)
print(n-(m-n)%2)
Problem p02862 (python)

Fixed submission:

from functools import reduce
def comb(n, max_k, mod):
    """
    (n,k) := n個からk個選ぶ組み合わせ
    k = 0~max_Kまでを計算して返す
    """
    res = [1]*(max_k+1)
    t = 1
    for i in range(max_k+1):
        res[i] *= t
        t *= n-i
        t %= mod

    n = reduce(lambda x,y: (x*y)%mod, range(1,max_k+1), 1)
    n = pow(n, mod-2, mod)

    for i in reversed(range(max_k+1)):
        res[i] *= n
        res[i] %= mod
        n *= i
        n %= mod
    return res

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

d = X+Y

if d%3 != 0:
    print(0)
else:
    n = d//3
    if X < n or 2*n < X:
        res = 0
    else:
        res = comb(n,X-n, 10**9+7)[X-n]
    print(res)

Buggy submission (verdict: Runtime Error):

from functools import reduce
def comb(n, max_k, mod):
    """
    (n,k) := n個からk個選ぶ組み合わせ
    k = 0~max_Kまでを計算して返す
    """
    res = [1]*(max_k+1)
    t = 1
    for i in range(max_k+1):
        res[i] *= t
        t *= n-i
        t %= mod

    n = reduce(lambda x,y: (x*y)%mod, range(1,max_k+1), 1)
    n = pow(n, mod-2, mod)

    for i in reversed(range(max_k+1)):
        res[i] *= n
        res[i] %= mod
        n *= i
        n %= mod
    return res

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

d = X+Y

if d%3 != 0:
    print(0)
else:
    n = d//3

    res = comb(n,X-n, 10**9+7)[X-n]
    print(res)
Problem p03201 (python)

Fixed submission:
from collections import defaultdict

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

cnt = defaultdict(int)

for a in A:
    cnt[a] += 1
res = 0
for x in reversed(A):
    y = (1 << x.bit_length()) - x
    if x == y:
        k = cnt[x]//2
    else:
        k = min(cnt[x], cnt[y])
    res += k
    cnt[x] -= k
    cnt[y] -= k

print(res)

Buggy submission (verdict: Runtime Error):
from collections import defaultdict

def adj(v):
    t = 1
    while t <= 2*10**9:
        if t > v:
            yield t-v
        t *= 2

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

cnt = defaultdict(int)
for a in A:
    cnt[a] += 1

degree = defaultdict(int)
for v in cnt.keys():
    for u in adj(v):
        if u == v:
            degree[v] += int(cnt.get(u,0) >= 2)
        else:
            degree[v] += int(cnt.get(u,0) >= 1)

stack = [v for v,c in degree.items() if c == 1]
res = 0
while stack:
    v = stack.pop()
    c = cnt.get(v,0)
    if c == 0:
        continue
    for u in adj(v):
        if u == v and cnt.get(u,0) >= 2:
            break
        elif cnt.get(u,0) >= 1:
            break
    res += 1
    del cnt[u]
    degree[u] -= 1
    if degree[u] == 1:
        stack.append(u)

print(res)
Problem p03332 (python)

Fixed submission:
MOD = 998244353

N,A,B,K = map(int,input().split())

A,B = sorted((A,B))

if (A+B)*N < K:
  print(0)
  exit()

# tabulate combinations



# 拡張ユークリッド互除法
# ax + by = gcd(a,b)の最小整数解を返す
def egcd(a, b):
    if a == 0:
        return (b, 0, 1)
    else:
        g, y, x = egcd(b % a, a)
        return (g, x - (b // a) * y, y)


# mを法とするaの乗法的逆元
def modinv(a, m):
    g, x, y = egcd(a, m)
    if g != 1:
        raise Exception('modular inverse does not exist')
    else:
        return x % m


memo = [0]*(N+1)
memo[0] = memo[N] = 1
for k in range(1,N//2+1):
  v = memo[k-1]*(N-k+1)*modinv(k,MOD)
  v %= MOD
  memo[k] = memo[N-k] = v

cnt = 0

for b in range(N+1):
  a = (K-b*B)//A
  if 0 <= a <= N and a*A+b*B == K:
    cnt += memo[a]*memo[b]
    cnt %= MOD

print(cnt)

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

N,A,B,K = map(int,input().split())

A,B = sorted((A,B))

if (A+B)*N < K:
  print(0)
  exit()

# tabulate combinations



# 拡張ユークリッド互除法
# ax + by = gcd(a,b)の最小整数解を返す
def egcd(a, b):
    if a == 0:
        return (b, 0, 1)
    else:
        g, y, x = egcd(b % a, a)
        return (g, x - (b // a) * y, y)


# mを法とするaの乗法的逆元
def modinv(a, m):
    g, x, y = egcd(a, m)
    if g != 1:
        raise Exception('modular inverse does not exist')
    else:
        return x % m


memo = [0]*(N+1)
memo[0] = memo[N] = 1
for k in range(1,N//2+1):
  v = memo[k-1]*(N-k+1)*modinv(k,MOD)
  v %= MOD
  memo[k] = memo[N-k] = v

cnt = 0

for b in range((max(0,K-N*A)-1)//B+1, (K-1)//B+1):
  a = (K-b*B)//A
  if a*A+b*B == K:
    cnt += memo[a]*memo[b]
    cnt %= MOD

print(cnt)
Problem p04021 (python)

Fixed submission:
from bisect import bisect_left
N = int(input())
A = list(int(input()) for _ in range(N))
S = sorted(A)
A = [bisect_left(S,a) for a in A]
print((sum((i-j)%2 for i,j in enumerate(A))+1)//2)

Buggy submission (verdict: Runtime Error):
from bisect import bisect_left
N = int(input())
A = list(int(input()) for _ in range(N))
S = sorted(S)
A = [bisect_left(S,a) for a in A]
print((sum((i-j)%2 for i,j in enumerate(A))+1)//2)