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

Fixed submission:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

import numpy as np

N = int(read())

def output(A):
    print('\n'.join(''.join(row) for row in A))

a = 'a'; b = 'b'; c = 'c'; d = 'd'; e = 'e'; f = 'f'; g = 'g'; h = 'h'; x = '.'

A3 = np.array([
        [a,a,x],
        [x,x,a],
        [x,x,a]
],dtype='U1')

A4 = np.array([
    [a,a,c,d],
    [b,b,c,d],
    [e,f,g,g],
    [e,f,h,h]
],dtype='U1')

A5 = np.array([
    [a,a,c,a,a],
    [b,b,c,x,c],
    [x,a,b,b,c],
    [b,a,x,x,a],
    [b,x,b,b,a]
],dtype='U1')

A7 = np.array([
    [a,b,a,x,x,x,x],
    [a,b,a,x,x,x,x],
    [c,c,b,a,a,x,x],
    [x,a,b,c,c,x,x],
    [x,a,x,x,a,b,b],
    [a,x,x,x,a,c,c],
    [a,x,b,b,x,a,a],
],dtype='U1')

def solve(N):
    if N <= 2:
        print(-1)
        return
    if N == 3:
        output(A3)
        return
    if N == 6:
        A = np.full((6,6),'.','U1')
        A[:3,:3] = A3
        A[3:,3:] = A3
        output(A)
        return
    if N == 7:
        output(A7)
        return
    if N == 11:
        A = np.full((11,11),'.','U1')
        A[:4,:4] = A4
        A[4:,4:] = A7
        output(A)
        return
    # 4,5,8,9,10,12以上
    # このときは、4x+5yと書ける
    x = 0
    while (N - 4 * x) % 5:
        x += 1
    y = (N - 4 * x) // 5
    A = np.full((N,N),'.','U1')
    for i in range(x):
        A[4*i:4*i+4,4*i:4*i+4] = A4
    for j in range(y):
        A[4*x+5*j:4*x+5*j+5,4*x+5*j:4*x+5*j+5] = A5
    output(A)
    return

solve(N)

Buggy submission (verdict: Runtime Error):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

import numpy as np

N = int(read())

def output(A):
    print('\n'.join(''.join(row) for row in A))

a = 'a'; b = 'b'; c = 'c'; d = 'd'; e = 'e'; f = 'f'; g = 'g'; h = 'h'; x = '.'

A3 = np.array([
        [a,a,x],
        [x,x,a],
        [x,x,a]
])

A4 = np.array([
    [a,a,c,d],
    [b,b,c,d],
    [e,f,g,g],
    [e,f,h,h]
])

A5 = np.array([
    [a,a,c,a,a],
    [b,b,c,x,c],
    [x,a,b,b,c],
    [b,a,x,x,a],
    [b,x,b,b,a]
])

A7 = np.array([
    [a,b,a,x,x,x,x],
    [a,b,a,x,x,x,x],
    [c,c,b,a,a,x,x],
    [x,a,b,c,c,x,x],
    [x,a,x,x,a,b,b],
    [a,x,x,x,a,c,c],
    [a,x,b,b,x,a,a]
])

def solve(N):
    if N <= 2:
        print(-1)
        return
    if N == 3:
        output(A3)
        return
    if N == 6:
        A = np.full((6,6),'.')
        A[:3,:3] = A3
        A[3:,3:] = A3
        output(A)
        return
    if N == 7:
        output(A7)
        return
    if N == 11:
        A = np.full((11,11),'.')
        A[:4,:4] = A4
        A[4:,4:] = A7
        output(A)
        return
    # 4,5,8,9,10,12以上
    # このときは、4x+5yと書ける
    x = 0
    while (N - 4 * x) % 5:
        x += 1
    y = (N - 4 * x) // 5
    A = np.full((N,N),'.')
    for i in range(x):
        A[4*i:4*i+4,4*i:4*i+4] = A4
    for j in range(y):
        A[4*x+5*j:4*x+5*j+5,4*x+5*j:4*x+5*j+5] = A5
    output(A)
    return

solve(N)
Problem p02839 (python)

Fixed submission:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

