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

Fixed submission:
S = input()
T = input()

partial_s = []
for i in range(len(S)-len(T)+1):
    partial_s.append(S[i:i+len(T)])
to_replace_idxs = []

for idx, sub_s in enumerate(partial_s):
    for i in range(len(sub_s)):
        flag = 1
        if sub_s[i] != T[i]:
            if sub_s[i] != "?":
                flag = 0
                break
    if flag:
        to_replace_idxs.append(idx)

tmp_ans = []
if not to_replace_idxs:
    print("UNRESTORABLE")
else:
    for to_replace_idx in to_replace_idxs:
        tmp_string = S
        tmp_string = list(tmp_string)
        for i in range(to_replace_idx, to_replace_idx+len(T)):
            tmp_string[i] = T[i-to_replace_idx]
            
        tmp_string = "".join(tmp_string)
        tmp_string = tmp_string.replace("?", "a")
        tmp_ans.append(tmp_string)
    print(min(tmp_ans))

Buggy submission (verdict: Runtime Error):
Sd = input()
T = input()
Sd = list(Sd)
N = len(Sd)

start_idxs = []
is_t_ing = 0
counter = 0
for i in range(N):
    if counter == len(T):
        is_t_ing = 0
        counter = 0

    if not is_t_ing:
        if Sd[i] == T[0]:
            is_t_ing = 1
            start_idxs.append(i)
            counter = 1
    else:
        if Sd[i] == "?" or Sd[i] == T[counter]:
            counter += 1
        else:
            is_t_ing = 0
            counter = 0
            start_idxs.pop()
S_list = []
if len(start_idxs) > 0:
    for start_idx in start_idxs:
        tmp_string = Sd.copy()
        for i in range(len(T)):
            tmp_string[start_idx + i] = T[i]
        tmp_string = "".join(tmp_string)
        tmp_string = tmp_string.replace("?", "a")
        S_list.append(tmp_string)
    ans = min(S_list)
    print(ans)
else:
    tmp_string = Sd.copy()
    tmp_string = "".join(tmp_string)
    tmp_string = tmp_string.replace("?"*len(T), T)
    if tmp_string == "".join(Sd):
        print("UNRESTORABLE")
    else:
        print(tmp_string.replace("?", "a"))
Problem p03644 (python)

Fixed submission:
N = int(input())

l = [i for i in range(1, N+1)]
l1 = l.copy()
c = [0]*N

while True:
    exist_even = False
    for i in range(N):
        if l[i] % 2 == 0:
            exist_even = True
    if not exist_even:
        break

    for i in range(N):
        if l[i] % 2 == 0:
            l[i] = l[i] // 2
            c[i] += 1

idx = c.index(max(c))
print(l1[idx])

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

l = [i for i in range(1, N+1)]
l1 = l.copy()
c = [0]*N

while True:
    exist_even = False
    for i in range(N):
        if l[i] % 2 == 0:
            exist_even = True
    if not exist_even:
        break

    for i in range(N):
        if l[i] % 2 == 0:
            l[i] = l[i] // 2
            c[i] += 1

idx = c.index(max(c))
print(l1[idx])
Problem p03665 (python)

Fixed submission:
from scipy.special import comb
N, P = map(int, input().split())
A = list(map(int, input().split()))
MAX_C = 100

# def comb_dp():
#     """
#     dpテーブルを作ってnCkを計算.
#     modのpは素数でなくてよい.
#     1 <= k <= n <= 2000程度
#     O(MAX_C ** 2).
#     """
#     Com = [[0]*MAX_C for _ in range(MAX_C)]
#     for i in range(1, MAX_C):
#         # iC0 = 1
#         Com[i][0] = 1
#         for j in range(1, MAX_C):
#             # iCj = i-1_C_j-1 + i-1_C_j mod p
#             Com[i][j] = (Com[i-1][j-1] + Com[i-1][j])
        
#     return Com
# Com = comb_dp()
# print(Com[2][1])
# print(Com[2][2])
if P == 0:
    is_even = [A[i]%2==0 for i in range(N)]
    num_e = is_even.count(True)
    num_o = N-num_e
    tmp1 = 0
    tmp2 = 0
    for i in range(0, num_e+1):
        tmp1 += comb(num_e, i, exact=True)
    for i in range(0, num_o+1, 2):
        tmp2 += comb(num_o, i, exact=True)
    print(tmp1*tmp2)
