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

Fixed submission:
K = list(map(int, input().strip()))
D = int(input())
mod = 10**9+7

dp = [0]*D
dp[0] += 1
S = 0
for k in K:
    dp[S%D] -= 1
    dp2 = [0]*(D+10)
    for j, d in enumerate(dp):
        dp2[j] += d
        dp2[j+10] -= d
    for j in range(1, D+10):
        dp2[j] = (dp2[j-1] + dp2[j]) % mod
    for j in range(10):
        dp2[(D+j)%D] = (dp2[(D+j)%D] + dp2[D+j]) % mod 
    dp = dp2[:D]
    for j in range(k+1):
        dp[(S+j)%D] = (dp[(S+j)%D] + 1)%mod
    S += k
print((dp[0]-1)%mod)

Buggy submission (verdict: Runtime Error):
K = list(map(int, input().strip()))
D = int(input())
mod = 10**9+7
a = 0
for i in range(1, J+1):
    if sum(map(int, str(i))) % D == 0:
        a += 1
print(a%mod)
dp = [0]*D
dp[0] += 1
S = 0
for k in K:
    dp[S%D] -= 1
    dp2 = [0]*(D+10)
    for j, d in enumerate(dp):
        dp2[j] += d
        dp2[j+10] -= d
    for j in range(1, D+10):
        dp2[j] = (dp2[j-1] + dp2[j]) % mod
    for j in range(10):
        dp2[(D+j)%D] = (dp2[(D+j)%D] + dp2[D+j]) % mod 
    dp = dp2[:D]
    for j in range(k+1):
        dp[(S+j)%D] = (dp[(S+j)%D] + 1)%mod
    S += k
print((dp[0]-1)%mod)

Buggy submission (verdict: Runtime Error):
K = list(map(int, J.strip()))
D = int(input())
mod = 10**9+7
a = 0
for i in range(1, J+1):
    if sum(map(int, str(i))) % D == 0:
        a += 1
print(a%mod)
dp = [0]*D
dp[0] += 1
S = 0
for k in K:
    dp[S%D] -= 1
    dp2 = [0]*(D+10)
    for j, d in enumerate(dp):
        dp2[j] += d
        dp2[j+10] -= d
    for j in range(1, D+10):
        dp2[j] = (dp2[j-1] + dp2[j]) % mod
    for j in range(10):
        dp2[(D+j)%D] = (dp2[(D+j)%D] + dp2[D+j]) % mod 
    dp = dp2[:D]
    for j in range(k+1):
        dp[(S+j)%D] = (dp[(S+j)%D] + 1)%mod
    S += k
print((dp[0]-1)%mod)
Problem p03203 (python)

Fixed submission:
from collections import defaultdict
import sys
def inpl(): return [int(i) for i in input().split()]
H, W, N = inpl()
if not N:
    print(H)
    sys.exit()
A = []
B = defaultdict(lambda: [])
C = defaultdict(lambda: H+1)
for _ in range(N):
    x, y = inpl()
    A.append((x,y))
    B[y].append(x)
T = [0]*(W+1)
T[1] = 2
for i in range(1,W):
    if not B[i+1]:
        T[i+1] = T[i] + 1
        continue
    ctr = T[i]
    while True:
        if ctr in B[i+1]:
            ctr += 1
            continue
        break
    T[i+1] = min(ctr+1, H+1)
for x,y in A:
    if x >= T[y]:
        C[y] = min(C[y], x)
print(min([i-1 for i in C.values()]+[H]))

Buggy submission (verdict: Runtime Error):
from collections import defaultdict
import sys
def inpl(): return [int(i) for i in input().split()]
H, W, N = inpl()
if not N:
    print(W)
    sys.exit()
A = []
B = defaultdict(lambda: [])
C = defaultdict(lambda: W)
for _ in range(N):
    x, y = inpl()
    A.append((x,y))
    B[y].append(x)
T = [0]*(H+1)
T[1] = 2
for i in range(1,H):
    if not B[i+1]:
        T[i+1] = min(W,T[i] + 1)
        continue
    ctr = T[i]
    while True:
        if ctr in B[i+1]:
            ctr += 1
            continue
        break
    T[i+1] = min(ctr+1, W)
for x,y in A:
    if x >= T[y]:
        C[y] = min(C[y], x)