H,W = map(int,readline().split())
A = [list(map(int,readline().split())) for _ in range(H)]
B = [list(map(int,readline().split())) for _ in range(H)]

X = (H + W) * 80
# X点持った状態から始める
dp = [[0] * W for _ in range(H)]
d = abs(A[0][0] - B[0][0])
dp[0][0] = (1 << (X - d))
for h in range(H):
    for w,(a,b) in enumerate(zip(A[h],B[h])):
        if h == w == 0:
            continue
        d = abs(a - b)
        x = 0
        if h != 0:
            x |= (dp[h-1][w] << d) 
            x |= (dp[h-1][w] >> d)
        if w != 0:
            x |= (dp[h][w-1] << d) 
            x |= (dp[h][w-1] >> d)
        dp[h][w] = x

dp = dp[-1][-1]

B = bin(dp)[2:]
can_make = [i - X for i,b in enumerate(B[::-1]) if b == '1']

answer = min(x if x >= 0 else -x for x in can_make)
print(answer)

Buggy submission (verdict: Runtime Error):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

import itertools
import numpy as np

H,W = map(int,readline().split())
A = [list(map(int,readline().split())) for _ in range(H)]
B = [list(map(int,readline().split())) for _ in range(H)]

X = (H + W) * 80
# X点持った状態から始める
dp = [[0] * W for _ in range(H)]
d = abs(A[0][0] - B[0][0])
dp[0][0] = np.zeros(X+X+1,np.bool)
dp[0][0][X+d] = 1
for h in range(H):
    for w,(a,b) in enumerate(zip(A[h],B[h])):
        if h == w == 0:
            continue
        d = abs(a - b)
        x = np.zeros(X+X+1,np.bool)
        if h != 0:
            x[d:] |= dp[h-1][w][:-d]
            x[:-d] |= dp[h-1][w][d:]
        if w != 0:
            x[d:] |= dp[h][w-1][:-d]
            x[:-d] |= dp[h][w-1][d:]
        dp[h][w] = x

dp = dp[-1][-1]
can_make = np.where(dp)[0] - X

answer = np.abs(can_make).min()
print(answer)
Problem p02874 (python)

Fixed submission:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N = int(readline())
m = map(int,read().split())
LR = list(zip(m,m))

if N==2:
    L1,R1 = LR[0]
    L2,R2 = LR[1]
    answer = (R1-L1+1) + (R2-L2+1)
    print(answer)
    exit()

# 片方に集める場合
max_width = max(R-L+1 for L,R in LR)

LR.sort()

INF=10**18
# Rが最小のもの
i = -1
opt_R = INF
for n,(L,R) in enumerate(LR):
    if opt_R>=R:
        opt_R=R
        i=n
j = -1
opt_L = -INF
for n,(L,R) in enumerate(LR):
    if opt_L<=L:
        opt_L=L
        j=n

if i==j:
    L,R = LR[i]
    answer = max_width + (R-L+1)
    print(answer)
    exit()

L1,R1 = LR[i]; L2,R2 = LR[j]

LR = [(L,R) for n,(L,R) in enumerate(LR) if n != i and n != j]

LR.sort(key = lambda x: (x[0],-x[1]))

n = len(LR)
dpl = [(-INF,INF)]*(n+1)
dpr = [(-INF,INF)]*(n+1)

for i in range(n):
    a,b = dpl[i]
    L,R = LR[i]
    dpl[i+1] = (max(a,L),min(b,R))
for i in range(n,0,-1):
    a,b = dpr[i]
    L,R = LR[i-1]
    dpr[i-1] = (max(a,L),min(b,R))

answer = 0
for (a,b),(c,d) in zip(dpl,dpr):
    l1 = max(L1,a); r1 = min(R1,b)
    l2 = max(L2,c); r2 = min(R2,d)
    x = max(0,r1-l1+1) + max(0,r2-l2+1)
    if answer < x:
        answer = x

x = max_width + max(0,min(R1,R2)-max(L1,L2)+1)
if answer < x:
    answer = x

print(answer)

Buggy submission (verdict: Runtime Error):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N = int(readline())
m = map(int,read().split())
LR = list(zip(m,m))

