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

Fixed submission:
N=input()

S=0
for a in N:
    S+=int(a)

if S%9:
    print("No")
else:
    print("Yes")

Buggy submission (verdict: Runtime Error):
def f(S):
    if S=="":
        return 0
    else:
        return f(S[:-1])+int(S[-1])

N=input()
if f(N)%9:
    print("No")
else:
    print("Yes")
Problem p02685 (python)

Fixed submission:
#入力部
C= 998244353
N,M,K=map(int,input().split())

#定義部
U=max(N+1,K)
F=[0]*U
G=[0]*U

F[0]=1
for i in range(1,U):
    F[i]=(F[i-1]*i)%C

G[i]=pow(F[-1],C-2,C)

for j in range(U-2,-1,-1):
    G[j]=(G[j+1]*(j+1))%C

def nCr(n,r):
    if r<0 or n<r:
        return 0
    else:
        return (F[n]*G[r]*G[n-r])

#メイン部
S=0
for k in range(0,K+1):
    S=(S+M*pow(M-1,N-k-1,C)*nCr(N-1,k))%C
    
print(S%C)

Buggy submission (verdict: Runtime Error):
#入力部
C= 998244353
N,M,K=map(int,input().split())

#定義部
U=max(N,K)
F=[0]*U
G=[0]*U

F[0]=1
for i in range(1,U):
    F[i]=(F[i-1]*i)%C

G[i]=pow(F[-1],C-2,C)

for j in range(U-2,-1,-1):
    G[j]=(G[j+1]*(j+1))%C

def nCr(n,r):
    if r<0 or n<r:
        return 0
    else:
        return (F[n]*G[r]*G[n-r])

#メイン部
S=0
for k in range(0,K+1):
    S=(S+M*pow(M-1,N-k-1,C)*nCr(N-1,k))%C
    
print(S%C)

Buggy submission (verdict: Runtime Error):
#入力部
C= 998244353
N,M,K=map(int,input().split())

#定義部
F=[0]*max(N,K)
G=[0]*max(N,K)

F[0]=1
for i in range(1,N):
    F[i]=(F[i-1]*i)%C

G[i]=pow(F[-1],C-2,C)

for j in range(N-2,-1,-1):
    G[j]=(G[j+1]*(j+1))%C

def nCr(n,r):
    if r<0 or n<r:
        return 0
    else:
        return (F[n]*G[r]*G[n-r])

#メイン部
S=0
for k in range(0,K+1):
    S=(S+M*pow(M-1,N-k-1,C)*nCr(N-1,k))%C
    
print(S%C)

Buggy submission (verdict: Runtime Error):
#入力部
C= 998244353
N,M,K=map(int,input().split())

#定義部
F=[0]*(2*10**5)
G=[0]*(2*10**5)

F[0]=1
for i in range(1,N):
    F[i]=(F[i-1]*i)%C

G[i]=pow(F[-1],C-2,C)

for j in range(N-2,-1,-1):
    G[j]=(G[j+1]*(j+1))%C

def nCr(n,r):
    if r<0 or n<r:
        return 0
    else:
        return (F[n]*G[r]*G[n-r])

#メイン部
S=0
for k in range(0,K+1):
    S=(S+M*pow(M-1,N-k-1,C)*nCr(N-1,k))%C
    
print(S%C)

Buggy submission (verdict: Runtime Error):
#入力部
C= 998244353
N,M,K=map(int,input().split())

#定義部
F=[0]*N
G=[0]*N

F[0]=1
for i in range(1,N):
    F[i]=(F[i-1]*i)%C

G[i]=pow(F[-1],C-2,C)

for j in range(N-2,-1,-1):
    G[j]=(G[j+1]*(j+1))%C

def nCr(n,r):
    if r<0 or n<r:
        return 0
    else:
        return (F[n]*G[r]*G[n-r])

#メイン部
S=0
for k in range(0,K+1):
    S=(S+M*pow(M-1,N-k-1,C)*nCr(N-1,k))%C
    
print(S%C)
Problem p02793 (python)

Fixed submission:
from math import gcd

def mod_prod(X,M):
    c=1
    for x in X:
        c*=x
        c%=M
    return c