print(min([i-1 for i in C.values()]+[W]))

Buggy submission (verdict: Runtime Error):
from collections import defaultdict
import sys
def inpl(): return [int(i) for i in input().split()]
H, W, N = inpl()
if not N:
    print(W)
    sys.exit()
A = []
B = defaultdict(lambda: [])
C = defaultdict(lambda: W)
for _ in range(N):
    x, y = inpl()
    A.append((x,y))
    B[y].append(x)
T = [0]*(H+1)
T[1] = 2
for i in range(1,H):
    if not B[i+1]:
        T[i+1] = min(W,T[i] + 1)
        continue
    ctr = T[i]
    while True:
        if ctr in B[i+1]:
            ctr += 1
            continue
        break
    T[i+1] = min(ctr+1, W)
for x,y in A:
    if x >= T[y]:
        C[y] = min(C[y], x)
print(min([i-1 for i in C.values()]+[W]))
print(T)

Buggy submission (verdict: Runtime Error):
from collections import defaultdict
import bisect,sys
def inpl(): return [int(i) for i in input().split()]
H, W, N = inpl()
if not N:
    print(W)
    sys.exit()
A = []
B = defaultdict(lambda: [])
C = defaultdict(lambda: W)
for _ in range(N):
    x, y = inpl()
    A.append((x,y))
    B[y].append(x)
T = [0]*(H+1)
T[1] = 2
for i in range(1,H):
    if not B[i+1]:
        T[i+1] = T[i] + 1
        continue
    ctr = T[i]+1
    while True:
        if ctr in B[i+1]:
            ctr += 1
            continue
        break
    T[i+1] = min(ctr, W)
for x,y in A:
    if x >= T[y]:
        C[y] = min(C[y], x)
print(min([i-1 for i in C.values()]+[W]))
Problem p03214 (python)

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

ans = 10**12
Ans = None
for i, a in enumerate(A):
    if abs(S - N*a) < ans:
        ans = abs(S - N*a)
        Ans = i
print(Ans)

Buggy submission (verdict: Runtime Error):
from itertools import accumulate, combinations
N, K = map(int, input().split())
A = list(map(int, input().split()))

SA = list(accumulate(A)) + [0]
B = A[:]

for l, r in combinations(range(N), 2):
    B.append(SA[r] - SA[l-1])

ans = 0
for i in range(42, -1, -1):
    ans *= 2
    B1 = [b for b in B if 2**i & b]
    if len(B1) >= K:
        ans += 1
        B = B1[:]

print(ans)
Problem p03249 (python)

Fixed submission:
from collections import Counter
import sys
def muri():
    print(-1)
    sys.exit()
N = int(input())
D = [int(sys.stdin.readline()) for _ in range(N)]
#D = list(map(int, input().split()))
Didx = list(range(1, N+1))
Didx.sort(key = lambda x: -D[x-1])
H = {d: i for i, d in enumerate(D, 1)}
H = Counter(H)
D = [0] + D
K = [1]*(N+1)
Ans = []
for vn in Didx[:-1]:
    p = H[D[vn] - (N - 2*K[vn])]
    if not p:
        muri()
    Ans.append((vn, p))
    K[p] += K[vn]
s = set()

Edge = [[] for _ in range(N+1)]
for a in Ans:
    x, y = a
    Edge[x].append(y)
    Edge[y].append(x)

dist = [10**9+7]*(N+1)
dist[0] = 0
dist[1] = 0
stack = [1]
visited = set([1])
while stack:
    vn = stack.pop()
    for vf in Edge[vn]:
        if vf not in visited:
            visited.add(vf)
            dist[vf] = 1 + dist[vn]
            stack.append(vf)
if D[1] != sum(dist[1:]):
    muri()

for a in Ans:
    print(*a)

Buggy submission (verdict: Runtime Error):
from collections import Counter
import sys
def muri():
    print(-1)
    sys.exit()
N = int(input())
#D = [int(sys.stdin.readline()) for _ in range(N)]
D = list(map(int, input().split()))
Didx = list(range(1, N+1))
Didx.sort(key = lambda x: -D[x-1])
H = {d: i for i, d in enumerate(D, 1)}
H = Counter(H)
D = [0] + D
K = [1]*(N+1)
Ans = []
for vn in Didx[:-1]:
    p = H[D[vn] - (N - 2*K[vn])]
    if not p:
        muri()
    Ans.append((vn, p))
    K[p] += K[vn]