if N==2:
    L1,R1 = LR[0]
    L2,R2 = LR[1]
    answer = (R1-L1+1) + (R2-L2+1)
    print(answer)
    exit()

# 片方に集める場合
max_width = max(R-L+1 for L,R in LR)

LR.sort()

INF=10**18
# Rが最小のもの
i = -1
opt_R = INF
for n,(L,R) in enumerate(LR):
    if opt_R>=R:
        opt_R=R
        i=n
j = -1
opt_L = -INF
for n,(L,R) in enumerate(LR):
    if opt_L<=L:
        opt_L=L
        j=n

if i==j:
    L,R = LR[i]
    answer = max_width + (R-L+1)
    print(answer)
    raise Exception
    exit()

L1,R1 = LR[i]; L2,R2 = LR[j]

LR = [(L,R) for n,(L,R) in enumerate(LR) if n != i and n != j]

LR.sort()

n = len(LR)
dpl = [(-INF,INF)]*(n+1)
dpr = [(-INF,INF)]*(n+1)

for i in range(n):
    a,b = dpl[i]
    L,R = LR[i]
    dpl[i+1] = (max(a,L),min(b,R))
for i in range(n,0,-1):
    a,b = dpr[i]
    L,R = LR[i-1]
    dpr[i-1] = (max(a,L),min(b,R))

answer = 0
for (a,b),(c,d) in zip(dpl,dpr):
    l1 = max(L1,a); r1 = min(R1,b)
    l2 = max(L2,c); r2 = min(R2,d)
    x = max(0,r1-l1+1) + max(0,r2-l2+1)
    if answer < x:
        answer = x

if answer < max_width:
    answer = max_width

print(answer)
Problem p02902 (python)

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

from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import connected_components
import numpy as np
from collections import defaultdict

N,M = map(int,readline().split())

if M == 0:
    print(-1)
    exit()

AB = [tuple(int(x) for x in line.split()) for line in readlines()]

A,B = zip(*AB)
graph = csr_matrix(([1]*M,(A,B)),(N+1,N+1))
_,comp = connected_components(graph,connection='strong')

size = np.bincount(comp)

if size.max() == 1:
    print(-1)
    exit()

n = np.where(size > 1)[0][0]

V = set(np.where(comp == n)[0])

graph = [[] for _ in range(N+1)]
for a,b in AB:
    if a in V and b in V:
        graph[a].append(b)

# とりあえず1つサイクルを見つける
order = defaultdict(int)
x = V.pop()
V.add(x)
q = [x]
order[x] = 1
path = [x]
flag = False
while True:
    y = graph[x][0]
    if order[y] != 0:
        path = path[order[y]-1:]
        break
    path.append(y)
    order[y] = order[x] + 1
    x = y

V = set(path)
next_v = {u:v for u,v in zip(path,path[1:])}; next_v[path[-1]] = path[0]

# 辺を1つ追加しながら、小さいサイクルを見つける
for a,b in AB:
    if not (a in V and b in V):
        continue
    # 間の頂点を削除
    x = next_v[a]
    while x != b:
        V.remove(x)
        x = next_v[x]
    next_v[a] = b

print(len(V))
print(*V,sep='\n')

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

from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import connected_components
import numpy as np
from collections import defaultdict

N,M = map(int,readline().split())

AB = [tuple(int(x) for x in line.split()) for line in readlines()]

A,B = zip(*AB)
graph = csr_matrix(([1]*M,(A,B)),(N+1,N+1))
_,comp = connected_components(graph,connection='strong')

size = np.bincount(comp)

if size.max() == 1:
    print(-1)
    exit()

n = np.where(size > 1)[0][0]

V = set(np.where(comp == n)[0])

V

graph = [[] for _ in range(N+1)]
for a,b in AB:
    if a in V and b in V:
        graph[a].append(b)

# とりあえず1つサイクルを見つける
order = defaultdict(int)
x = V.pop()
V.add(x)
q = [x]
order[x] = 1
path = [x]
flag = False
while True:
    y = graph[x][0]
    if order[y] != 0:
        path = path[order[y]-1:]
        break
    path.append(y)
    order[y] = order[x] + 1
    x = y