N=int(input())
A=list(map(int,input().split()))
Mod=10**9+7

E=[0]*N
a=1
for i in range(N):
    E[i]=A[i]//gcd(a,A[i])
    a*=E[i]

P=mod_prod(E,Mod)

Q=0
for a in A:
    Q+=pow(a,Mod-2,Mod)
    Q%=Mod
print((P*Q)%Mod)

Buggy submission (verdict: Runtime Error):
from math import gcd

def lcm(X):
    m=(X[0]*X[1])//gcd(X[0],X[1])

    for i in range(2,len(X)):
        g=gcd(m,X[i])
        m=(m*X[i])//g
    return m


N=map(int,input().split())
A=list(map(int,input().split()))
Mod=10**9+7

M=lcm(A)
K=0
for a in A:
    K+=M//a
    K%=Mod
print(K)
Problem p02833 (python)

Fixed submission:
import math
N = int(input())

k = 0
if N%2 ==0:
  n = N//2
  i = 5
  while n >= i :
    k += n //i
    i *= 5

print(k)

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

k = 0
if N%2 ==0:
  n = N//2
  i = 1
  while n　>= 5**i :
    k += math.floor(n /(5**i))
    i += 1

print(k)
Problem p02880 (python)

Fixed submission:
N = int(input())
k = 0
for i in range(9):
  for j in range(9):
    if (i+1)*(j+1) == N:
      k += 1
if k ==0:
  print('No')
else:
  print('Yes')

Buggy submission (verdict: Runtime Error):
N = int(input())
k = 0:
  for i in range(9):
    for j in range(9):
      if (i+1)*(j+1) == N:
        k += 1
if k ==0:
  print('No')
else:
  print('Yes')
Problem p03013 (python)

Fixed submission:
N,M = map(int, input().split())
A = [0]*M
for i in range(M):
    A[i] = int(input())
if M != 0:
    
    L = A[0]+1
    if L < N - A[M-1]:
        L = N - A[M-1]
    for i in range(M-1):
        L1 = A[i+1]-A[i]
        if L1 > L:
            L = L1
else:
    L = N +2
    
x = [0]*L
x.insert(0,0)
x.insert(1,1)
if L >2:
    for i in range(L-2):
        x[i+2] = x[i+1] + x[i]
if M != 0:
    S = x[A[0]] * x[N - A[M-1]]
    for i in range(M-1):
        S *= x[A[i+1]-A[i]-1]
else:
    S = x[N+1]
print(S%1000000007)

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

L = A[0]+1
if L < N - A[M-1]:
    L = N - A[M-1]
for i in range(M-1):
    L1 = A[i+1]-A[i]
    if L1 > L:
        L = L1
        
x = [0]*L
x.insert(0,0)
x.insert(1,1)
if L >2:
    for i in range(L-2):
        x[i+2] = x[i+1] + x[i]
    
S = x[A[0]] * x[N - A[M-1]]
for i in range(M-1):
    S *= x[A[i+1]-A[i]-1]
    
print(S%1000000007)

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

L = A[0]+2
if L < N - A[M-1]:
    L = N - A[M-1]
for i in range(M-1):
    L1 = A[i+1]-A[i]
    if L1 > L:
        L = L1
        
x = [0]*L
x.insert(0,0)
x.insert(1,1)
for i in range(L-2):
    x[i+2] = x[i+1] + x[i]
    
S = x[A[0]] * x[N - A[M-1]]
for i in range(M-1):
    S *= x[A[i+1]-A[i]-1]
    
print(S%1000000007)

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

L = A[0]+2
if L < N - A[M-1]:
    L = N - A[M-1]
for i in range(M-1):
    L1 = A[i+1]-A[i]
    if L1 > L:
        L = L1
        
x = [0]*L
x.insert(0,0)
x.insert(1,1)
for i in range(L-2):
    x[i+2] = x[i+1] + x[i]
    
S = x[A[0]] * x[N - A[M-1]]
for i in range(M-1):
    S *= x[A[i+1]-A[i]-1]
    
print(S%1000000007)
Problem p02679 (python)

