Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
N = int(input())
 
sum = 0
tmp = N
while tmp > 0:
  sum += tmp % 10
  tmp =// 10
if N % sum == 0:
  print("Yes")
else:
  print("No")

Fixed submission:
N = int(input())
 
sum = 0
tmp = N
while tmp > 0:
  sum += tmp % 10
  tmp //= 10
if N % sum == 0:
  print("Yes")
else:
  print("No")

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())

sum = 0
tmp = N
while tmp > 0:
  sum += tmp % 10
  tmp // 10
if N % sum == 0:
  print("Yes")
else:
  print("No")

Fixed submission:
N = int(input())
 
sum = 0
tmp = N
while tmp > 0:
  sum += tmp % 10
  tmp //= 10
if N % sum == 0:
  print("Yes")
else:
  print("No")
Buggy python submission (verdict: Runtime Error):
s = input()
print(s[0] + len(s[1:len(s) - 1]) + s[len(s) - 1]

Fixed submission:
s = input()
print(s[0] + str(len(s[1:len(s) - 1])) +  s[len(s) - 1])
Buggy python submission (verdict: Runtime Error):
x_1,y_1,x_2,y_2=tuple(map(int,input().split()))

be=[x_2-x_1,y_2-y_1]
za3=[x_2+(-1)*be[1],y_2+be[0]]
za4=[x_1+(-1)*be[1],y_1+be[0]]

print(za3[0],za3[1],za4[0],za4[1])

Fixed submission:
n=int(input())
if n % 2==0:
	print((n//2)**2)
else:
	m=(n+1)//2
	print(m*(n-m))
Buggy python submission (verdict: Runtime Error):
x_1,y_1,x_2,y_2=tuple(map(int,input().split()))

be=[x_2-x_1,y_2-y_1]
za3=[x_2+(-1)*be[1],y_2+be[0]]
za4=[x_1+(-1)*be[1],y_1+be[0]]

print(za3[0],za3[1],za4[0],za4[

Fixed submission:
x_1,y_1,x_2,y_2=tuple(map(int,input().split()))

be=[x_2-x_1,y_2-y_1]
za3=[x_2+(-1)*be[1],y_2+be[0]]
za4=[x_1+(-1)*be[1],y_1+be[0]]

print(za3[0],za3[1],za4[0],za4[1])
Buggy python submission (verdict: Time Limit Exceeded):
n, h = [int(t) for t in input().split()]
aa = []
bb = []
for _ in range(n):
    a, b = [int(t) for t in input().split()]
    aa.append(a)
    if b > a:
        bb.append(b)
aa.sort()
bb.sort()

cnt = 0
while h > 0:
    if not bb:
        cnt += (h - 1) // aa[-1] + 1
        break
    elif bb[-1] > aa[-1]:
        h -= bb.pop()
    else:
        h -= aa[-1]
    cnt += 1
print(cnt)


Fixed submission:
n, h = [int(t) for t in input().split()]
aa = []
bb = []
for _ in range(n):
    a, b = [int(t) for t in input().split()]
    aa.append(a)
    if b > a:
        bb.append(b)
aa.sort()
bb.sort()

cnt = 0
while h > 0:
    if not bb or aa[-1] >= bb[-1]:
        cnt += (h - 1) // aa[-1] + 1
        break
    else:
        h -= bb.pop()
    cnt += 1
print(cnt)
Buggy python submission (verdict: Runtime Error):
a, b, c, d, e, f = [int(x) for x in input().strip().split()]
a *= 100
b *= 100
if f < a + c:
    print("0 0")
    raise SystemExit

water_cands = set()
t = 0
while t <= f:
    water_cands.add(t)
    u = t + b
    while u <= f:
        water_cands.add(u)
        u += b
    t += a

rat, wat, sug = 0.0, 0, 0
for w in water_cands:
    suger_limit = min(f - w, w // 100 * e)
    suger_max = 0
    t = 0
    while t <= suger_limit:
        if suger_max < t:
            suger_max = t
        u = t + d
        while u <= suger_limit:
            if suger_max < u:
                suger_max = u
            u += d
        t += c
    if suger_max / (suger_max + w) > rat:
        rat = suger_max / (suger_max + w)
        wat = w
        sug = suger_max

print(wat + sug, sug)


Fixed submission:
a, b, c, d, e, f = [int(x) for x in input().strip().split()]
a *= 100
b *= 100

water_cands = set()
t = 0
while t <= f:
    water_cands.add(t)
    u = t + b
    while u <= f:
        water_cands.add(u)
        u += b
    t += a

rat, wat, sug = 0.0, a, 0
for w in water_cands:
    if w == 0:
        continue
    suger_limit = min(f - w, w // 100 * e)
    suger_max = 0
    t = 0
    while t <= suger_limit:
        if suger_max < t:
            suger_max = t
        u = t + d
        while u <= suger_limit:
            if suger_max < u:
                suger_max = u
            u += d
        t += c
    if suger_max / (suger_max + w) > rat:
        rat = suger_max / (suger_max + w)
        wat = w
        sug = suger_max

print(wat + sug, sug)
Buggy python submission (verdict: Runtime Error):
    if [1 for c in input() if c == '9']:
        print('Yes')
    else:
        print('No')

Fixed submission:
if [1 for c in input() if c == '9']:
    print('Yes')
else:
    print('No')
Buggy python submission (verdict: Runtime Error):

Copy
Copy
from functools import lru_cache
 
 
N, M = map(int, input().split())
# 壊れている床番号
a = []
for _ in range(M):
    a.append(int(input()))
# ルートの総数
root = 0
# 今いる階段の段数
now = 0
 
 
def isSafe(now):
    """
    1, 2段先共になければ0
    1段先だけあれば1
    2段先だけあれば2
    1, 2段先共にあれば3
    """
    now_1 = not (now-1) in a
    now_2 = not (now-2) in a
    if now<=1:
        return 0
    elif now==2:
        return int(now_1)
    else:
        return int(now_1 + now_2*2)
 
 
@lru_cache(maxsize=100000)
def step(N):
    # N段を登る移動方法数
    if N <= 1:
        if not (1 in a):
            return 1   
        else:
            return 0
    elif N==2:
        if not (2 in a):
            return step(1) + 1
        else:
            return step(1)
    elif isSafe(N)==1:
        return step(N-1)
    elif isSafe(N)==2:
        return step(N-2)
    elif isSafe(N)==3:
        return step(N-1)+step(N-2)
    else:
        return 0
 
print(step(N)%1000000007)

Fixed submission:
def main():
  p, q, r = map(int, input().split())
  print(min(p+q, p+r, q+r))
 
if __name__=='__main__':
  main()

Buggy python submission (verdict: Runtime Error):
N, M = list(map(int, input().split(" ")))
A = [1] * (N+1)
for i in range(M):
    A[int(input())] = -1
for i in range(2, N + 1):
    if(A[i] == -1):
        A[i] = 0
    else:
        A[i] = A[i-1] + A[i-2]
print(A[-1] % 1000000007)


Fixed submission:
import itertools
pqr = list(map(int,input().split(" ")))
print(sum(min(list(itertools.combinations(pqr, 2)), key=lambda x:sum(x))))

Buggy python submission (verdict: Runtime Error):
P=int(input())
Q=int(input())
R=int(input())
print("{}".format(min([P+Q,Q+R,R+P])))

Fixed submission:
P, Q,R = map(int, input().split())
print("{}".format(min([P+Q,Q+R,R+P])))

Buggy python submission (verdict: Runtime Error):
P=int(input())
Q=int(input())
R=int(input())

a=[P+Q,Q+R,R+P]

print("{}".format(min(a))



Fixed submission:
P, Q,R = map(int, input().split())
print("{}".format(min([P+Q,Q+R,R+P])))
Buggy python submission (verdict: Runtime Error):
n, a, b, c, d, e = map(int, input().split())
x = min(a, b, c, d, e)
print((n + x - 1) // x + 4)

Fixed submission:
n = int(input())
A = []
for _ in range(5):
  A.append(int(input()))
x = min(A)
print((n + x - 1) // x + 4)
Buggy python submission (verdict: Runtime Error):
def func(x):
  if(x % 2 == 0):
    return x / 2
  else:
    return 3 * x + 1

s = int(input())
if(s == 1 || s == 2):
  print(4)
  exit()
  
cnt = 1
while(s != 1):
  s = func(s)
  cnt += 1
print(cnt + 1)


Fixed submission:
def func(x):
  if(x % 2 == 0):
    return x / 2
  else:
    return 3 * x + 1

s = int(input())
if((s == 1) or (s == 2)):
  print(4)
  exit()
  
cnt = 1
while(s != 1):
  s = func(s)
  cnt += 1
print(cnt + 1)
Buggy python submission (verdict: Runtime Error):
A, B, C = map(int, input().split())
print(B + min(A+B+1, c))

Fixed submission:
A, B, C = map(int, input().split())
print(B + min(A+B+1, C))
Buggy python submission (verdict: Runtime Error):
n = int(input())
a = list(map(int, input().split()))
t = [0] * (n + 1)
#t[i] = a0 + ... + a(i-1)
for i in range(n):
    t[i + 1] = t[i] + a[i]

'''
前処理O(N + log mod)でN以下の階乗および階乗の逆数を計算
-> O(1)でcomb(n, k)を計算できる
'''

mod = 10 ** 9 + 7
dp_F = [0] * (n * 2) 
dp_Finv = [0] * (n * 2)
dp_inv = [0] * (n * 2)
#dp_F[i] = i!

def factorial(n):
    if dp_F[n] > 0:
        return dp_F[n]
    elif n == 0:
        dp_F[0] = 1
        return 1
    else:
        dp_F[n] = n * factorial(n - 1) % mod
        return dp_F[n]

def inverse(n):
    #modでのnの逆数
    return pow(n, mod - 2, mod)

c = factorial(n)
def f(k):
    return ((c * inverse(k)) % mod)

ans = 0
for i in range(1, n):
    ans += t[i] * (f(n - i + 1) - f(i + 1))
    ans %= mod
ans += t[n] * sum([f(i) for i in range(1, n + 1)])
ans %= mod
print(ans)

Fixed submission:
import sys; sys.setrecursionlimit(1000000)

n = int(input())
a = list(map(int, input().split()))
t = [0] * (n + 1)
#t[i] = a0 + ... + a(i-1)
for i in range(n):
    t[i + 1] = t[i] + a[i]

'''
前処理O(N + log mod)でN以下の階乗および階乗の逆数を計算
-> O(1)でcomb(n, k)を計算できる
'''

mod = 10 ** 9 + 7
dp_F = [0] * (n * 2) 
dp_Finv = [0] * (n * 2)
dp_inv = [0] * (n * 2)
#dp_F[i] = i!

def factorial(n):
    if dp_F[n] > 0:
        return dp_F[n]
    elif n == 0:
        dp_F[0] = 1
        return 1
    else:
        dp_F[n] = n * factorial(n - 1) % mod
        return dp_F[n]

def inverse(n):
    #modでのnの逆数
    return pow(n, mod - 2, mod)

c = factorial(n)
def f(k):
    return ((c * inverse(k)) % mod)

ans = 0
for i in range(1, n):
    ans += t[i] * (f(n - i + 1) - f(i + 1))
    ans %= mod
ans += t[n] * sum([f(i) for i in range(1, n + 1)])
ans %= mod
print(ans)
Buggy python submission (verdict: Runtime Error):
import numpy as np

N, X = map(int, input().split())
city = [X] + list(map(int, input().split()))
city_diff = np.array([abs(city[i + 1] - city[i]) for i in range(N)])
ans = np.gcd.reduce(city_diff)
print(ans)

Fixed submission:
def gcd(x, y):
    if y > x:
        return gcd(y, x)
    elif x == 0:
        return 0
    else:
        z = x % y
        if z == 0:
            return y
        else:
            return gcd(y, z)

N, X = map(int, input().split())
city = [X] + list(map(int, input().split()))
city_diff = [abs(city[i + 1] - city[i]) for i in range(N)]
ans = city_diff[0]
for i in range(1, N, 1):
    ans = gcd(ans, city_diff[i])
print(ans)

Buggy python submission (verdict: Runtime Error):
import math
import sys
sys.setrecursionlimit(100000) 
N, X = map(int, input().split())
city = [X] + list(map(int, input().split()))
city_diff = [abs(city[i + 1] - city[i]) for i in range(N)]
ans = city_diff[0]
for i in range(1, N, 1):
    ans = math.gcd(ans, city_diff[i])
print(ans)

Fixed submission:
def gcd(x, y):
    if y > x:
        return gcd(y, x)
    elif x == 0:
        return 0
    else:
        z = x % y
        if z == 0:
            return y
        else:
            return gcd(y, z)

N, X = map(int, input().split())
city = [X] + list(map(int, input().split()))
city_diff = [abs(city[i + 1] - city[i]) for i in range(N)]
ans = city_diff[0]
for i in range(1, N, 1):
    ans = gcd(ans, city_diff[i])
print(ans)

Buggy python submission (verdict: Runtime Error):
import math
N, X = map(int, input().split())
city = [X] + list(map(int, input().split()))
city_diff = [abs(city[i + 1] - city[i]) for i in range(N)]
import math
ans = city_diff[0]
for i in range(1, N, 1):
    ans = math.gcd(ans, city_diff[i])
print(ans)

Fixed submission:
def gcd(x, y):
    if y > x:
        return gcd(y, x)
    elif x == 0:
        return 0
    else:
        z = x % y
        if z == 0:
            return y
        else:
            return gcd(y, z)

N, X = map(int, input().split())
city = [X] + list(map(int, input().split()))
city_diff = [abs(city[i + 1] - city[i]) for i in range(N)]
ans = city_diff[0]
for i in range(1, N, 1):
    ans = gcd(ans, city_diff[i])
print(ans)
Buggy python submission (verdict: Runtime Error):
import bisect

N, M, Q = map(int, input().split())
trains = [list(map(int, input().split())) for i in range(M)]
queries = [list(map(int, input().split())) for i in range(Q)]
trains.sort(key = lambda x: x[1])
L = [train[0] for train in trains]
R = [train[1] for train in trains]

dp = [[0] * N for i in range(N)]
for i in range(N):
    for j in range(i, N):
        right = bisect.bisect_right(R, j + 1)
        A = sorted(L[:right])
        left = bisect.bisect_left(A, i + 1)
        dp[i][j] = right - left

for q in range(Q):
    pi, qi = queries[q]
    print(dp[pi - 1][qi - 1])


Fixed submission:
A, B = map(int, input().split())
print((A - 1) * (B - 1))
Buggy python submission (verdict: Runtime Error):
T = input()
N = len(T)
mod = 10**9 + 7
A_left = 0
q_left = 0
q_right = T[1:].count('?')
C_right = T[1:].count('C')
ans = 0
for i in range(1, N-1, 1):
    if T[i-1] == 'A':
        A_left += 1
    if T[i-1] =='?':
        q_left += 1
    if T[i] == '?':
        q_right -= 1
    if T[i] == 'C':
        C_right -= 1
    if T[i] == 'B' or T[i] == '?':
        ans += int((A_left * (3 ** q_left) + q_left * (3 ** (q_left - 1))) * (C_right * (3 ** q_right) + q_right * (3 ** (q_right - 1))))
        ans %= mod
print(ans)


Fixed submission:
T = input()
N = len(T)
mod = 10**9 + 7

def func(X, Y):
    if Y == 0:
        return X
    else:
        return ((3* X + Y) * pow(3, Y - 1, mod)) % mod

A_left = 0
q_left = 0
q_right = T[1:].count('?')
C_right = T[1:].count('C')
ans = 0
for i in range(1, N-1, 1):
    if T[i-1] == 'A':
        A_left += 1
    if T[i-1] =='?':
        q_left += 1
    if T[i] == '?':
        q_right -= 1
    if T[i] == 'C':
        C_right -= 1
    if T[i] == 'B' or T[i] == '?':
        ans += func(A_left, q_left) * func(C_right, q_right)
        ans %= mod
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
#import numpy as np

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

#dp[S] = A[S], -inf で初期化　Sの部分集合TのうちA[T]が(最大のもの, 次に大きいもの)　を入れていく

dp = [[A[S], -float('inf') ] for S in range(1 << N)]
for i in range(N):
    for T in range( 1 << N):
        if (T >> i) & 1 == 1: # Tがiを含むとき
            merge = dp[T] + dp[T ^ (1 << i)]
            merge.sort(reverse = True)
            dp[T] =merge[:2]
ans = -float('inf')
for S in range(1, 1 << N , 1):
    ans = max(ans, sum(dp[S]))
    print(ans)

Fixed submission:
N = int(input())
A = list(map(int, input().split()))
 
#dp[S] = A[S], -inf で初期化　Sの部分集合TのうちA[T]が(最大のもの, 次に大きいもの)　を入れていく
 
dp = [[A[S], -float('inf') ] for S in range(1 << N)]
for i in range(N):
    for T in range( 1 << N):
        if (T >> i) & 1 == 1: # Tがiを含むとき
            merge = dp[T] + dp[T ^ (1 << i)]
            merge.sort(reverse = True)
            dp[T] =merge[:2]
ans = -float('inf')
for S in range(1, 1 << N , 1):
    ans = max(ans, sum(dp[S]))
    print(ans)
Buggy python submission (verdict: Runtime Error):
N, K = map(int, input().split())
A = list(map(int, input().split()))
def kiriage(a, b): #切り上げ
    return (a + b - 1) // b

print(kiriage((N - 1, K - 1)))


Fixed submission:
N, K = map(int, input().split())
A = list(map(int, input().split()))
def kiriage(a, b): #切り上げ
    return (a + b - 1) // b

print(kiriage(N - 1, K - 1))
Buggy python submission (verdict: Time Limit Exceeded):
N, C = map(int, input().split())
#D[X][Y] = (X+1)→(Y+1)に塗り替えたときの違和感
D = [list(map(int, input().split())) for i in range(C)]
#C[i][j] = (i+1,j+1)の色
c = [list(map(int, input().split())) for i in range(N)]
#import pdb; pdb.set_trace()
ans = float('inf')

def mult3(x):
    if x % 3 == 0:
        return 1
    else:
        return 0

for x in range(C):
    for y in range(C):
        for z in range(C):
            if (x-y)*(y-z)*(z-x) != 0:
                new_grid = [[mult3(i+j) * x + mult3(i+j-1) * y + mult3(i + j -2) * z + 1 for j in range(N)]for i in range(N)]
                iwakan = 0
                for i in range(N):
                    for j in range(N):
                        iwakan += D[c[i][j] - 1][new_grid[i][j] - 1]
                ans = min(ans, iwakan)
print(ans)


Fixed submission:
N, C = map(int, input().split())
#D[X][Y] = (X+1)→(Y+1)に塗り替えたときの違和感
D = [list(map(int, input().split())) for i in range(C)]
#C[i][j] = (i+1,j+1)の色
c = [list(map(int, input().split())) for i in range(N)]
#import pdb; pdb.set_trace()
ans = float('inf')
#c0: (i+1)+(j+1) \equiv 0 なるマスに色1, 色2, ..., 色Cがそれぞれ何個書かれているか
c0 = [0] * C
c1 = [0] * C
c2 = [0] * C
for i in range(N):
    for j in range(N):
        if (i + j) % 3 == 0:
            c0[c[i][j] - 1] += 1
        if (i + j) % 3 == 1:
            c1[c[i][j] - 1] += 1
        if (i + j) % 3 == 2:
            c2[c[i][j] - 1] += 1


for x in range(C):
    for y in range(C):
        for z in range(C):
            if (x-y)*(y-z)*(z-x) != 0:
                iwakan = sum(D[i][x] * c0[i] for i in range(C)) + sum(D[j][y] * c1[j] for j in range(C))+sum(D[k][z] * c2[k] for k in range(C))                
                ans = min(ans, iwakan)
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
S = input()
print(min(S[:i].count('W') + S[i+1:].count('E') for i in range(N)))


Fixed submission:
N = int(input())
S = input()
ans = [0]
ans[0] = S[1:].count('E')
for i in range(N-1):
    if S[i] == 'E' and S[i+1] == 'E':
        ans.append(ans[i] -1)
    if S[i] == 'E' and S[i+1] == 'W':
        ans.append(ans[i])
    if S[i] == 'W' and S[i+1] == 'E':
        ans.append(ans[i] )
    if S[i] == 'W' and S[i+1] == 'W':
        ans.append(ans[i] +1)
print(min(ans))
Buggy python submission (verdict: Time Limit Exceeded):
s = input()
K = int(input())

substrings = set()

for i in range(len(s)):
    for j in range(i+1, len(s)+1):
        substrings.add(s[i:j])

substrings_list = sorted(list(substrings))
print(substrings_list[K-1])


Fixed submission:
s = input()
K = int(input())
 
substrings = set()
 
for i in range(len(s)):
    for j in range(i+1, min(len(s)+1, i+K+1)):
        substrings.add(s[i:j])
 
substrings_list = sorted(list(substrings))
print(substrings_list[K-1])
Buggy python submission (verdict: Runtime Error):
def prime_list(n):
    '''
    n以下の素数のリストを返す
    '''
    A = list(range(2, n + 1, 1))
    prime = []
    p_max = A.pop(0)    
    while A[-1] >= p_max ** 2:
        for n in A:
            if n % p_max == 0:
                A.remove(n)
        prime.append(p_max)
        p_max = A.pop(0)
    prime.append(p_max)
    prime += A
    return prime

N = int(input())
ans = [2]
for prime in prime_list(1362):
    if len(ans) < N:
        if prime % 5 == 1:
            ans.append(prime)

print(' '.join(ans))


Fixed submission:
def prime_list(n):
    '''
    n以下の素数のリストを返す
    '''
    A = list(range(2, n + 1, 1))
    prime = []
    p_max = A.pop(0)    
    while A[-1] >= p_max ** 2:
        for n in A:
            if n % p_max == 0:
                A.remove(n)
        prime.append(p_max)
        p_max = A.pop(0)
    prime.append(p_max)
    prime += A
    return prime

N = int(input())
ans = ['2']
for prime in prime_list(1362):
    if len(ans) < N:
        if prime % 5 == 1:
            ans.append(str(prime))

print(' '.join(ans))
Buggy python submission (verdict: Runtime Error):
A, B = map(int, input().split())

h, w = 100, 100
grid = [['.'] * 100 for _ in range(50)]+ [['#'] * 100 for _ in range(50)]

for i in range(A - 1):
    A1, A2 = divmod(i, 50)
    grid[2 * A1][2 * A2] = '#'

for j in range(B - 1):
    B1, B2 = divmod(i, 50)
    grid[2 * B1 + 51][2 * B2] = '.'

print(h,w)
for i in range(100):
    print(grid[i])


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

h, w = 100, 100
grid = [['.'] * 100 for _ in range(50)] + [['#'] * 100 for _ in range(50)]

for i in range(A - 1):
    A1, A2 = divmod(i, 50)
    grid[2 * A1][2 * A2] = '#'

for j in range(B - 1):
    B1, B2 = divmod(j, 50)
    grid[2 * B1 + 51][2 * B2] = '.'

print(h, w)
for i in range(100):
    print(''.join(grid[i]))
Buggy python submission (verdict: Runtime Error):
import numpy as np
H, W, D = map(int, input().split())
grid = np.array([[0, 0]] * (H * W))
for i in range(H):
    Ai = list(map(int, input().split()))
    for j in range(W):
        grid[Ai[j] - 1] = [i, j]
Q = int(input())
tasks = [list(map(int, input().split())) for _ in range(Q)]

accum = [[]] * D
for i in range(D):
    diff = (abs(np.diff(grid[i::D, 0])) + abs(np.diff(grid[i::D, 1])))
    accum[i] = [0] + list(np.cumsum(diff))

for q in range(Q):
    Li, Ri = tasks[q]
    res = (Li - 1) % D
    x = (Li - 1) // D
    y = (Ri - 1)// D
    print(accum[res][y] - accum[res][x])


Fixed submission:
import numpy as np
H, W, D = map(int, input().split())
grid = np.array([[0, 0]] * (H * W))
for i in range(H):
    Ai = list(map(int, input().split()))
    for j in range(W):
        grid[Ai[j] - 1] = [i, j]
Q = int(input())
tasks = [list(map(int, input().split())) for _ in range(Q)]

accum = [0] * (H*W)
for i in range(H * W):
    if i >= D:
        accum[i] = accum[i - D] + abs(grid[i][0] - grid[i - D][0]) + abs(grid[i][1] - grid[i - D][1])

for q in range(Q):
    Li, Ri = tasks[q]
    print(accum[Ri - 1] - accum[Li - 1])

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
H, W, D = map(int, input().split())
grid = np.array([[0, 0]] * (H * W))
for i in range(H):
    Ai = list(map(int, input().split()))
    for j in range(W):
        grid[Ai[j] - 1] = [i, j]
Q = int(input())
tasks = [list(map(int, input().split())) for _ in range(Q)]

accum = [[]] * D
for i in range(D):
    diff = (abs(np.diff(grid[i::D, 0])) + abs(np.diff(grid[i::D, 1])))
    accum[i] = [0] + list(np.cumsum(diff))

for q in range(Q):
    Li, Ri = tasks[q]
    res = (Li - 1) % D
    x = (Li - 1) // D
    y = (Ri - 1)// D
    print(accum[res][y] - accum[res][x])


Fixed submission:
import numpy as np
H, W, D = map(int, input().split())
grid = np.array([[0, 0]] * (H * W))
for i in range(H):
    Ai = list(map(int, input().split()))
    for j in range(W):
        grid[Ai[j] - 1] = [i, j]
Q = int(input())
tasks = [list(map(int, input().split())) for _ in range(Q)]

accum = [0] * (H*W)
for i in range(H * W):
    if i >= D:
        accum[i] = accum[i - D] + abs(grid[i][0] - grid[i - D][0]) + abs(grid[i][1] - grid[i - D][1])

for q in range(Q):
    Li, Ri = tasks[q]
    print(accum[Ri - 1] - accum[Li - 1])
Buggy python submission (verdict: Time Limit Exceeded):
import bisect
def prime_list(n):
    '''
    n未満の素数のリストを返す
    '''
    num = list(range(2, n))
    prime = []
    while num[0] ** 2 < n:
        p = num.pop(0)
        for j in range(2 * p, n, p):
            if j in num:
                num.remove(j)
        prime.append(p)
    prime += num
    return prime

Q = int(input())
queries = [list(map(int, input().split())) for _ in range(Q)]
primes = prime_list(10 ** 5)
prime_2017like = []
for prime in primes:
    if (prime + 1) // 2 in primes:
        prime_2017like.append(prime)
for i in range(Q):
    li, ri = queries[i]
    print(bisect.bisect_right(prime_2017like, ri) - bisect.bisect_right(prime_2017like, li - 1))


Fixed submission:
def sieve(n):
    '''

    :param n:
    :return: n以下の素数のリストを返す
    '''
    prime = []
    is_prime = [True] * (n + 1) #is_prime[i] = Trueならiは素数
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2, n+1):
        if is_prime[i]:
            prime.append(i)
            for j in range(2 * i, n + 1, i):
                is_prime[j] = False
    return is_prime

import bisect
Q = int(input())
queries = [list(map(int, input().split())) for _ in range(Q)]
primehantei = sieve(10 ** 5)
like2017 = []
for i in range(10 ** 5):
    if primehantei[i] == True and primehantei[ (i + 1) // 2] == True:
        like2017.append(i)

for i in range(Q):
    li, ri = queries[i]
    print(bisect.bisect_right(like2017, ri) - bisect.bisect_right(like2017, li - 1))

Buggy python submission (verdict: Runtime Error):
import bisect
def prime_list(n):
    '''
    n未満の素数のリストを返す
    '''
    num = list(range(2, n))
    prime = []
    while num[0] ** 2 < n:
        p = num.pop(0)
        for j in range(2 * p, n, p):
            if j in num:
                num.remove(j)
        prime.append(p)
    prime += num
    return prime

Q = int(input())
queries = [list(map(int, input().split())) for _ in range(Q)]
primes = prime_list(10 ** 5)
prime_2017like = []
for prime in primes:]
    if (prime + 1) // 2 in primes:
        prime_2017like.append(prime)
for i in range(Q):
    li, ri = queries[i]
    print(bisect.bisect_right(prime_2017like, ri) - bisect.bisect_right(prime_2017like, li - 1))


Fixed submission:
def sieve(n):
    '''

    :param n:
    :return: n以下の素数のリストを返す
    '''
    prime = []
    is_prime = [True] * (n + 1) #is_prime[i] = Trueならiは素数
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2, n+1):
        if is_prime[i]:
            prime.append(i)
            for j in range(2 * i, n + 1, i):
                is_prime[j] = False
    return is_prime

import bisect
Q = int(input())
queries = [list(map(int, input().split())) for _ in range(Q)]
primehantei = sieve(10 ** 5)
like2017 = []
for i in range(10 ** 5):
    if primehantei[i] == True and primehantei[ (i + 1) // 2] == True:
        like2017.append(i)

for i in range(Q):
    li, ri = queries[i]
    print(bisect.bisect_right(like2017, ri) - bisect.bisect_right(like2017, li - 1))