V = set(path)
next_v = {u:v for u,v in zip(path,path[1:])}; next_v[path[-1]] = path[0]

path

# 辺を1つ追加しながら、小さいサイクルを見つける
for a,b in AB:
    if not (a in V and b in V):
        continue
    # 間の頂点を削除
    x = next_v[a]
    while x != b:
        V.remove(x)
        x = next_v[x]
    next_v[a] = b

print(len(V))
print(*V,sep='\n')
Problem p02913 (python)

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

import numpy as np

N = int(input())
S = np.array([ord(x) for x in input().rstrip()],dtype=np.int64)

MOD = 10**9 + 7
base = 1234

def cumprod(arr):
    L = len(arr); Lsq = int(L**.5+1)
    arr = np.resize(arr,Lsq**2).reshape(Lsq,Lsq)
    for n in range(1,Lsq):
        arr[:,n] *= arr[:,n-1]; arr[:,n] %= MOD
    for n in range(1,Lsq):
        arr[n] *= arr[n-1,-1]; arr[n] %= MOD
    return arr.ravel()[:L]

x = np.full(N,base,np.int64); x[0] = 1
power = cumprod(x)
x = np.full(N,pow(base,MOD-2,MOD),np.int64); x[0] = 1
power_inv = cumprod(x)

# rolling hash
H = S * power % MOD
Hcum = H.cumsum() % MOD

def test(n):
    # 長さnの文字列で2回現れるものが存在する
    x = (Hcum[n-1:N] - Hcum[:N-n+1] + H[:N-n+1]) % MOD
    x *= power_inv[:N-n+1]
    x %= MOD
    for i,a in np.ndenumerate(x):
        if (x[i[0]+n:] == a).any():
            return True
    return False

left = 0 # 存在
right = N+1 # 非存在
while left + 1 < right:
    mid = (left + right)//2
    if test(mid):
        left = mid
    else:
        right = mid

answer = left
print(answer)

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

import numpy as np

N = int(input())
S = np.array([ord(x) for x in input().rstrip()],dtype=np.int64)

MOD = 10**9 + 7
base = 1234

def cumprod(arr):
    L = len(arr); Lsq = int(L**.5+1)
    arr = np.resize(arr,Lsq**2).reshape(Lsq,Lsq)
    for n in range(1,Lsq):
        arr[:,n] *= arr[:,n-1]; arr[:,n] %= MOD
    for n in range(1,Lsq):
        arr[n] *= arr[n-1,-1]; arr[n] %= MOD
    return arr.ravel()[:L]

x = np.full(N,base,np.int64); x[0] = 1
power = cumprod(x)
x = np.full(N,pow(base,MOD-2,MOD),np.int64); x[0] = 1
power_inv = cumprod(x)

# rolling hash
H = S * power % MOD
Hcum = H.cumsum() % MOD

def test(n):
    # 長さnの文字列で2回現れるものが存在する
    x = (Hcum[n-1:] - Hcum[:N-n+1] + H[:N-n+1]) % MOD
    x *= power_inv[:N-n+1]
    x %= MOD
    # 距離n以上離れたところで連番が存在しないといけない
    ind = x.argsort(kind='stable')
    x = x[ind]
    is_left_end = np.zeros(N-n+1,np.bool)
    is_left_end[1:] = (x[1:] != x[:-1])
    is_left_end[0] = True
    is_right_end = np.zeros(N-n+1,np.bool)
    is_right_end[:-1] = (x[:-1] != x[1:])
    is_right_end[-1] = True
    gap = ind[is_right_end] - ind[is_left_end]
    return gap.max() >= n

left = 0 # 存在
right = N+1 # 非存在
while left + 1 < right:
    mid = (left + right)//2
    if test(mid):
        left = mid
    else:
        right = mid

answer = left
print(answer)
Problem p02923 (python)

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

N = int(input())
H = [int(x) for x in input().split()]

score = [0]*N
for n in range(N-2,-1,-1):
    if H[n] >= H[n+1]:
        score[n] = score[n+1] + 1

print(max(score))

Buggy submission (verdict: Runtime Error):
import sys
import numpy as np
N = int(sys.stdin.readline())
A = np.farray(sys.stdin.readline().split(), dtype=np.int32)