Fixed submission:
import sys
input = sys.stdin.readline
from math import gcd
def solve():
    N = int(input())
    AB = [tuple(map(int,input().split())) for i in range(N)]
    MOD = 10**9+7

    from collections import Counter
    ctr = Counter()
    az = bz = zz = 0
    for a,b in AB:
        if a==b==0:
            zz += 1
        elif a==0:
            az += 1
        elif b==0:
            bz += 1
        else:
            g = gcd(a,b)
            a,b = a//g, b//g
            if b < 0:
                b *= -1
                a *= -1
            ctr[(a,b)] += 1
    ans = pow(2,az,MOD) + pow(2,bz,MOD) - 1

    used = set()
    for (a1,b1),v in ctr.items():
        if (a1,b1) in used: continue
        a2,b2 = -b1,a1
        if b2 < 0:
            b2 *= -1
            a2 *= -1
        if (a2,b2) in ctr:
            used.add((a2,b2))
        r = pow(2,v,MOD) + pow(2,ctr[(a2,b2)],MOD) - 1
        ans *= r
        ans %= MOD
    ans += zz - 1
    ans %= MOD
    print(ans)
solve()

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
from fractions import Fraction
def solve():
    N = int(input())
    AB = [tuple(map(int,input().split())) for i in range(N)]
    MOD = 10**9+7

    from collections import Counter
    ctr = Counter()
    az = bz = zz = 0
    for a,b in AB:
        if a==b==0:
            zz += 1
        elif a==0:
            az += 1
        elif b==0:
            bz += 1
        else:
            f = Fraction(a,b)
        ctr[f] += 1
    ans = pow(2,az,MOD) + pow(2,bz,MOD) - 1

    used = set()
    for f1,v in ctr.items():
        if f1 in used: continue
        f2 = -1/f1
        if f2 in ctr:
            used.add(f2)
        r = pow(2,v,MOD) + pow(2,ctr[f2],MOD) - 1
        ans *= r
        ans %= MOD
    ans += zz - 1
    ans %= MOD
    print(ans)
solve()

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
from fractions import Fraction
def solve():
    N = int(input())
    AB = [tuple(map(int,input().split())) for i in range(N)]
    MOD = 10**9+7

    from collections import Counter
    ctr = Counter()
    az = bz = zz = 0
    for a,b in AB:
        if a==b==0:
            zz += 1
        elif a==0:
            az += 1
        elif b==0:
            bz += 1
        f = Fraction(a,b)
        ctr[f] += 1
    ans = pow(2,az,MOD) + pow(2,bz,MOD) - 1

    used = set()
    for f1,v in ctr.items():
        if f1 in used: continue
        f2 = -1/f1
        if f2 in ctr:
            used.add(f2)
        r = pow(2,v,MOD) + pow(2,ctr[f2],MOD) - 1
        ans *= r
        ans %= MOD
    ans += zz - 1
    ans %= MOD
    print(ans)
solve()
Problem p02763 (python)

Fixed submission:
import sys
input = sys.stdin.readline
N = int(input())
S = input().rstrip()
Q = int(input())
qs = [input().split() for i in range(Q)]

def ctoi(c):
    return ord(c) - ord('a')

bit = [[0]*(N+2) for _ in range(26)]
def bit_add(x,w,ci):
    while x <= N+1:
        bit[ci][x] += w
        x += (x & -x)
def bit_sum(x,ci):
    ret = 0
    while x > 0:
        ret += bit[ci][x]
        x -= (x & -x)
    return ret

for i,c in enumerate(S):
    bit_add(i+1,1,ctoi(c))

s = list(S)
ans = []
for a,b,c in qs:
    if a=='1':
        x = int(b)
        bit_add(x,1,ctoi(c))
        bit_add(x,-1, ctoi(s[x-1]))
        s[x-1] = c
    else:
        tmp = 0
        for i in range(26):
            if bit_sum(int(c),i) - bit_sum(int(b)-1,i) > 0:
                tmp += 1
        ans.append(tmp)
print(*ans, sep='\n')

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
N = int(input())
S = input().rstrip()
Q = int(input())
qs = [input().split() for i in range(Q)]

def ctoi(c):
    return ord(c) - ord('a')