else:
    is_even = [A[i]%2==0 for i in range(N)]
    num_e = is_even.count(True)
    num_o = N-num_e
    tmp1 = 0
    tmp2 = 0
    for i in range(0, num_e+1):
        tmp1 += comb(num_e, i, exact=True)
    for i in range(1, num_o+1, 2):
        tmp2 += comb(num_o, i, exact=True)
    print(tmp1*tmp2)

Buggy submission (verdict: Runtime Error):
from scipy.special import comb
N, P = map(int, input().split())
A = list(map(int, input().split()))
MAX_C = 100

# def comb_dp():
#     """
#     dpテーブルを作ってnCkを計算.
#     modのpは素数でなくてよい.
#     1 <= k <= n <= 2000程度
#     O(MAX_C ** 2).
#     """
#     Com = [[0]*MAX_C for _ in range(MAX_C)]
#     for i in range(1, MAX_C):
#         # iC0 = 1
#         Com[i][0] = 1
#         for j in range(1, MAX_C):
#             # iCj = i-1_C_j-1 + i-1_C_j mod p
#             Com[i][j] = (Com[i-1][j-1] + Com[i-1][j])
        
#     return Com
# Com = comb_dp()
# print(Com[2][1])
# print(Com[2][2])
if P == 0:
    is_even = [A[i]%2==0 for i in range(N)]
    num_e = is_even.count(True)
    num_o = N-num_e
    tmp1 = 0
    tmp2 = 0
    for i in range(0, num_e+1):
        tmp1 += comb(num_e, i, exact=True)
    for i in range(0, num_o+1, 2):
        tmp2 += comb(num_o, i, exact=True)
    print(tmp1*tmp2)
else:
    is_even = [A[i]%2==0 for i in range(N)]
    num_e = is_even.count(True)
    num_o = N-num_e
    tmp1 = 0
    tmp2 = 0
    for i in range(0, num_e+1):
        tmp1 += comb(num_e, i, exact=True)
    for i in range(1, num_o+1, 2):
        tmp2 += comb(num_o, i, exact=True)
    print(tmp1*tmp2)
Problem p03699 (python)

Fixed submission:
N=int(input())
s=[int(input()) for _ in range(N)]
ss = [s[i] for i in range(N) if s[i]%10!=0]
if sum(s)%10 != 0:
    print(sum(s))
else:
    if not ss:
        print(0)
    else:
        print(sum(s) - min(ss))

Buggy submission (verdict: Runtime Error):
N=int(input())
s=[int(input()) for _ in range(N)]
ss = [s[i] for i in range(N) if s[i]%10!=0]
if sum(s)%10 != 0:
    print(sum(s))
else:
    print(sum(s)-min(ss))
Problem p03846 (python)

Fixed submission:
N = int(input())
A=list(map(int, input().split()))
from collections import Counter


c = Counter(A)
MOD = 10**9 + 7
if N % 2 == 0:
    is_ok = 1
    cnt = 0
    for num in range(1, N, 2):
        cnt += 1
        if c[num] != 2:
            is_ok=0
            break
    if not is_ok:
        print(0)
        exit()
    print(pow(2, cnt, MOD))
else:
    is_ok = 1
    cnt = 0
    if c[0] != 1:
        is_ok = 0
    for num in range(2, N, 2):
        cnt += 1
        if c[num] != 2:
            is_ok = 0
            break
    if not is_ok:
        print(0)
        exit()
    print(pow(2, cnt, MOD))

Buggy submission (verdict: Runtime Error):
N = int(input())
A=list(map(int, input().split()))
from collections import Counter


c = dict(Counter(A))
MOD = 10**9 + 7
if N % 2 == 0:
    is_ok = 1
    cnt = 0
    for num in range(1, N, 2):
        cnt += 1
        if c[num] != 2:
            is_ok=0
            break
    if not is_ok:
        print(0)
        exit()
    print(pow(2, cnt, MOD))
else:
    is_ok = 1
    cnt = 0
    if c[0] != 1:
        is_ok = 0
    for num in range(2, N, 2):
        cnt += 1
        if c[num] != 2:
            is_ok = 0
            break
    if not is_ok:
        print(0)
        exit()
    print(pow(2, cnt, MOD))