"""
A = [10,4,8,7,3]
is_left_end = [1,0,1,0,0]
left_idx = [0,0,2,2,2]
というものを作る
"""
is_left_end = np.empty(N, dtype=np.bool)
is_left_end[0] = 1
is_left_end[1:] = (A[1:] > A[:-1])


left_idx = np.where(is_left_end, np.arange(N), 0) # [0,0,2,0,0]
np.maximum.accumulate(left_idx, out=left_idx) # [0,0,2,2,2]

answer = (np.arange(N) - left_idx).max()
print(answer)
Problem p02927 (python)

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

M,D = map(int,input().split())

answer = 0
for d in range(10,D+1):
    a,b = divmod(d,10)
    if a >= 2 and b >= 2 and 1<=a*b<=M:
        answer += 1

print(answer)

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

N,H,W = map(int,readline().split())
RCA = [tuple(int(x) for x in line.split()) for line in readlines()]

RCA.sort(key = lambda x: -x[2])

RCA

root = list(range(H+W))
size = [0] * (H+W)
no_cycle = [True] * (H+W)

def find_root(x):
    y = root[x]
    if x == y:
        return y
    z = find_root(y)
    root[x] = z
    return z

def merge(x,y):
    x,y = find_root(x),find_root(y)
    sx,sy = size[x],size[y]
    if sx < sy:
        sx,sy = sy,sx
        x,y = y,x
    root[y] = x
    no_cycle[x] = no_cycle[x] and no_cycle[y]

answer = 0
for R,C,A in RCA:
    x,y = R-1,H+C-1
    rx,ry = find_root(x),find_root(y)
    if rx == ry:
        if not no_cycle[rx]:
            continue
        no_cycle[rx] = False
    else:
        if (not no_cycle[rx]) and (not no_cycle[ry]):
            continue
        merge(rx,ry)
    answer += A

print(answer)
Problem p02936 (python)

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

# 遅延評価で加えてあげるだけ

N,Q = map(int,input().split())
AB = [[int(x) for x in input().split()] for _ in range(N-1)]
PX = [[int(x) for x in input().split()] for _ in range(Q)]

graph = [[] for _ in range(N+1)]
for a,b in AB:
    graph[a].append(b)
    graph[b].append(a)

value = [0] * (N+1)
for p,x in PX:
    value[p] += x

def dfs(v,parent,add):
    value[v] += add
    for x in graph[v]:
        if x == parent:
            continue
        dfs(x,v,value[v])

dfs(1,0,0)

answer = ' '.join(map(str,value[1:]))
print(answer)

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

# 遅延評価で加えてあげるだけ

N,Q = map(int,readline().split())
lines = readlines()
AB = [tuple(int(x) for x in readline().split()) for _ in range(N-1)]
PX = [tuple(int(x) for x in readline().split()) for _ in range(Q)]

graph = [[] for _ in range(N+1)]
for a,b in AB:
    graph[a].append(b)
    graph[b].append(a)

value = [0] * (N+1)
for p,x in PX:
    value[p] += x

par = [None] * (N+1)
q = [1]
while q:
    x = q.pop()
    for y in graph[x]:
        if y == par[x]:
            continue
        value[y] += value[x]
        q.append(y)
        par[y] = x

answer = ' '.join(map(str,value[1:]))
print(answer)
Problem p02949 (python)

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

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

INF = 10 ** 18
dist = [INF] * (N+1)
dist[1] = 0

loop = False
for i in range(2*N+100):
    for a,b,c in ABC:
        x = dist[a] + P - c
        if dist[b] > x:
            dist[b] = x
            if b == N and i > N:
                loop = True

if loop:
    answer = -1
else:
    answer = -dist[N]
    if answer < 0:
        answer = 0

print(answer)

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

from scipy.sparse.csgraph import bellman_ford, connected_components
from scipy.sparse import csr_matrix

N,M,P = map(int,input().split())
ABC = [[int(x) for x in input().split()] for _ in range(M)]
A,B,C = zip(*ABC)
C = [P-c for c in C]