s = set()

Edge = [[] for _ in range(N+1)]
for a in Ans:
    x, y = a
    Edge[x].append(y)
    Edge[y].append(x)

dist = [10**9+7]*(N+1)
dist[0] = 0
dist[1] = 0
stack = [1]
visited = set([1])
while stack:
    vn = stack.pop()
    for vf in Edge[vn]:
        if vf not in visited:
            visited.add(vf)
            dist[vf] = 1 + dist[vn]
            stack.append(vf)
if D[1] != sum(dist[1:]):
    muri()

for a in Ans:
    print(*a)

Buggy submission (verdict: Runtime Error):
from collections import Counter
import sys
def muri():
    print(-1)
    sys.exit()
N = int(input())
#D = [int(sys.stdin.readline()) for _ in range(N)]
D = list(map(int, input().split()))
Didx = list(range(1, N+1))
Didx.sort(key = lambda x: -D[x-1])
H = {d: i for i, d in enumerate(D, 1)}
H = Counter(H)
D = [0] + D
K = [0]*(N+1)
Ans = []
for vn in Didx[:-1]:
    if K[vn] == 0:
        K[vn] = 1
    p = H[D[vn] - (N - 2*K[vn])]
    if not p:
        muri()
    Ans.append((vn, p))
    K[p] += K[vn] + 1
s = set()

Edge = [[] for _ in range(N+1)]
for a in Ans:
    x, y = a
    Edge[x].append(y)
    Edge[y].append(x)

dist = [10**9+7]*(N+1)
dist[0] = 0
dist[1] = 0
stack = [1]
visited = set([1])
while stack:
    vn = stack.pop()
    for vf in Edge[vn]:
        if vf not in visited:
            visited.add(vf)
            dist[vf] = 1 + dist[vn]
            stack.append(vf)
if D[1] != sum(dist[1:]):
    muri()

for a in Ans:
    print(*a)
Problem p03270 (python)

Fixed submission:
from collections import Counter
K, N = map(int, input().split())
mod = 998244353
frac = [1]*5010
for i in range(2,5010):
    frac[i] = i * frac[i-1]%mod
fraci = [None]*5010
for i in range(5010):
    fraci[i] = pow(frac[i], mod-2, mod)
def comb(a, b):
    if not a >= b >= 0:
        return 0
    return frac[a]*fraci[b]*fraci[a-b]%mod