Problem p02658 (python)

Fixed submission:
N=int(input())
A=list(map(int,input().split()))
A.sort()
ans=A.pop(-1)
if 0 in A:
    print(0)
else:
    for i in range(N-1):
        ans=ans*A[N-(i+2)]
        if ans > (10**18): break
        if ans == 0: break
    print(ans if ans<=10**18 else -1)

Buggy submission (verdict: Runtime Error):
N=int(input())
A=list(map(int,input().split()))
if 0 in A: print(0)
else:
    A.sort()
    ans=A.pop(-1)
    for i in range(N-1):
        ans=ans*A[N-(i+2)]
        if ans > (10**18): break
        if ans == 0: break
print(ans if ans<=10**18 else -1)
Problem p02675 (python)

Fixed submission:
N=input()
if int(N[len(N)-1]) in [2,4,5,7,9]: print('hon')
elif int(N[len(N)-1]) in [0,1,6,8]: print('pon')
else: print('bon')

Buggy submission (verdict: Runtime Error):
N=int(input())
if N in [2,4,5,7,9]: print(N+str(N)+'hon')
elif N in [0,1,6,8]: print(N+str(N)+'pon')
else: print(N+str(N)+'bon')
Problem p02687 (python)

Fixed submission:
S=input()
print('ABC' if S=='ARC' else 'ARC')

Buggy submission (verdict: Runtime Error):
S=input()
print('ABC' if S='ARC' else 'ARC')
Problem p02741 (python)

Fixed submission:
K=int(input())
l=[1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51]
print(l[K-1])

Buggy submission (verdict: Runtime Error):
K=int(input())
l=[1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51]
print(l[K])
Problem p02761 (python)

Fixed submission:
N,M=map(int,input().split())
s,c=[0]*M,[0]*M
for i in range(M):
    s[i],c[i] = map(int,input().split())
for i in range(10**N):
    if len(str(i)) != N:
        pass
    else:
        num=list(str(i))
        ok = True
        for j in range(M):
            if num[s[j]-1] != str(c[j]):
                ok = False
        if ok:
            print(i)
            exit()
print(-1)

Buggy submission (verdict: Runtime Error):
N,M=map(int,input().split())
sc=[]
ans=[]
for i in range(M):
    sc.append(list(map(int,input().split())))
    ans.append(0)
for i in sc:
    if ans[i[0]-1] > i[1] or ans[i[0]-1]==0:
        ans[i[0]-1]=i[1]
stack=""
for i in ans:
    stack=stack+str(i)
stack=int(stack)
stack=str(stack)
if len(stack) == N and stack != "0":
    print(stack)
else:
    print(-1)

Buggy submission (verdict: Runtime Error):
N,M=map(int,input().split())
sc=[]
ans=[]
for i in range(M):
    sc.append(list(map(int,input().split())))
    ans.append(0)
for i in sc:
    ans[i[0]-1]=i[1]
stack=""
for i in ans:
    stack=stack+str(i)
stack=int(stack)
stack=str(stack)
if len(stack) == N and stack != "0":
    print(stack)
else:
    print(-1)
Problem p02801 (python)

Fixed submission:
C = input()
char = "abcdefghijklmnopqrstuvwxyz"
L = []

for i in range(len(char)):
    L.append(char[i])

print(L[L.index(C)+1])

Buggy submission (verdict: Runtime Error):
#include<iostream>
using namespace std;

int main(){
    char C;
    cin >> C;
    C ++;
    cout << C << "\n";
    return 0;
}
Problem p02802 (python)

Fixed submission:
N,M=map(int,input().split())
ac=[0]*N
wa=[0]*N
pena=0
acsum=0
for i in range(M):
    p,S=input().split()
    p=int(p)-1
    if S == 'AC':
        if ac[p]==0:
            ac[p]=1
            pena+=wa[p]
            acsum+=1
    else:
        wa[p]+=1
print(acsum,pena)

Buggy submission (verdict: Runtime Error):
N,M = map(int,input().split(" "))
p = []
S = []
flag_AC = []
flag_WA = []

for i in range(M):
    inp = input().split(" ")
    p.append(int(inp[0]))
    S.append(inp[1])