graph = csr_matrix((C,(A,B)),(N+1,N+1))
graph2 = csr_matrix(([1]*(M+1),(A+(N,),B+(1,))),(N+1,N+1))
_,comp = connected_components(graph2, connection='strong')
select = (comp == comp[1])
graph = graph[select,:][:,select]

try:
    dist = bellman_ford(graph, indices=[0])
    answer = int((.5-dist[0,-1]) // 1)
    if answer < 0:
        answer = 0
except NegativeCycleError:
    answer = -1
print(answer)
Problem p02957 (python)

Fixed submission:
a,b = map(int,input().split())
if (a-b) % 2 == 0:
  print((a+b)//2)
else:
  print('IMPOSSIBLE')

Buggy submission (verdict: Runtime Error):
import random
r = random.randint(1,3)

if r == 1:
  # TLE
  import time
  time.sleep(5)
elif r == 2:
  # MLE
  x = '01234567'
  x *= (1 << 27)
else:
  # RE
  RE
Problem p02958 (python)

Fixed submission:
import itertools
N = int(input())
A = [int(x) for x in input().split()]
B = list(range(1,N+1))

def test(i,j):
  AA = A.copy()
  AA[i], AA[j] = AA[j], AA[i]
  return AA == B

bl = (A == B)
for i,j in itertools.combinations(range(N),2):
  if test(i,j):
    bl = True
answer = 'YES' if bl else 'NO'
print(answer)

Buggy submission (verdict: Runtime Error):
import itertools
N = int(input())
A = [int(x) for x in input().split()]
B = list(range(1,N+1))

def test(i,j):
  AA = A.copy()
  AA[i], AA[j] = AA[j], AA[i]
  return AA == B

bl = (A == B)
for i,j in itertools.combinations(range(N)):
  if test(i,j):
    bl = True
answer = 'YES' if bl else 'NO'
print(answer)
Problem p02960 (python)

Fixed submission:
MOD = 10 ** 9 + 7
S = input()

dp = [0] * 13
dp[0] = 1

for s in S:
  prev = [x for x in dp]
  dp = [0] * 13
  if s == '?':
    add = range(10)
  else:
    add = [int(s)]
  for i in range(13):
    for x in add:
      dp[(10*i+x)%13] += prev[i]
  dp = [x % MOD for x in dp]
  
print(dp[5])

Buggy submission (verdict: Runtime Error):
MOD = 10 ** 9 + 7
S = input()

dp = [0] * 13
dp[0] = 1

for s in S:
  prev = dp.copy()
  dp = [0] * 13
  if s == '?':
    add = range(10)
  else:
    add = [int(s)]
  for i in range(13):
    for x in add:
      dp[(10*i+x)%13] += prev[i]
  dp = [x % MOD for x in dp]
  
print(dp[5])
Problem p02976 (python)

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

N,M = map(int,input().split())
if M&1:
    print(-1)
    exit()

AB = [[int(x) for x in input().split()] for _ in range(M)]

graph = [[] for _ in range(N+1)]
for a,b in AB:
    graph[a].append(b)
    graph[b].append(a)

# まずは全域木をつくる
tree = [set() for _ in range(N+1)]
visited = [False] * (N+1)
q = [1]
while q:
    x = q.pop()
    for y in graph[x]:
        if visited[y]:
            continue
        visited[y] = True
        tree[x].add(y)
        tree[y].add(x)
        q.append(y)

answer = []
out_deg = [0] * (N+1)
# 木を残して適当に決める
for a,b in AB:
    if b in tree[a]:
        continue
    answer.append('{} {}'.format(a,b))
    out_deg[a] += 1

def dfs(x = 1,parent = None):
    for y in tree[x]:
        if y == parent:
            continue
        dfs(y,x)
        if out_deg[y] & 1:
            answer.append('{} {}'.format(y,x))
            out_deg[y] += 1
        else:
            answer.append('{} {}'.format(x,y))
            out_deg[x] += 1

dfs()

print('\n'.join(answer))

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

N,M = map(int,input().split())
if M&1:
    print(-1)
    exit()

AB = [[int(x) for x in input().split()] for _ in range(M)]

graph = [[] for _ in range(N+1)]
for a,b in AB:
    graph[a].append(b)
    graph[b].append(a)

# まずは全域木をつくる
tree = [set() for _ in range(N+1)]
visited = [False] * (N+1)
q = [1]
while q:
    x = q.pop()
    for y in graph[x]:
        if visited[y]:
            continue
        visited[y] = True
        tree[x].add(y)
        tree[y].add(x)
        q.append(y)

answer = []
out_deg = [0] * (N+1)
# 木を残して適当に決める
for a,b in AB:
    if b in tree[a]:
        continue
    answer.append('{} {}'.format(a,b))
    out_deg[a] += 1

def dfs(x = 1,parent = None):
    for y in tree[x]:
        if y == parent:
            continue
        dfs(y,x)
        if out_deg[y] & 1:
            answer.append('{} {}'.format(y,x))
            out_deg[y] += 1
        else:
            answer.append('{} {}'.format(x,y))
            out_deg[x] += 1

dfs()

print('\n'.join(answer))
Problem p02991 (python)

Fixed submission:
# 頂点を3回ずつに持つ (x歩目の着地）
# 最短路

import sys
input = sys.stdin.readline
N,M = map(int,input().split())
if M == 0:
  print(-1)
  exit()
edge = [[] for _ in range(N+1)]
for _ in range(M):
  u,v = map(int,input().split())
  edge[u].append(v)
S,T = map(int,input().split())

INF = 10 ** 9
dist = [[INF,INF,INF] for _ in range(N+1)]

d = 0
q = [S]
while q:
  d += 1
  r = d % 3
  qq = []
  for u in q:
    for v in edge[u]:
      if dist[v][r] == INF:
        dist[v][r] = d
        qq.append(v)
  q = qq

d = dist[T][0]
answer = -1 if d == INF else d // 3
print(answer)

Buggy submission (verdict: Runtime Error):
# 頂点を3回ずつに持つ (x歩目の着地）
# 最短路

import sys
import numpy as np
from scipy.sparse import *
input = sys.stdin.readline
N,M = map(int,input().split())
UV = np.array([input().split() for _ in range(M)], dtype=np.int32)
U = UV[:,0] * 3
V = UV[:,1] * 3
S,T = map(int,input().split())

rows = np.concatenate([U, U+1, U+2])
cols = np.concatenate([V+1, V+2, V])

graph = csr_matrix((np.ones_like(rows), (rows, cols)), (3*N+3, 3*N+3))
dist = csgraph.dijkstra(graph, indices = 3*S)

answer = -1 if dist[3*T] > 3*N else int(dist[3*T]//3)
print(answer)

Buggy submission (verdict: Runtime Error):
# 頂点を3回ずつに持つ (x歩目の着地）
# 最短路

import sys
import numpy as np
from scipy.sparse import *
input = sys.stdin.readline
N,M = map(int,input().split())
UV = np.array([input().split() for _ in range(M)], dtype=np.int32)
U = UV[:,0]
V = UV[:,1]
S,T = map(int,input().split())
if M == 0:
  print(-1)
  exit()
rows = np.concatenate([3*U, 3*U+1, 3*U+2])
cols = np.concatenate([3*V+1, 3*V+2, 3*V])

graph = csr_matrix((np.ones_like(rows), (rows, cols)), (3*N+3, 3*N+3))
dist = csgraph.dijkstra(graph, indices = 3*S)

answer = -1 if dist[3*T] > 3*N else int(dist[3*T]//3)
print(answer)

Buggy submission (verdict: Runtime Error):
# 頂点を3回ずつに持つ (x歩目の着地）
# 最短路

import sys
import numpy as np
from scipy.sparse import *
input = sys.stdin.readline
N,M = map(int,input().split())
UV = np.array([input().split() for _ in range(M)], dtype=np.int32)
U = UV[:,0]
V = UV[:,1]
S,T = map(int,input().split())

rows = np.concatenate([3*U, 3*U+1, 3*U+2])
cols = np.concatenate([3*V+1, 3*V+2, 3*V])

graph = csr_matrix((np.ones_like(rows), (rows, cols)), (3*N+3, 3*N+3))
dist = csgraph.dijkstra(graph, indices = 3*S)

answer = -1 if dist[3*T] > 3*N else int(dist[3*T]//3)
print(answer)