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

Fixed submission:
S = input()
N = len(S)
cells = [0]*N

seq = 0
for i, s in enumerate(S):
    if s == 'R':
        seq += 1
        cells[i] = seq
        continue
    seq = 0

rev_S = S[::-1]
seq = 0
for i, s in enumerate(rev_S):
    if s == 'L':
        seq += 1
        cells[N-1-i] = seq
        continue
    seq = 0

ans = [0]*N
pre_cell = 0
pre_s = 'R'
l_flag =False
for i, s in enumerate(S):
    cell = cells[i]
    if pre_s == 'R' and s == 'L':
        ans[i] = cell//2 + cell%2 + pre_cell//2
        ans[i-1] = pre_cell//2 + pre_cell % 2 + cell//2
    pre_s = s
    pre_cell = cell

ans_str = ""
for a in ans:
    ans_str += str(a)+" "
print(ans_str)

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

def get_divisors(n):
    from math import ceil
    divisors = []
    for num in range(ceil(n**0.5), n+1):
        if n % num == 0:
            divisors.append(n//num)
            if n//num != n:
                divisors.append(num)
    divisors.sort(reverse=True)
    return divisors

total = sum(A)
divisors = get_divisors(total)
ans = -1
for d in divisors:
    R = [a%d for a in A]
    R.sort(reverse=True)
    pr = 0
    nr = -sum(R)
    for i, r in enumerate(R):
        pr += d-r
        nr += r
        if nr+pr >= 0:
            if pr <= K and nr+pr == 0:
                ans = d
            break
    if ans != -1:
        print(ans)
        break
else:
    print(1)

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

def get_divisors(n):
    from math import sqrt, ceil
    divisors = []
    for num in range(ceil(n**0.5), n+1):
        if n % num == 0:
            divisors.append(n//num)
            if n//num != n:
                divisors.append(num)
    divisors.sort(reverse=True)
    return divisors

total = sum(A)
divisors = get_divisors(total)
ans = -1
for d in divisors:
    R = [a%d for a in A]
    R.sort(reverse=True)
    pr = 0
    nr = -sum(R)
    for i, r in enumerate(R):
        pr += d-r
        nr += r
        if nr+pr >= 0:
            if pr <= K and nr+pr == 0:
                ans = d
            break
    if ans != -1:
        print(ans)
        break
else:
    print(1)
Problem p02955 (python)

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

# nの約数を求める。計算量O(sqrt(N))
def get_divisors(n):
    from math import sqrt, ceil
    from collections import deque
    divisors = deque([])
    for num in range(ceil(sqrt(n)), n+1):
        if n % num == 0:
            divisors.append(n//num)
            if n//num != n:
                divisors.appendleft(num)
    return list(divisors)


def make_divisors(n):
    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)
    divisors.sort(reverse=True)
    return divisors

total = sum(A)
divisors = make_divisors(total)
ans = -1
for d in divisors:
    R = [a%d for a in A]
    R.sort(reverse=True)
    pr = 0
    nr = -sum(R)
    for i, r in enumerate(R):
        pr += d-r
        nr += r
        if nr+pr >= 0:
            if pr <= K and nr+pr == 0:
                ans = d
            break
    if ans != -1:
        print(ans)
        break
else:
    print(1)

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

def get_divisors(n):
    divisors = []
    for num in range(ceil(n**0.5), n+1):
        if n % num == 0:
            divisors.append(n//num)
            if n//num != n:
                divisors.append(num)
    divisors.sort(reverse=True)
    return divisors

total = sum(A)
divisors = get_divisors(total)
ans = -1
for d in divisors:
    R = [a%d for a in A]
    R.sort(reverse=True)
    pr = 0
    nr = -sum(R)
    for i, r in enumerate(R):
        pr += d-r
        nr += r
        if nr+pr >= 0:
            if pr <= K and nr+pr == 0:
                ans = d
            break
    if ans != -1:
        print(ans)
        break
else:
    print(1)
Problem p02987 (python)

Fixed submission:
S = input()
one = 0
two = 0
strs = ["", ""]

for s in S:
    if strs[0] == s:
        one += 1
    elif strs[1] == s:
        two += 1
    elif strs[0] == "":
        strs[0] = s
        one += 1
    elif strs[1] == "":
        strs[1] = s
        two += 1
    else:
        print("No")
        break
    if one > 2 or two > 2:
        print("No")
        break
else:
    print("Yes")

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

def combination(a, b):
    minb = min(a-b, b)
    r = 1
    for i in range(1, b+1):
        r = r * (a-b+i)/i
    return r

if K == N:
    print(0)
    import sys
    sys.exit()

for i in range(1, K+1):
    if i-1 == 0:
        must = 1
    else:
        must = combination(K-1, i-1)
    left_ball = N-K-i+1
    if left_ball < 0:
        print(0)
        continue
    elif left_ball == 0:
        print(1)
        continue
    can = combination(left_ball+i, i)
    print(int((must*can)%(1000000007)))
Problem p03013 (python)

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

DENOMINATOR = 1000000007

pos = 0
route = 1
before = 0

for i in range(1, N+1):
    route, before = route+before, route
    if pos < M:
        if A[pos] == i:
            route = 0
            pos+=1

print(route%DENOMINATOR)

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

DENOMINATOR = 1000000007

pos = 0
route = 1
before = 0

for i in range(1, N+1):
    route, before = route+before, route
    if A[pos] == i:
        route = 0
        if pos < M-1:
            pos+=1

print(route%DENOMINATOR)
Problem p03038 (python)

Fixed submission:
N, M = map(int, input().split())
A = list(map(int, input().split()))
ops = [list(map(int, input().split())) for _ in range(M)]
A.sort()
ops.sort(key=lambda x:x[1], reverse=True)

a_index = 0
for op in ops:
    b, c = op
    tail = min(a_index+b, N)
    for i in range(a_index, a_index+b):
        if i >= N:
            a_index = None
            break
        if A[i] > c:
            a_index = None
            break
        A[i] = c
    else:
        a_index += b
    if a_index is None:
        break

print(sum(A))

Buggy submission (verdict: Runtime Error):
N, M = map(int, input().split())
A = list(map(int, input().split()))
ops = [list(map(int, input().split())) for _ in range(M)]
A.sort()
ops.sort(key=lambda x:x[1], reverse=True)

a_index = 0
for op in ops:
    b, c = op
    tail = min(a_index+b, N)
    for i in range(a_index, a_index+b):
        if A[i] > c:
            a_index = None
            break
        A[i] = c
    else:
        a_index += b
    if a_index is None:
        break
    if a_index >= N:
        break

print(sum(A))
Problem p02676 (python)

Fixed submission:
x = input()
y = input()
z = len(y)
if z <= int(x):
  print(y)
else:
  print(y[:int(x)]+'...')

Buggy submission (verdict: Runtime Error):
x = input()
y = input()
z = len(y)
if z <= int(x):
  print(y)
else:
  print(y[:x])

Buggy submission (verdict: Runtime Error):
x = input()
y = input()
z = len(y)
if z <= x:
  print(y)
else:
  print(y[:x])

Buggy submission (verdict: Runtime Error):
x = input()
y = input()
z = len(y)
if z <= x:
  print(y)
else:
  print(y[:x]
Problem p02682 (python)

Fixed submission:

a,b,c,k = map(int,input().split())

if  k < a:
    print(k)
elif k < a + b:
    print(a)
else:
    print(2*a + b - k)

Buggy submission (verdict: Runtime Error):

a,b,c,k = map(int,input().split())

if  k < a:
    print(k)
elif k < a + b:
    print(a)
nelse:
    print(2*a + b - k)
Problem p02699 (python)

Fixed submission:
s,w=map(int,input().split())
if s <= w:
    print("unsafe")
else:
    print("safe")

Buggy submission (verdict: Runtime Error):
import math

a,b,c,d=map(int,input().split())
x=int((a+d-1)/d)
y=int((c+b-1)/c)
 
if x>=y:
  print("Yes")
else:
  print("No")

Buggy submission (verdict: Runtime Error):
a,b,c,d=map(int,input().split())
x=int((a+d-1)/d)
y=int((c+b-1)/c)
 
if x>=y:
  print("Yes")
else:
  print("No")

Buggy submission (verdict: Runtime Error):
a,b,c,d=map(int,input().split())
x=(a+d-1)/d
y=(c+b-1)/c
 
if x>=y:
  print("yes")
else:
  print("no")

Buggy submission (verdict: Runtime Error):
a,b,c,d=map(int,input().split())
x=float(a-1/d)
y=float(c-1/b)

if x>=y:
  print("yes")
else:
  print("no")

Buggy submission (verdict: Runtime Error):
a,b,c,d=map(int,input().split())
x=float(a/d)
y=float(c/b)

if x>=y:
  print("yes")
else:
  print("no")
Problem p02682 (python)

Fixed submission:
one,zero,ng_one,card = map(int, input().split())

if card < one:
    print(card)
elif card <= one+zero:
    print(one)
else:
    print(one - (card - one - zero))

Buggy submission (verdict: Runtime Error):
one = int(input())
zero = int(input())
ng_one = int(input())
card = int(input())

card_list = []
for i in range(one):
    card_list.append(1)
for i in range(zero):
    card_list.append(0)
for i in range(ng_one):
    card_list.append(-1)

print(sum(card_list[:card]))
Problem p03108 (python)

Fixed submission:
import sys
sys.setrecursionlimit(65536)

N,M=map(int, input().split())
bridge=[[int(x) for x in input().split()] for _ in range(M)]

uf=[i for i in range(N+1)]
nc=[1 for i in range(N+1)]

def find(i):
	if uf[i] == i:
		return i
	else:
		uf[i] = find(uf[i])
		return uf[i]

def union(i, j):
	gi, gj = find(i), find(j)
	if gi != gj:
		uf[gi] = gj
		diff = nc[gj] * nc[gi]
		nc[gj] += nc[gi]
		return diff
	return 0

su = N*(N-1)//2
ans = [-1]*M

for i in range(M-1, -1, -1):
	ans[i] = su
	x,y = bridge[i]
	su -= union(x, y)

print("\n".join(map(str, ans)))

Buggy submission (verdict: Runtime Error):
N,M=map(int, input().split())
bridge=[[int(x) for x in input().split()] for _ in range(M)]

uf=[i for i in range(N+1)]
nc=[1 for i in range(N+1)]

def find(i):
	if uf[i] == i:
		return i
	else:
		uf[i] = find(uf[i])
		return uf[i]

def union(i, j):
	gi, gj = find(i), find(j)
	if gi != gj:
		uf[gi] = gj
		diff = nc[gj] * nc[gi]
		nc[gj] += nc[gi]
		return diff
	return 0

su = N*(N-1)//2
ans = [-1]*M

for i in range(M-1, -1, -1):
	ans[i] = su
	x,y = bridge[i]
	su -= union(x, y)

print("\n".join(map(str, ans)))

Buggy submission (verdict: Runtime Error):
N,M=map(int, input().split())
bridge=[[int(x) for x in input().split()] for _ in range(M)]

uf=[i for i in range(N+1)]
nc=[1 for i in range(N+1)]

def find(i):
	if uf[i] == i:
		return i
	else:
		uf[i] = find(uf[i])
		return uf[i]

def union(i, j):
	gi, gj = find(i), find(j)
	if gi != gj:
		uf[gi] = gj
		diff = nc[gj] * nc[gi]
		nc[gj] += nc[gi]
		return diff
	return 0

su = N*(N-1)//2
ans = [-1]*M

for i in range(M-1, -1, -1):
	ans[i] = su
	x,y = bridge[i]
	su -= union(x, y)

print("\n".join(map(str, ans)))

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

uf=[i for i in range(N+1)]
nc=[1 for i in range(N+1)]

def find(i):
	if uf[i] == i:
		return i
	else:
		uf[i] = find(uf[i])
		return uf[i]

def union(i, j):
	gi, gj = find(i), find(j)
	if gi != gj:
		uf[gi] = gj
		diff = nc[gj] * nc[gi]
		nc[gj] += nc[gi]
		return diff
	return 0

su = N*(N-1)//2
ans = []

for x,y in reversed([list(map(int, input().split())) for _ in range(M)]):
	ans.insert(0, su)
	su -= union(x,y)

for a in ans:
	print(a)
Problem p03127 (python)

Fixed submission:
from fractions import gcd
from functools import reduce

input()
print(reduce(gcd, map(int, input().split())))

Buggy submission (verdict: Runtime Error):
import math
from functools import reduce

input()
print(reduce(math.gcd, map(int, input().split())))
Problem p03208 (python)

Fixed submission:
n,k=map(int, input().split())
h=sorted([int(input()) for _ in range(n)])
print(min(h[i] - h[i-k+1] for i in range(k-1, n)))

Buggy submission (verdict: Runtime Error):
n,k=map(int, input().split())
h=sorted([int(input()) for _ in range(n)])
d=n-k
print(min(h[i+d]-h[i] for i in range(d+1)))
Problem p03634 (python)

Fixed submission:
import sys
sys.setrecursionlimit(1145141919)
N=int(input())
d=[-1 for _ in range(N+1)]
e=[[] for _ in range(N+1)]
for _ in range(N-1):
	a,b,c=map(int,input().split())
	e[a].append((b, c))
	e[b].append((a, c))
Q,K=map(int,input().split())
def dfs(now, dis):
	d[now] = dis
	for nxt,cos in e[now]:
		if d[nxt] == -1:
			dfs(nxt, dis+cos)
dfs(K, 0)
for _ in range(Q):
	x,y=map(int,input().split())
	print(d[x]+d[y])

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(65536)

N=int(input())
d=[-1 for _ in range(N+1)]
e=[[] for _ in range(N+1)]
for _ in range(N-1):
	a,b,c=map(int,input().split())
	e[a].append([b, c])
	e[b].append([a, c])
Q,K=map(int,input().split())
def bfs(now, dis):
	if d[now] != -1:
		return
	d[now] = dis
	for nxt, cos in e[now]:
		bfs(nxt, dis+cos)
bfs(K, 0)
for _ in range(Q):
	x,y=map(int,input().split())
	print(d[x]+d[y])

Buggy submission (verdict: Runtime Error):
N=int(input())
d=[-1 for _ in range(N+1)]
e=[[] for _ in range(N+1)]
for _ in range(N-1):
	a,b,c=map(int,input().split())
	e[a].append([b, c])
	e[b].append([a, c])
Q,K=map(int,input().split())
def bfs(now, dis):
	if d[now] != -1:
		return
	d[now] = dis
	for nxt, cos in e[now]:
		bfs(nxt, dis+cos)
bfs(K, 0)
for _ in range(Q):
	x,y=map(int,input().split())
	print(d[x]+d[y])
Problem p02790 (python)

Fixed submission:
a,b=map(int,input().split())#整数を複数の変数に格納

if a > b:
    print(str(b)*a)
elif a < b:
    print(str(a)*b)
else:
    print(str(a)*a)

Buggy submission (verdict: Runtime Error):
a,b=map(int,input().split())#整数を複数の変数に格納

for i in range
if a > b:
    print(str(b)*a)
else:
    print(str(a)*b)

Buggy submission (verdict: Runtime Error):
a,b=map(int,input().split())#整数を複数の変数に格納

for i in range
if a > b:
    print(str(b)*a)
else:
    print(str(a)*b)
Problem p02829 (python)

Fixed submission:
A = int(input())
B = int(input())

if A == 1 and B == 2:
    print(3)
elif A == 2 and B == 1:
    print(3)
elif A == 1 and B == 3:
    print(2)
elif A == 3 and B == 1:
    print(2)
else:
    print(1)

Buggy submission (verdict: Runtime Error):
A = int(input())
B = int(input())

if A == 1 and B == 2:
    print(3)
elif A == 2 and B == 1:
    print(3):
elif A == 1 and B == 3:
    print(2)
elif A == 3 and B == 1:
    print(2)
else:
    print(1)

Buggy submission (verdict: Runtime Error):
A = input()
B = input()

if A == 1 and B == 2:
    print(3)
elif A == 2 and B == 1:
    print(3):
elif A == 1 and B == 3:
    print(2)
elif A == 3 and B == 1:
    print(2)
else:
    print(1)
Problem p02847 (python)

Fixed submission:
S=input()

if S=="MON":
    print(6)
elif S == "TUE":
    print(5)
elif S == "WED":
    print(4)
elif S=="THU":
    print(3)
elif S=="FRI":
    print(2)
elif S=="SAT":
    print(1)
else:
    print(7)

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

if S="MON":
    print(6)
elif S = "TUE":
    print(5)
elif S = "WED":
    print(4)
elif S="THU":
    print(3)
elif S="FRI":
    print(2)
elif S="SAT":
    print(1)
else:
    print(7)

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

if S = "MON":
    print(6)
elif S = "TUE":
    print(5)
elif S = "WED":
    print(4)
elif S="THU":
    print(3)
elif S="FRI":
    print(2)
elif S="SAT":
    print(1)
else:
    print(7)

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

if S = "Mon":
    print(6)
elif S = "TUE":
    print(5)
elif S = "WED":
    print(4)
elif S="THU":
    print(3)
elif S="FRI":
    print(2)
elif S="SAT":
    print(1)
else:
    print(7)
Problem p02699 (python)

Fixed submission:
a,b=map(int,input().split())
if a<=b:
  print("unsafe")
else:
  print("safe")

Buggy submission (verdict: Runtime Error):
a,b=map(int,input().sprit())
if a<=b:
  print("unsafe")
else:
  print("safe")

Buggy submission (verdict: Runtime Error):
a,b=map(int,input().sprit())
if a <=b:
  print("unsafe")
else:
  print("safe")
Problem p02897 (python)

Fixed submission:
N = int(input())
 
ans = 0
for i in range(1, N+1):
    if i % 2 == 1:
        ans += 1
print(ans/N)

Buggy submission (verdict: Runtime Error):
import sys
 
stdin = sys.stdin
 
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces
 
n, m = na()
g = [[False] * n for _ in range(n)]
gs = [[] for _ in range(n)]
for i in range(m):
    s, t = na()
    gs[s-1].append(t-1)
    g[s-1][t-1] = True
 
ans = 999999
best = None
for i in range(n):
    ds = [9999999] * n
    prevs = [-1] * n
    ds[i] = 0
    q = [i]
    qp = 0
    while qp < len(q):
        cur = q[qp]
        qp += 1
        for e in gs[cur]:
            if ds[e] > ds[cur] + 1:
                ds[e] = ds[cur] + 1
                prevs[e] = cur
                q.append(e)
    for j in range(n):
        if g[j][i]:
            if ds[j] + 1 < ans:
                ans = ds[j] + 1
                best = [0] * (ds[j]+1)
                best[0] = i
                cur = j
                r = -1
                while cur != i:
                    best[r] = cur
                    r -= 1
                    cur = prevs[cur]
 
if not best:
    print(-1)
else:
    print(ans)
    for x in best:
        print(x+1)

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

b = ((a-a/2)/a)

print(b)

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

b = ((a-a/2)/a)

print(b)

Buggy submission (verdict: Runtime Error):
a = input()
if a %2 ==0:
    print(1/2)
    else:
        print(1-a/2)

Buggy submission (verdict: Runtime Error):
a = input()
if a %2 ==0:
    print(1/2)
    else:
        print(1-a/2)

Buggy submission (verdict: Runtime Error):
a = input()
	if a%2 == 0:
  		print("0.5")
      else
      print(1-a/2)
Problem p03104 (python)

Fixed submission:
import sys
from math import log, ceil, floor

fin = sys.stdin.readline
A, B = [int(elem) for elem in fin().split()]

def partial_f(n):
    if n == 0:
        return 0
    return ((n + 1) >> 1) & 1 if n & 1 else n | ((n >> 1) & 1)


print(partial_f(A - 1) ^ partial_f(B))

Buggy submission (verdict: Runtime Error):
import sys
from math import log, ceil, floor

fin = sys.stdin.readline
A, B = [int(elem) for elem in fin().split()]

# O(lg(max{A, B}))
result = 0
num_bits = floor(log(B, 2))
for d in range(num_bits + 1):
    base = 1 << d
    quo_A, rem_A = divmod(A, base << 1)
    quo_B, rem_B = divmod(B, base << 1)
    if quo_A != quo_B:
        parity = (base - max(0, rem_A - base) + max(0, rem_B - (base - 1))) % 2
    else:
        parity = 0 if rem_A < base and rem_B < base else \
            (rem_B - base + 1 - max(0, rem_A - base)) % 2
    if parity:
        result |= 1 << d

print(result)

Buggy submission (verdict: Runtime Error):
import sys
from math import log, ceil, floor

fin = sys.stdin.readline
A, B = [int(elem) for elem in fin().split()]
# if A == B:
#     print(A)
#     exit(0)

# O(lg(max{A, B}))
result = 0
num_bits = floor(log(B, 2))
for d in range(num_bits + 1):
    base = 1 << (d + 1)
    quo_A, rem_A = divmod(A, base)
    quo_B, rem_B = divmod(B, base)
    if quo_A != quo_B:
        parity = ((base >> 1) - max(0, rem_A - (base >> 1)) + max(0, rem_B - ((base >> 1) - 1))) % 2
    else:
        parity = 0 if rem_A < (base >> 1) and rem_B < (base >> 1) else \
            rem_B - (base >> 1) + 1 - max(0, rem_A - (base >> 1))
    if parity:
        result |= 1 << d

print(result)

Buggy submission (verdict: Runtime Error):
import sys
from math import log2, ceil, floor

fin = sys.stdin.readline
A, B = [int(elem) for elem in fin().split()]
if A == B:
    print(A)
    exit(0)

# O(lg(max{A, B}))
result = 0
num_bits = floor(log2(B))
for d in range(num_bits + 1):
    base = 1 << (d + 1)
    mod_A = A % base
    mod_B = B % base

    parity = ((base >> 1) - max(0, mod_A - (base >> 1)) + max(0, mod_B - ((base >> 1) - 1))) % 2
    if parity:
        result |= 1 << d

print(result)
Problem p03232 (python)

Fixed submission:
import sys
fin = sys.stdin.readline
sys.setrecursionlimit(200000)

def factorial(n, mod):
    if n == 0:
        return 1
    else:
        return n * factorial(n - 1, mod) % mod


MOD = 10**9 + 7
N = int(fin())
A_list = [int(elem) for elem in fin().split()]

fac_N = factorial(N, MOD)
inv_nums = [fac_N * pow(i, MOD - 2, MOD) % MOD for i in range(1, N + 1)]
cuml_inv_nums = [inv_nums[0]]
for inv_num in inv_nums[1:]:
    cuml_inv_nums.append((cuml_inv_nums[-1] + inv_num) % MOD)

ans = 0
for i, A in enumerate(A_list):
    ans += A * (cuml_inv_nums[i] + cuml_inv_nums[N - 1 - i] - cuml_inv_nums[0]) % MOD
    ans %= MOD

print(ans)

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


def factorial(n, mod):
    if n == 0:
        return 1
    else:
        return n * factorial(n - 1, mod) % mod


MOD = 10**9 + 7
N = int(fin())
A_list = [int(elem) for elem in fin().split()]

fac_N = factorial(N, MOD)
inv_nums = [fac_N * pow(i, MOD - 2, MOD) % MOD for i in range(1, N + 1)]
cuml_inv_nums = [inv_nums[0]]
for inv_num in inv_nums[1:]:
    cuml_inv_nums.append((cuml_inv_nums[-1] + inv_num) % MOD)

ans = 0
for i, A in enumerate(A_list):
    ans += A * (cuml_inv_nums[i] + cuml_inv_nums[N - 1 - i] - cuml_inv_nums[0]) % MOD
    ans %= MOD

print(ans)
Problem p03291 (python)

Fixed submission:
S = input()

MOD = 10**9 + 7
dp = [[0] * 4 for _ in range(len(S) + 1)]
dp[len(S)][3] = 1
for i in range(len(S))[::-1]:
    dp[i][3] = 3 * dp[i + 1][3] if S[i] == "?" else dp[i + 1][3]
    dp[i][3] %= MOD
    if S[i] == "?":
        dp[i][0] = (3 * dp[i + 1][0] + dp[i + 1][1]) % MOD
        dp[i][1] = (3 * dp[i + 1][1] + dp[i + 1][2]) % MOD
        dp[i][2] = (3 * dp[i + 1][2] + dp[i + 1][3]) % MOD
    else:
        dp[i][0] = (dp[i + 1][0] + int(S[i] == "A") * dp[i + 1][1]) % MOD
        dp[i][1] = (dp[i + 1][1] + int(S[i] == "B") * dp[i + 1][2]) % MOD
        dp[i][2] = (dp[i + 1][2] + int(S[i] == "C") * dp[i + 1][3]) % MOD
print(dp[0][0])

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


MOD = 10**9 + 7
# time complexity: O(|S|)
def focus_on_B(S):
    cuml = {"A": [], 
            "C": [],
            "?": []}
    current_nums = {"A": 0,
                    "C": 0,
                    "?": 0}
    for char in S:
        if char in cuml.keys():
            current_nums[char] += 1
        for key in cuml.keys():
            cuml[key].append(current_nums[key])

    num_combinations = 0
    for i in range(1, len(S) - 1):
        if S[i] in {"B", "?"}:
            A_L = cuml["A"][i-1]
            Q_L = cuml["?"][i-1]
            C_R = cuml["C"][-1] - cuml["C"][i]
            Q_R = cuml["?"][-1] - cuml["?"][i]
            num_combinations += int((A_L * 3 + Q_L) * 3**(Q_L - 1)\
                            * (C_R * 3 + Q_R) * 3**(Q_R - 1))
            num_combinations %= MOD
        else:
            continue
    print(num_combinations)


focus_on_B(S)
Problem p03327 (python)

Fixed submission:
N = int(input())

if N < 1000:
    print("ABC")
else:
    print("ABD")

Buggy submission (verdict: Runtime Error):
from itertools import permutations

N, C = [int(elem) for elem in input().split(' ')]
D_matrix = [[int(elem) for elem in input().split(' ')] for _ in range(C)]
assert len(D_matrix) == C
assert len(D_matrix[0]) == C
c_matrix = [[int(elem) for elem in input().split(' ')] for _ in range(N)]
assert len(c_matrix) == N
assert len(c_matrix[0]) == N

# grouping
# O(N^2)
mod_list = [[] for _ in range(3)]
for i in range(N):
    for j in range(N):
        mod_list[(i + j) % 3].append((i, j))

# O(N^2)
mod_color_list = [[0] * C for _ in range(3)]
for k, mod in enumerate(mod_list):
    for i, j in mod:
        mod_color_list[k][c_matrix[i][j] - 1] += 1

# O(C^3 * N^2) -> O(C^4)
min_cost = float('inf')
for colors in permutations(range(C), 3):
    total_cost = 0
    for color, mod_color in zip(colors, mod_color_list):
        total_cost += sum([D_matrix[X][color] * mod_color[X] for X in range(C)])
    min_cost = min(min_cost, total_cost)

print(min_cost)