for i in range(N):
    flag_AC.append(0)
    flag_WA.append(0)

# n番目の提出ステータスを取得
def Anc(n):
    L = []
    L.append(p[n])
    L.append(S[n])
    return L

AC = 0
WA = 0

for i in range(M):
    data = Anc(i)
    if data[1] == "AC":
        if flag_AC[data[0]] == 0:
            flag_AC[data[0]] = 1
            AC = AC + 1

    if data[1] == "WA":
        if flag_AC[data[0]] == 0:
            flag_WA[data[0]] = flag_WA[data[0]] + 1

for i in range(M):
    data = Anc(i)
    if flag_AC[data[0]] == 0:
        flag_WA[data[0]] = 0
    else:
        flag_WA[data[0]] = flag_WA[data[0]] + 1

print(AC,WA)

Buggy submission (verdict: Runtime Error):
N,M = map(int,input().split(" "))
p = []
S = []
flag_AC = []
flag_WA = []

for i in range(M):
    inp = input().split(" ")
    p.append(int(inp[0]))
    S.append(inp[1])

for i in range(N):
    flag_AC.append(0)
    flag_WA.append(0)

# n番目の提出ステータスを取得
def Anc(n):
    L = []
    L.append(p[n])
    L.append(S[n])
    return L

AC = 0
WA = 0

for i in range(M):
    data = Anc(i)
    if data[1] == "AC":
        if flag_AC[data[0]] == 0:
            flag_AC[data[0]] = 1
            AC = AC + 1

    if data[1] == "WA":
        if flag_AC[data[0]] == 0:
            flag_WA[data[0]] = flag_WA[data[0]] + 1
            WA = WA+1

print(AC,WA)

Buggy submission (verdict: Runtime Error):
N,M = map(int,input().split(" "))
p = []
S = []
flag_AC = []
flag_WA = []

for i in range(M):
    inp = input().split(" ")
    p.append(int(inp[0]))
    S.append(inp[1])
    flag_AC.append(0)
    flag_WA.append(0)

# n番目の提出ステータスを取得
def Anc(n):
    L = []
    L.append(p[n])
    L.append(S[n])
    return L

AC = 0
WA = 0

for i in range(M):
    data = Anc(i)
    if data[1] == "AC":
        if flag_AC[data[0]] == 0:
            flag_AC[data[0]] = 1
            AC = AC + 1

    if data[1] == "WA" and flag_AC[data[0]] == 0:
        flag_WA[data[0]] = flag_WA[data[0]] + 1
        WA = WA+1

print(AC,WA)
Problem p02817 (python)

Fixed submission:
inp = str(input()).split(" ")
print(inp[1]+inp[0])

Buggy submission (verdict: Runtime Error):
inp = input().split(" ")
takahashi = int(inp[0])
aoki = int(inp[1])
K = int(inp[2])

for i in range(K):
    if takahashi > 0 :
        takahashi = takahashi -1
    elif aoki > 0 :
        aoki = aoki-1

print(str(takahashi) + " " + str(aoki))

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

print(a+b)
Problem p02818 (python)

Fixed submission:
A,B,K = map(int,input().split(" "))
if A+B < K:print(0,0)
elif A > K:print(A-K,B)
elif A  <= K:print(0,A+B-K)
# Kenparさん、写経させていただきました。ありがとうございます。
# こんな解法があったなんて・・・

Buggy submission (verdict: Runtime Error):
class AC:
    @staticmethod
    def inputStr(char):
        return input().split(char)
    
    @staticmethod
    def inputInt(char):
        a = input().split(char)
        for i in range(len(a)):
            a[i] = int(a[i])
        return a

inp = AC.inputStr(" ")
A = inp[0]
B = inp[1]
K = inp[2]

for i in range(K):
    if A > 0 :
        A = A-1
    elif B > 0 :
        B = B-1
print(str(A) + " " + str(B))
Problem p02909 (python)

Fixed submission:
S=input()
l=['Sunny','Cloudy','Rainy']
print(l[l.index(S)+1] if S!= 'Rainy' else 'Sunny')

Buggy submission (verdict: Runtime Error):
S=input()
l=['Sunny','Cloudy','Rainy']
print(l[l.index(S)+1])
Problem p02995 (python)