class BinaryIndexedTree:
    def __init__(self,size):
        self.N = size
        self.bit = [0]*size
    def add(self,x,w):
        while x < self.N:
            self.bit[x] += w
            x += (x & -x)
    def sum(self,x):
        ret = 0
        while x > 0:
            ret += self.bit[x]
            x -= (x & -x)
        return ret
bits = [BinaryIndexedTree(N) for _ in range(26)]

for i,c in enumerate(S):
    bits[ctoi(c)].add(i+1,1)

s = list(S)
ans = []
for a,b,c in qs:
    if a=='1':
        x = int(b)
        bits[ctoi(c)].add(x,1)
        bits[ctoi(s[x-1])].add(x,-1)
        s[x-1] = c
    else:
        tmp = 0
        for i in range(26):
            if bits[i].sum(int(c)) - bits[i].sum(int(b)-1) > 0:
                tmp += 1
        ans.append(tmp)
print(*ans, sep='\n')
Problem p02815 (python)

Fixed submission:
N = int(input())
C = list(map(int,input().split()))
C.sort(reverse=True)
MOD = 10**9+7
if N==1:
    print((C[0]*2)%MOD)
    exit()

ans = 0
d = pow(2,N-2,MOD)
base = pow(2,N-1,MOD)
for i,c in enumerate(C):
    ans += c * (base + i*d)
ans *= pow(2,N,MOD)
ans %= MOD
print(ans)

Buggy submission (verdict: Runtime Error):
N = int(input())
C = list(map(int,input().split()))
C.sort(reverse=True)
MOD = 10**9+7

ans = 0
d = pow(2,N-2,MOD)
base = pow(2,N-1,MOD)
for i,c in enumerate(C):
    ans += c * (base + i*d)
ans *= pow(2,N,MOD)
ans %= MOD
print(ans)
Problem p02851 (python)

Fixed submission:
N,K = map(int,input().split())
A = list(map(int,input().split()))
B = [a%K for a in A]
cs = [0]
for b in B:
    c = (cs[-1] + b-1) % K
    cs.append(c)

from collections import deque
from collections import defaultdict
qs = defaultdict(lambda: deque())

ans = 0
for i,c in enumerate(cs):
    while qs[c] and qs[c][0] + K <= i:
        qs[c].popleft()
    ans += len(qs[c])
    qs[c].append(i)
print(ans)

Buggy submission (verdict: Runtime Error):
N,K = map(int,input().split())
A = list(map(int,input().split()))
B = [a%K for a in A]
cs = [0]
for b in B:
    c = (cs[-1] + b-1) % K
    cs.append(c)

from collections import deque
mk = min(K,N+1)
qs = [deque() for i in range(mk)]

ans = 0
for i,c in enumerate(cs):
    while qs[c] and qs[c][0] + mk <= i:
        qs[c].popleft()
    ans += len(qs[c])
    qs[c].append(i)
print(ans)
Problem p02862 (python)

Fixed submission:
X,Y = map(int,input().split())
if X>Y:
    X,Y = Y,X
if(X+Y)%3:
    print(0)
    exit()
n = (X+Y)//3
if X < n:
    print(0)
    exit()

MOD = 10**9+7
r = X-n