A = Counter()
def calc(x):
    if A[x]:
        return A[x]
    res = 0
    for i in range(N//2+1):
        res = (res + (-1)**i*comb(x, i)*comb(N+K-1-2*i, N-2*i))%mod
    A[x] = res
    return res
for j in range(2, 2*K+1):
    print(calc(min(j, (2*K+2-j))//2))

Buggy submission (verdict: Runtime Error):
from collections import Counter
K, N = map(int, input().split())
mod = 998244353
frac = [1]*2010
for i in range(2,2010):
    frac[i] = i * frac[i-1]%mod
def comb(a, b):
    if not a >= b >= 0:
        return 0
    return frac[a]*pow(frac[b], mod-2, mod)*pow(frac[a-b], mod-2, mod)%mod
A = Counter()
def calc(x):
    if A[x]:
        return A[x]
    res = 0
    for i in range(N//2+1):
        res = (res + (-1)**i*comb(x, i)*comb(N+K-1-2*i, N-2*i))%mod
    A[x] = res
    return res
for j in range(2, 2*K+1):
    print(calc(min(j, (2*K+2-j))//2))
Problem p03309 (python)

Fixed submission:
def f(x,i,n):
    sadness = 0
    for j in range(n):
        sadness += abs(x[j]-i)
    return(sadness)
N = int(input())
H = [int(i) for i in input().split()]
for i in range(N):
    H[i] -= i+1
H = sorted(H)
k = N // 2
if N == 1 :
    print(0)
else:
    print(f(H,H[k],N))

Buggy submission (verdict: Runtime Error):
def f(x,i,n):
    sadness = 0
    for j in range(n):
        sadness += abs(x[j]-i)
    return(sadness)
N = int(input())
H = [int(i) for i in input().split()]
for i in range(N):
    H[i] -= i+1
H = sorted(H)
k = N // 2
print(min(f(H,H[k-1],N),f(H,H[k],N),f(H,H[k+1],N)))
Problem p03328 (python)

Fixed submission:
a,b =map(int,input().split())
s = b-a
s = int(s*(s+1)/2-b)
print(s)

Buggy submission (verdict: Runtime Error):
a,b =map(int,input().split())
s = b - a
s = s*(s+1)/2-b
s = int(s)
input(s)

Buggy submission (verdict: Runtime Error):
a,b =map(int,input().split())
s = b - a
s = s*(s+1)/2-b
s = int(s)
input(s)
Problem p03330 (python)

Fixed submission:
n,c = [int(i) for i in input().split()]
d = [[int(i) for i in input().split()]for i in range(c)]
#d[A][B] はAからBになるときの違和感
color = [[int(i) for i in input().split()]for i in range(n)]

devcol = [[0 for i in range(3)] for j in range(c)]
for l in range(n):
        for row in range(n):
            devcol[color[l][row]-1][(l+row)%3] += 1
Dsum = [[0 for i in range(3)] for j in range(c)]
for i in range(c):
    for j in range(c):
        for k in range(3):
            Dsum[i][k] += d[j][i]*devcol[j][k] 
D = Dsum[0][0]+Dsum[1][1]+Dsum[2][2]
for c0 in range (c):
    for c1 in range (c):
        if c1 == c0:
            continue
        for c2 in range (c):
            if c2 == c1 or c2 == c0:
                continue
            D = min(D,Dsum[c0][0]+Dsum[c1][1]+Dsum[c2][2])
#O(c^3)のつもり
print(D)

Buggy submission (verdict: Runtime Error):
import sys
from itertools import permutations
N, C = map(int, input().split())
D = [list(map(int, sys.stdin.readline().split())) for _ in range(C)]
CM = [list(map(int, sys.stdin.readline().split())) for _ in range(N)]

CG = [[0]*C for _ in range(3)]
for i in range(N):
    for j in range(N):
        nc = CM[i][j] - 1
        for c in range(C):
            CG[(i+j)%3][c] += D[nc][c]
ans = 10**10
for c1, c2, c3 in permutations(range(C), 3):
    ans = min(ans, CG[c1][0] + CG[c2][1] + CG[c3][2])
print(ans)
Problem p03357 (python)

Fixed submission:
import sys
from itertools import accumulate
readline = sys.stdin.readline

class BIT:
    #1-indexed
    def __init__(self, n):
        self.size = n
        self.tree = [0] * (n + 1)
        self.p = 2**(n.bit_length() - 1)
        self.dep = n.bit_length()
    def get(self, i):
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s
 
    def add(self, i, x):
        while i <= self.size:
            self.tree[i] += x
            i += i & -i
    
    def bl(self, v):
        if v <= 0:
            return -1
        s = 0
        k = self.p
        for _ in range(self.dep):
            if s + k <= self.size and self.tree[s+k] < v:
                s += k
                v -= self.tree[s+k]
            k //= 2
        return s + 1

N = int(readline())
W = [0]*N
B = [0]*N
for i in range(1, 2*N+1):
    c, a = readline().split()
    if c == 'W':
        W[int(a)-1] = i
    else:
        B[int(a)-1] = i

cw = [0]*(N+1)
cb = [0]*(N+1)
T = BIT(2*N)
for i in range(N):
    cw[i+1] = i-T.get(W[i])
    T.add(W[i], 1)
T = BIT(2*N)
for i in range(N):
    cb[i+1] = i-T.get(B[i])
    T.add(B[i], 1)

invw = [None] + [[0] + list(accumulate([1 if W[i] < B[j] else 0 for j in range(N)])) for i in range(N)]
invb = [None] + [[0] + list(accumulate([1 if B[i] < W[j] else 0 for j in range(N)])) for i in range(N)]
    
inf = 10**9+7
dp = [[inf]*(N+1) for _ in range(N+1)]
dp[0][0] = 0
for s in range(2*N):
    for w in range(max(0, s-N), min(N+1, s+1)):
        b = s-w
        if w != N:
            dp[w+1][b] = min(dp[w+1][b], dp[w][b] + invw[w+1][b] + cw[w+1])
        if b != N:
            dp[w][b+1] = min(dp[w][b+1], dp[w][b] + invb[b+1][w] + cb[b+1])
print(dp[N][N])

Buggy submission (verdict: Runtime Error):
class BIT:
    def __init__(self, n):
        self.size = n
        self.tree = [0] * (n + 1)
 
    def sum(self, i):
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s
 
    def add(self, i, x):
        while i <= self.size:
            self.tree[i] += x
            i += i & -i

N = int(input())
WP, BP = [2001]*(N+2), [2001]*(N+2)
for i in range(2*N):
    c, a = input().split()
    if c == 'W':
        WP[int(a)] = i+1
    else:
        BP[int(a)] = i+1
Q = [1, 1]
end = [0]*2*N
for i in range(2*N):
    w, b = Q
    if WP[w] < BP[b]:
        end[i] = WP[w]
        w += 1
    else:
        end[i] = BP[b]
        b += 1
    Q = w, b

Tr = BIT(2*N)
ans = N*(2*N-1)
for i in range(2*N):
    ans -= Tr.sum(end[i])
    Tr.add(end[i], 1)
print(ans)
Problem p03413 (python)

Fixed submission:
import sys
N = int(input())
a = [(int(v),i) for i, v in enumerate(input().split())]
ao = sum([v for v, i in a if i%2 and v>0])
ae = sum([v for v, i in a if not i%2 and v>0])
if max(ao, ae)== 0:
    ai = a.index(max(a))
    Ans = [1]*(ai)+list(range(N-ai,1,-1))
    print(max(a)[0])
    print(len(Ans))
    for i in Ans:
        print(i)
    sys.exit()
if ao >= ae:
    print(ao)
    yn = [i for v, i in a if i%2 and v>0]
else:
    print(ae)
    yn = [i for v, i in a if not i%2 and v>0]
listyn = [i in yn for i in range(N)]
Ans = []
while not listyn[0]:
    Ans.append(1)
    listyn = listyn[1:]
while not listyn[-1]:
    Ans.append(len(listyn))
    listyn = listyn[:-1]
while True:
    if len(listyn) == 1:
        break
    if len(listyn) == [2,3]:
        Ans.append(2)
        break
    if listyn[2]:
        Ans.append(2)
        listyn = [True] + listyn[3:]
    else:
        Ans.append(3)
        listyn = [True, False] + listyn[4:]
print(len(Ans))
for i in Ans:
    print(i)

Buggy submission (verdict: Runtime Error):
N = int(input())
a = [(int(v),i) for i, v in enumerate(input().split())]
ao = sum([v for v, i in a if i%2 and v>0])
ae = sum([v for v, i in a if not i%2 and v>0])
if max(ao, ae)== 0:
     ai = [a.index(max(a))]
     Ans = [1]*(ai)+list(range(N-ai,1,-1))
     for i in Ans:
         print(i)
if ao >= ae:
    print(ao)
    yn = [i for v, i in a if i%2 and v>0]
else:
    print(ae)
    yn = [i for v, i in a if not i%2 and v>0]
listyn = [i in yn for i in range(N)]
Ans = []
while not listyn[0]:
    Ans.append(1)
    listyn = listyn[1:]
while not listyn[-1]:
    Ans.append(len(listyn))
    listyn = listyn[:-1]
while True:
    if len(listyn) == 1:
        break
    if len(listyn) in [2,3]:
        Ans.append(2)
        break
    if listyn[2]:
        Ans.append(2)
        listyn = [True] + listyn[3:]
    else:
        Ans.append(3)
        listyn = [True] + listyn[4:]
print(len(Ans))
for i in Ans:
    print(i)

Buggy submission (verdict: Runtime Error):
def inpl(): return [int(i) for i in input().split()]
from collections import defaultdict
import sys
def edge(S):
    e = defaultdict(lambda: set())
    for i in range(N+1):
        for j in S:
            e[i].add(i%j)
        e[i] = e[i] | {i}
    return e
def path(edge):
    p = defaultdict(lambda: set())
    for st in range(N+1):
        Q, p[st], new = [edge[st]]*3
        while Q:
            for j in Q:
                new = new | edge[j]
            Q = new - p[st]
            p[st] = p[st] | new
    return p

K = int(input())
A = inpl()
B = inpl()
for i in range(K):
    if A[i] < B[i]:
        print(-1)
        sys.exit()
if A == B:
    print(0)
    sys.exit()
N = max(A) 
T = []
for l in range(N,-1,-1):
    S = T + list(range(1,1+l))
    Pa = path(edge(S))
    for i in range(K):
        if B[i] not in Pa[A[i]]:
            break
    else:
        continue
    T.append(l+1)
if not T:
    print(2)
    sys.exit()
if N+1 in T:
    print(-1)
    sys.exit()
print(sum([2**i for i in T]))
Problem p03422 (python)

Fixed submission:
import sys
sys.setrecursionlimit(10**6)
def grun(A, K):
    if A < K:
        return 0
    a, b = divmod(A, K)
    if b == 0:
        return a
    a += 1
    r = A % a
    return grun((((a-1)*K-r)//a)*a+r, K)
def solve():
    N = int(input())
    X = 0
    for _ in range(N):
        A, K = map(int, input().split())
        X ^= grun(A, K)
    print('Takahashi' if X else 'Aoki')

if __name__ == '__main__':
    solve()

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10**6)
def grun(A, K):
    if A < K:
        return 0
    if A%K == 0:
        return A//K
    return grun(A - A//K - 1, K)
def solve():
    N = int(input())
    X = 0
    for _ in range(N):
        A, K = map(int, input().split())
        X ^= grun(A, K)
    print('Takahashi' if X else 'Aoki')

if __name__ == '__main__':
    solve()
Problem p03423 (python)

Fixed submission:
print(int(input())//3)

Buggy submission (verdict: Runtime Error):
from collections import defaultdict
nametag = defaultdict(lambda: 0)
N = int(input())
for _ in range(N):
    s = input()
    if s[0] == 'M':
        nametag['M'] += 1
    if s[0] == 'A':
        nametag['A'] += 1
    if s[0] == 'R':
        nametag['R'] += 1
    if s[0] == 'C':
        nametag['C'] += 1
    if s[0] == 'H':
        nametag['H'] += 1
ans = 0
march = list(nametag.keys())
for i in march:
    for j in march:
        for k in march:
            if i == k or j == k or i == j:
                continue
            ans += nametag[i]*nametag[j]*nametag[k]
print(ans//6)
Problem p03436 (python)

Fixed submission:
def inpl(): return [int(i) for i in input().split()]
from collections import deque
H,W = inpl()
S =  []
for i in range(H):
    S.append(input())
    
dist = [[-1 for _ in range(W)] for _ in range(H)]
dist[0][0] = 1
Q = deque()
Q.append((0,0))
d = [(0, 1), (0, -1), (1, 0), (-1, 0)]
while Q:
    nw, nh = Q.pop() 
    for dw, dh in d:
        if not 0 <= nh + dh <= H-1:
            continue
        if not 0 <= nw + dw <= W-1:
            continue
        if S[nh+dh][nw+dw] == '#':
            continue
        if dist[nh+dh][nw+dw] != -1:
            continue
        dist[nh+dh][nw+dw] =dist[nh][nw] +1
        Q.appendleft((nw+dw,nh+dh))
if dist[H-1][W-1] == -1:
    print(-1)
else:
    ans = 0
    for i in range(H):
        ans += S[i].count('.')
    ans -= dist[H-1][W-1]
    print(ans)

Buggy submission (verdict: Runtime Error):
def inpl(): return [int(i) for i in input().split()]

H, W, D = inpl()
S = dict([])
for i in range(H):
    a = inpl()
    for j in range(W):
        S[a[j]] = [i,j]

dist = [[0] for i in range(D+1) ]
for i in range(1,D+1):
    for j in range(1,1+(H*W-i)//D):
        dist[i].append(dist[i][-1] +\
        (abs(S[i+j*D][0]-S[i+(j-1)*D][0])+abs(S[i+j*D][1]-S[i+(j-1)*D][1])))
Q = int(input())
ans = []
for i in range(Q):
    l, r = inpl()
    mod = l % D
    if mod == 0 : mod = D
    l, r = (l-mod)//D, (r-mod)//D
    ans.append(dist[mod][r]-dist[mod][l])
for i in ans:
    print(i)
Problem p03452 (python)

Fixed submission:
def inpl(): return [int(i) for i in input().split()]
def find(x):
    if par[x] == x:
        return x
    else:
        par[x],dist[x] = find(par[x]),dist[x]+dist[par[x]]
        return par[x]
N, M = inpl()
par = list(range(N+1))
dist = [0 for _ in range(N+1)]

for _ in range(M):
    l, r, d = inpl()
    fl = find(l)
    fr = find(r)
    if fl == fr and d + dist[l] - dist[r] != 0:
        print('No')
        break
    else: 
        par[fr] = fl
        dist[fr] = d + dist[l] - dist[r]
else:
    print('Yes')

Buggy submission (verdict: Runtime Error):
def inpl(): return [int(i) for i in input().split()]
class UnionFind():
    def __init__(self, num):
        self.par = [-1 for _ in range(num)]
        self.dist = [0 for _ in range(num)]
    def find(self, x):
        if self.par[x] < 0:
            return x
        else:
            x = self.par[x]
            return self.find(x)
    
    def union(self, x, y, d):
        rx = self.find(x)
        ry = self.find(y)
        if rx != ry:
            self.par[ry] = rx
            self.dist[ry] = d + self.dist[x] - self.dist[y]
        elif self.dist[ry] != d + self.dist[x] - self.dist[y]:
            return False
        return True 
    
ans = 'Yes'
N, M = inpl()
X = UnionFind(N)
for _ in range(M):
    l, r, d = inpl()
    if not X.union(l-1, r-1, d):
        ans = 'No'

print(ans)

Buggy submission (verdict: Runtime Error):
def inpl(): return [int(i) for i in input().split()]
class UnionFind():
    def __init__(self, num):
        self.par = [-1 for _ in range(num)]
        self.dist = [0 for _ in range(num)]
    def find(self, x):
        if self.par[x] < 0:
            return x
        else:
            x = self.par[x]
            return self.find(x)
    
    def union(self, x, y, d):
        rx = self.find(x)
        ry = self.find(y)
        if rx != ry:
            self.par[ry] = rx
            self.dist[ry] = d + self.dist[x] - self.dist[y]
        elif self.dist[ry] != d + self.dist[x] - self.dist[y]:
            return False
        return True 
    

N, M = inpl()
X = UnionFind(N)
for _ in range(M):
    l, r, d = inpl()
    if not X.union(l-1, r-1, d):
        ans = 'No'
else:
    ans = 'Yes'
print(ans)
Problem p03490 (python)

Fixed submission:
s = input() + 'T'
x, y = [int(i) for i in input().split()]

dire = True
xi = []
yi = []
ctr = 0
for i in range(len(s)):
    if s[i] == 'F':
        ctr += 1
        continue
    if dire:
        xi.append(ctr)
    else:
        yi.append(ctr)
    ctr = 0
    dire = not dire
ans = 'Yes'
X = (sum(xi)-x)//2
x0 = xi.pop(0)
Hx = {0: True}
for i in xi:
    for j in Hx.copy().keys():
        Hx[j+i] = True
Y = (sum(yi)-y)//2
Hy = {0: True}
for i in yi:
    for j in Hy.copy().keys():
        Hy[j+i] = True
        
if (x0+sum(xi)-x)%2 != 0 or (sum(yi)-y)%2 != 0:
    ans = 'No'
if not Hx.get(X, False) or not Hy.get(Y, False):
    ans = 'No'
print(ans)

Buggy submission (verdict: Runtime Error):
def inpl(): return [int(i) for i in input().split()]

s = input().split('T')
x, y = inpl()
xi = [len(s[i]) for i in s if i%2 == 0]
yi = [len(s[i]) for i in s if i%2 == 1]

X = (sum(xi)-x)//2
x0 = xi.pop(0) 
Hx = {0: True}
for i in xi:
    for j in Hx.copy().keys():
        Hx[j+i] = True
Y = (sum(yi)-y)//2
Hy = {0: True}
for i in yi:
    for j in Hy.copy().keys():
        Hy[j+i] = True
  
ans = 'Yes'      
if (x0+sum(xi)-x)%2 != 0 or (sum(yi)-y)%2 != 0:
    ans = 'No'
if not Hx.get(X, False) or not Hy.get(Y, False):
    ans = 'No'
print(ans)