Fixed submission:
import fractions
a,b,c,d=map(int,input().split())
g=fractions.gcd(c,d)
l=c*d//g
print(b-b//c-b//d+b//l-(a-1-(a-1)//c-(a-1)//d+(a-1)//l))

Buggy submission (verdict: Runtime Error):
import math
def lcm(n,m): return n*m//math.gcd(n,m)
A,B,C,D=map(int,input().split())
print(((B-A)-(((B-A)//C)+((B-A)//D)))+((B-A)//lcm(C,D)))
Problem p03262 (python)

Fixed submission:
import math

def agcd(l):
    a=math.gcd(l[0],l[1])
    for i in range(2,len(l)):
        a=math.gcd(a,l[i])
    return a

N,X=map(int,input().split())
x=[abs(i-X) for i in list(map(int,input().split()))]

print(agcd(x)if 1<N else x[0])

Buggy submission (verdict: Runtime Error):
import math

def agcd(l):
    a=math.gcd(l[0],l[1])
    for i in range(2,len(l)):
        a=math.gcd(a,l[i])
    return a

N,X=map(int,input().split())
print(agcd([abs(i-X) for i in list(map(int,input().split()))]))
Problem p03280 (python)

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

Buggy submission (verdict: Runtime Error):
print((int(input())-1)*(int(input())-1))
Problem p03361 (python)

Fixed submission:
H,W=map(int,input().split())
s=[list(input())for i in range(H)]
c=[['.']*W for i in range(H)]
for i in range(H):
    countX=0
    nowX=s[i][0]
    for j in range(W):
        if nowX==s[i][j]:
            countX+=1
            if 1<countX and nowX=='#':
                c[i][j-1],c[i][j]='#','#'
        else:
            countX=1
        nowX=s[i][j]
for i in range(W):
    countY=0
    nowY=s[0][i]
    for j in range(H):
        if nowY==s[j][i]:
            countY+=1
            if 1<countY and nowY =='#':
                c[j-1][i],c[j][i]='#','#'
        else:
            countY=1
        nowY=s[j][i]
print('Yes' if c==s else 'No')

Buggy submission (verdict: Runtime Error):
H,W=map(int,input().split())
s=[list(input())for i in range(H)]
c=[['.']*W for i in range(H)]
for i in range(H):
    countX=0
    nowX=s[i][0]
    countY=0
    nowY=s[0][i]
    for j in range(W):
        if nowX==s[i][j]:
            countX+=1
            if 1<countX and nowX=='#':
                c[i][j-1],c[i][j]='#','#'
        else:
            countX=1
        if nowY==s[j][i]:
            countY+=1
            if 1<countY and nowY =='#':
                c[j-1][i],c[j][i]='#','#'
        else:
            countY=1
        nowX=s[i][j]
        nowY=s[j][i]
print('Yes' if c==s else 'No')
Problem p03605 (python)

Fixed submission:
N=list(input())
print('Yes' if '9' in N else 'No')

Buggy submission (verdict: Runtime Error):
import math
def lcm(n,m): return n*m//math.gcd(n,m)
A,B,C,D=map(int,input().split())
print(((B-A)-(((B-A)//C)+((B-A)//D)))+((B-A)//lcm(C,D)))
Problem p03633 (python)

Fixed submission:
import math
def lcm(l):
    r=l.pop(0)
    for i in l:
        r=r*i//math.gcd(r,i)
    return r

N=int(input())
T=[]
for i in range(N):
    T.append(int(input()))
print(lcm(T))

Buggy submission (verdict: Runtime Error):
import math
def lcm(l):
    r=l.pop(0)
    for i in l:
        r=r*i//math.gcd(r,i)
    return r*i//math.gcd(r,i)

N=int(input())
T=[]
for i in range(N):
    T.append(int(input()))
print(lcm(T))
Problem p03694 (python)

Fixed submission:
N=int(input())
a=list(set(list(map(int,input().split()))))
ans=0
a.sort()
for i in range(1,len(a)):
    ans+=a[i]-a[i-1]
print(ans)

Buggy submission (verdict: Runtime Error):
N=int(input())
a=list(set(list(map(int,input().split()))))
ans=0
a.sort()
for i in range(1,N):
    ans+=a[i]-a[i-1]
print(ans)
Problem p03815 (python)

Fixed submission:
print((2*int(input())-2)//11+1)

Buggy submission (verdict: Runtime Error):
print((2*input()-2)//11+1)
Problem p03844 (python)

Fixed submission:
l=input().split()
print(int(l[0])+int(l[2]) if l[1] == '+' else int(l[0])-int(l[2]))

Buggy submission (verdict: Runtime Error):
l=input().split()
print(l[0]+l[2] if l[1] == '+' else l[0]-l[2])
Problem p02666 (python)

Fixed submission:
def par(a):
    L = []
    while P[a] != a:
        L.append(a)
        a = P[a]
    for l in L:
        P[l] = a
    return a
def unite(a, b):
    pa = par(a)
    pb = par(b)
    if pa == pb: return
    if LEN[pa] < LEN[pb]:
        a, b, pa, pb = b, a, pb, pa
    P[pb] = pa
    if LEN[pa] == LEN[pb]: LEN[pa] += 1
    CNT[pa] += CNT[pb]
def cnt(a):
    return CNT[par(a)]

N = int(input())
P = [i for i in range(N)]
LEN = [1] * N
CNT = [1] * N
FULL = [0] * N
A = [int(a) - 1 for a in input().split()]
for i, a in enumerate(A):
    if a < 0: continue
    if par(i) != par(a):
        unite(i, a)
    else:
        FULL[i] = 1
for i in range(N):
    if FULL[i]:
        FULL[par(i)] = 1
X = []
Y = []
for i in range(N):
    if par(i) == i:
        if FULL[i] == 0:
            X.append(CNT[i])
        else:
            Y.append(CNT[i])
M = len(X)
mod = 10 ** 9 + 7
K = 96
m = int(("1" * 32 + "0" * 64) * 5050, 2)
pa = (1 << 64) - ((1 << 64) % mod)
modP = lambda x: x - ((x & m) >> 64) * pa
ans = (sum(X) + sum(Y) - len(Y)) * pow(N - 1, M, mod) % mod
x = 1
for i, a in enumerate(X):
    x *= (a << K) + 1
    x = modP(x)

sx = bin(x)[2:] + "_"
L = [int(sx[-(i+1) * K - 1:-i * K - 1], 2) % mod for i in range((len(sx)+K-2) // K)]
fa = 1
ans = (ans + M * pow(N - 1, M - 1, mod)) % mod
for i, l in enumerate(L):
    if i == 0: continue
    ans = (ans - l * fa * pow(N - 1, M - i, mod)) % mod
    fa = fa * i % mod
print(ans)

Buggy submission (verdict: Runtime Error):
def par(a):
    L = []
    while P[a] != a:
        L.append(a)
        a = P[a]
    for l in L:
        P[l] = a
    return a
def unite(a, b):
    pa = par(a)
    pb = par(b)
    if pa == pb: return
    if LEN[pa] < LEN[pb]:
        a, b, pa, pb = b, a, pb, pa
    P[pb] = pa
    if LEN[pa] == LEN[pb]: LEN[pa] += 1
    CNT[pa] += CNT[pb]
def cnt(a):
    return CNT[par(a)]

N = int(input())
P = [i for i in range(N)]
LEN = [1] * N
CNT = [1] * N
FULL = [0] * N
A = [int(a) - 1 for a in input().split()]
for i, a in enumerate(A):
    if a < 0: continue
    if par(i) != par(a):
        unite(i, a)
    else:
        FULL[i] = 1
for i in range(N):
    if FULL[i]:
        FULL[par(i)] = 1
X = []
Y = []
for i in range(N):
    if par(i) == i:
        if FULL[i] == 0:
            X.append(CNT[i])
        else:
            Y.append(CNT[i])
M = len(X)
mod = 10 ** 9 + 7
K = 96
m = int(("1" * 32 + "0" * 64) * 5050, 2)
pa = (1 << 64) - ((1 << 64) % mod)
modP = lambda x: x - ((x & m) >> 64) * pa
ans = (sum(X) + sum(Y) - len(Y)) * pow(N - 1, M, mod) % mod
x = 1
for i, a in enumerate(X):
    x *= (a << K) + 1
    x = modP(x)

sx = bin(x)[2:] + "_"
L = [int(sx[-(i+1) * K - 1:-i * K - 1], 2) % mod for i in range((len(sx)+K-2) // K)]
fa = 1
ans = (ans + M * pow(N - 1, M - 1, mod)) % mod
for i, l in enumerate(L):
    if i == 0: continue
    ans = (ans - l * fa * pow(N - 1, M - i, mod)) % mod
    fa = fa * i % mod
print(ans)
Problem p02670 (python)

Fixed submission:
from collections import deque
N = int(input())
A = [int(a)-1 for a in input().split()]
M = N + 2
X = [max(min(i-1, j-1, M-2-i, M-2-j), -1) for j in range(M) for i in range(M)]
Y = [min(x, 0) + 1 for x in X]

def calc(i0):
    h = X[i0]
    if h < 0: return
    Q = deque([(i0, h)])
    X[i0] -= 1
    Y[i0] = 0
    dd = (-1, 1, M, -M)
    while Q:
        i, h = deque.popleft(Q)
        for d in dd:
            j = i + d
            y = Y[j]
            if X[j] >= h + y:
                X[j] -= 1
                deque.append(Q, (j, h + y))
ans = 0
for a in A:
    aa = (a // N + 1) * M + a % N + 1
    ans += X[aa]
    calc(aa)
print(ans)

Buggy submission (verdict: Runtime Error):
from numba import njit

@njit('(i4,i4,i4[::1],i4[::1],i4[::1])', cache=True)
def calc(N, M, A, X, Y):
    ans = 0
    for a in A:
        aa = (a // N + 1) * M + a % N + 1
        ans += X[aa]
        h = X[aa]
        if h < 0: continue
        Q = [(aa, h)]
        q = 0
        X[aa] -= 1
        Y[aa] = 0
        dd = (-1, 1, M, -M)
        while q < len(Q):
            i, h = Q[q]
            q += 1
            for d in dd:
                j = i + d
                y = Y[j]
                if X[j] >= h + y:
                    X[j] -= 1
                    Q.append((j, h + y))
    return ans

N = int(input())
A = [int(a)-1 for a in input().split()]
M = N + 2
X = [max(min(i-1, j-1, M-2-i, M-2-j), -1) for j in range(M) for i in range(M)]
Y = [min(x, 0) + 1 for x in X]
print(calc(N, M, A, X, Y))

Buggy submission (verdict: Runtime Error):
from numba import njit

@njit('(i4,i4,i4[::-1],i4[::-1],i4[::-1])', cache=True)
def calc(N, M, A, X, Y):
    ans = 0
    for a in A:
        aa = (a // N + 1) * M + a % N + 1
        ans += X[aa]
        h = X[aa]
        if h < 0: continue
        Q = [(aa, h)]
        q = 0
        X[aa] -= 1
        Y[aa] = 0
        dd = (-1, 1, M, -M)
        while q < len(Q):
            i, h = Q[q]
            q += 1
            for d in dd:
                j = i + d
                y = Y[j]
                if X[j] >= h + y:
                    X[j] -= 1
                    Q.append((j, h + y))
    return ans

N = int(input())
A = [int(a)-1 for a in input().split()]
M = N + 2
X = [max(min(i-1, j-1, M-2-i, M-2-j), -1) for j in range(M) for i in range(M)]
Y = [min(x, 0) + 1 for x in X]
print(calc(N, M, A, X, Y))
Problem p02690 (python)

Fixed submission:
def f(n):
    l, r = 0, 1 << 20
    while r - l > 1:
        m = (l + r) // 2
        if m ** 5 <= n:
            l = m
        else:
            r = m
    return l if l ** 5 == n else 0

X = int(input())
for i in range(1 << 20):
    a = f(X + i ** 5)
    if a:
        print(a, i)
        break
    a = f(X - i ** 5)
    if a:
        print(a, -i)
        break

Buggy submission (verdict: Runtime Error):
def calc(n):
    x = 1 << (n.bit_length() + 4) // 5
    y = (4 * x + n // (x ** 4)) // 5
    while y < x:
        x = y
        y = (4 * x + n // (x ** 4)) // 5
    return x
def f(n):
    c = calc(n)
    return c if c ** 5 == n else 0

X = int(input())
for i in range(1 << 10):
    a = f(X + i ** 5)
    if a:
        print(a, i)
        break
    a = f(X - i ** 5)
    if a:
        print(a, -i)
        break