maxn = n+5
fac = [1,1] + [0]*maxn
finv = [1,1] + [0]*maxn
inv = [0,1] + [0]*maxn
for i in range(2,maxn+2):
    fac[i] = fac[i-1] * i % MOD
    inv[i] = -inv[MOD%i] * (MOD // i) % MOD
    finv[i] = finv[i-1] * inv[i] % MOD

def ncr(n,r):
    if n < r: return 0
    if n < 0 or r < 0: return 0
    return fac[n] * (finv[r] * finv[n-r] % MOD) % MOD

print(ncr(n,r))

Buggy submission (verdict: Runtime Error):
X,Y = map(int,input().split())
MOD = 10**9+7

if (X+Y)%3:
    print(0)
    exit()
n = (X+Y)//3
r = X-n
if not 0 <= r <= n:
    print(0)

MAXN = n
inv = [0,1] + [0]*MAXN
for i in range(2,MAXN+2):
    inv[i] = -inv[MOD%i] * (MOD // i) % MOD
def comb(n,r):
    ret = 1
    for i in range(r):
        ret *= n-i
        ret *= inv[i+1]
        ret %= MOD
    return ret

print(comb(n,r))
Problem p02925 (python)

Fixed submission:
import sys
input = sys.stdin.readline
from collections import deque

N = int(input())
A = [list(reversed(list(map(lambda x:int(x)-1,input().split())))) for i in range(N)]
B = {i:row for i,row in enumerate(A)}

end = 0
for k in range(8000):
    used = set()
    dels = []
    for i,q in B.items():
        if not q: continue
        if i in used: continue
        j = q[-1]
        if j in used: continue
        if A[j][-1] == i:
            A[j].pop()
            A[i].pop()
            used.add(i)
            used.add(j)
            if not A[i]:
                end += 1
                dels.append(i)
            if not A[j]:
                end += 1
                dels.append(j)
    if not used:
        print(-1)
        exit()
    if end == N:
        print(k+1)
        exit()
    for i in dels:
        del B[i]

print(N*(N-1)//2)

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

es = [[[] for j in range(i)] for i in range(N)]
indeg = [[0]*i for i in range(N)]
for i,row in enumerate(A):
    for x,y in zip(row,row[1:]):
        x,y = x-1,y-1
        a0,b0 = (i,x) if i>x else (x,i)
        a1,b1 = (i,y) if i>y else (y,i)
        es[a0][b0].append((a1,b1))
        indeg[a1][b1] += 1

status = [[-1]*i for i in range(N)] #-1:new 0:active 1:finished
def has_cycle(v):
    a0,b0 = v
    status[a0][b0] = 0
    for a1,b1 in es[a0][b0]:
        if status[a1][b1] == 0: return True
        if status[a1][b1] == -1:
            if has_cycle((a1,b1)): return True
    status[a0][b0] = 1
    return False

for i in range(N):
    for j in range(i):
        if status[i][j] != -1: continue
        if has_cycle((i,j)):
            print(-1)
            exit()

from collections import deque
q = deque()
for i in range(N):
    for j in range(i):
        if indeg[i][j] == 0:
            q.append((i,j,1))

ans = -1
while q:
    a0,b0,d = q.popleft()
    ans = max(ans,d)
    for a1,b1 in es[a0][b0]:
        indeg[a1][b1] -= 1
        if indeg[a1][b1] == 0:
            q.append((a1,b1,d+1))
print(ans)
Problem p03055 (python)

Fixed submission:
N = int(input())
src = [tuple(map(lambda x:int(x)-1,input().split())) for i in range(N-1)]
es = [[] for i in range(N)]
for a,b in src:
    es[a].append(b)
    es[b].append(a)

def farthest_node_and_dist(s):
    dist = [N]*N
    dist[s] = 0
    stack = [s]
    ret_node = -1
    ret_dist = 0
    while stack:
        v = stack.pop()
        d = dist[v]
        for to in es[v]:
            if d+1 < dist[to]:
                dist[to] = d+1
                stack.append(to)
                if ret_dist < d+1:
                    ret_dist = d+1
                    ret_node = to
    return (ret_node, ret_dist)

n,d = farthest_node_and_dist(0)
n,d = farthest_node_and_dist(n)
print('Second' if d%3==1 else 'First')

Buggy submission (verdict: Runtime Error):
N = int(input())
src = [tuple(map(lambda x:int(x)-1,input().split())) for i in range(N-1)]
es = [[] for i in range(N)]
for a,b in src:
    es[a].append(b)
    es[b].append(a)

if all(len(e)<=2 for e in es):
    print('First' if N%2 else 'Second')
    exit()
assert False
Problem p03107 (python)

Fixed submission:
S = input()
z = S.count('0')
o = len(S) - z
print(len(S) - abs(z-o))

Buggy submission (verdict: Runtime Error):
S = input()
one = S.count('1')
zero = len(S) - one
print(min(one, zero) * 2py )
Problem p03253 (python)

Fixed submission:
N,M = map(int,input().split())
MOD = 10**9+7

from collections import Counter
def factorize(n):
    d = Counter()
    m = 2
    while m*m <= n:
        while n%m == 0:
            n //= m
            d[m] += 1
        m += 1
    if n > 1:
        d[n] += 1
    return d

fac = [1,1] + [0]*(N+100)
finv = [1,1] + [0]*(N+100)
inv = [0,1] + [0]*(N+100)
for i in range(2,N+102):
    fac[i] = fac[i-1] * i % MOD
    inv[i] = -inv[MOD%i] * (MOD // i) % MOD
    finv[i] = finv[i-1] * inv[i] % MOD

def ncr(n,r):
    if n < r: return 0
    if n < 0 or r < 0: return 0
    return fac[n] * (finv[r] * finv[n-r] % MOD) % MOD

ans = 1
facs = factorize(M)
for v in facs.values():
    ans *= ncr(N+v-1,v)
    ans %= MOD
print(ans)

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

from collections import Counter
def factorize(n):
    d = Counter()
    m = 2
    while m*m <= n:
        while n%m == 0:
            n //= m
            d[m] += 1
        m += 1
    if n > 1:
        d[n] += 1
    return d

MOD = 10**9+7

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

def ncr(n,r):
    if n < r: return 0
    if n < 0 or r < 0: return 0
    return fac[n] * (finv[r] * finv[n-r] % MOD) % MOD
def nhr(n,r):
    return ncr(n+r-1, r)


factors = factorize(M)
ans = 1
for v in factors.values():
    ans *= nhr(N,v)
    ans %= MOD
print(ans)
Problem p03275 (python)

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

cands = list(sorted(set(src)))

def enough(i):
    bit = [0] * (2*N+10)
    def bit_add(a,w):
        x = a
        while x <= (2*N+9):
            bit[x] += w
            x += (x & -x)
    def bit_sum(a):
        x = a
        ret = 0
        while x > 0:
            ret += bit[x]
            x -= (x & -x)
        return ret

    cums = [0]
    for a in src:
        tmp = 1 if a >= cands[i] else -1
        cums.append(cums[-1] + tmp)

    cnt = 0
    for i in range(N+1):
        a = cums[i] + N+5
        cnt += bit_sum(a)
        bit_add(a, 1)
    return cnt*2 >= N*(N+1)//2

ok = 0
ng = len(cands)
while ng - ok > 1:
    m = (ok+ng)//2
    if enough(m):
        ok = m
    else:
        ng = m
print(cands[ok])

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

cands = list(sorted(set(src)))

def enough(i):
    bit = [0] * (2*N+10)
    def bit_add(a,w):
        x = a
        while x <= (2*N+9):
            bit[x] += w
            x += (x & -x)
    def bit_sum(a):
        x = a
        ret = 0
        while x > 0:
            ret += bit[x]
            x -= (x & -x)
        return ret

    cums = [0]
    for a in src:
        tmp = 1 if a >= cands(i) else -1
        cums.append(cums[-1] + tmp)

    cnt = 0
    for i in range(N+1):
        a = cums[i] + N+5
        cnt += bit_sum(a)
        bit_add(a, 1)
    return cnt*2 >= N*(N+1)//2

ok = 0
ng = len(cands)
while ng - ok > 1:
    m = (ok+ng)//2
    if enough(m):
        ok = m
    else:
        ng = m
print(ok)
Problem p03426 (python)

Fixed submission:
H,W,D = map(int,input().split())
src = [list(map(int,input().split())) for i in range(H)]
Q = int(input())
qs = [tuple(map(int,input().split())) for i in range(Q)]

pos = [[None for i in range(D)] for j in range(H*W//D + 1)]
for y in range(H):
    for x in range(W):
        a = src[y][x]
        b,c = a//D, a%D
        pos[b][c] = (x,y)

cums = [[0] for i in range(D)]
for i in range(D):
    for j in range(H*W//D):
        if pos[j][i] is None or pos[j+1][i] is None:
            cums[i].append(cums[i][-1])
        else:
            x1,y1 = pos[j][i]
            x2,y2 = pos[j+1][i]
            dist = abs(x1-x2) + abs(y1-y2)
            cums[i].append(cums[i][-1] + dist)
            
for l,r in qs:
    d = l%D
    print(cums[d][r//D] - cums[d][l//D])

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
H,W,D = map(int,input().split())
A = [list(map(int,input().split())) for i in range(H)]
Q = int(input())
LR = [tuple(map(int,input().split())) for i in range(Q)]

INF = float('inf')
h = 0--(H*W)//D
pos = [[INF]*h for _ in range(D)]
for i,row in enumerate(A):
    for j,a in enumerate(row):
        d,m = divmod(a,D)
        pos[m][d] = (i,j)

dists = [[0] for _ in range(D)]
for i,p in enumerate(pos):
    for p1,p2 in zip(p,p[1:]):
        if p1==INF or p2==INF: continue
        a1,b1 = p1
        a2,b2 = p2
        dist = abs(a1-a2) + abs(b1-b2)
        dists[i].append(dists[i][-1] + dist)

ans = []
for l,r in LR:
    a,i = divmod(l,D)
    b,j = divmod(r,D)
    if i==0:
        a-=1; b-=1
    ans.append(dists[i][b] - dists[i][a])
print(*ans, sep='\n')
Problem p03724 (python)

Fixed submission:
N,M = map(int, raw_input().split())
hist = [0 for i in range(N)]
for i in range(M):
  a,b = map(int, raw_input().split())
  hist[a-1] += 1
  hist[b-1] += 1
for v in hist:
  if v%2 == 1:
    print 'NO'
    break
else : print 'YES'

Buggy submission (verdict: Runtime Error):
N,M = map(int, raw_input().split())
hist = {}
for i in range(N):
  a,b = map(int, raw_input().split())
  if a in hist:
    hist[a] += 1
  else:
    hist[a] = 1
  if b in hist:
    hist[b] += 1
  else:
    hist[b] = 1

for v in hist.values():
  if v%2 == 1:
    print 'NO'
    break
else : print 'YES'
Problem p03745 (python)

Fixed submission:
N = int(input())
src = list(map(int,input().split()))
asc = None
ans = 1
for i in range(N-1):
    d = src[i+1] - src[i]
    if d == 0: continue
    if asc is None:
        asc = d > 0
    elif (asc and d < 0) or (not asc and d > 0):
        ans += 1
        asc = None
print(ans)

Buggy submission (verdict: Runtime Error):
N = int(input())
src = map(int,input().split())
asc = 0
ans = 1
for i in range(N-1):
    d = src[i+1] - src[i]
    if asc * d < 0:
        ans += 1
        asc = 0
    else:
        asc = d
print(ans)
Problem p03776 (python)

Fixed submission:
from collections import Counter
N,A,B = map(int,input().split())
src = list(map(int,input().split()))
src.sort(reverse=True)

ave = sum(src[:A]) / A
print(ave)

counter = Counter(src)
cnt = counter[src[0]]

ncr = [[1]]
for n in range(1,N+1):
    ncr.append([1])
    for r in range(1,n):
        ncr[n].append(ncr[n-1][r-1] + ncr[n-1][r])
    ncr[n].append(1)

if cnt < A:
    last = src[A-1]
    n = counter[last]
    r = src[:A].count(last)
    print(ncr[n][r])
elif cnt == A:
    print(1)
else:
    ans = 0
    for r in range(A, min(B,cnt)+1):
        ans += ncr[cnt][r]
    print(ans)

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

ave = sum(src[:A]) / A
print(ave)

counter = Counter(src)
cnt = counter[src[0]]

ncr = [[1]]
for n in range(1,N+1):
    ncr.append([1])
    for r in range(1,n):
        ncr[n].append(ncr[n-1][r-1] + ncr[n-1][r])
    ncr[n].append(1)

if cnt < A:
    last = src[A-1]
    n = counter[last]
    r = src[:A].count(last)
    print(ncr[n][r])
elif cnt == A:
    print(1)
else:
    ans = 0
    for r in range(A,B+1):
        ans += ncr[cnt][r]
    print(ans)

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

ave = sum(src[:A]) / A
print(ave)

counter = Counter(src)
cnt = counter[src[0]]

ncr = [[1]]
for n in range(1,N+1):
    ncr.append([1])
    for r in range(1,n):
        ncr[n].append(ncr[n-1][r-1] + ncr[n-1][r])
    ncr[n].append(1)

if cnt < A:
    last = src[A-1]
    n = counter[last]
    r = src[:A].count(last)
    print(ncr[n][r])
else:
    ans = 0
    for r in range(A,B+1):
        ans += ncr[cnt][r]
    print(ans)