Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
K = input()
D = int(input())
L = len(K)
MOD = 10**9+7
dp = [[[0]*D for j in range(2)] for i in range(L+1)] #dp[i][less][modD]
dp[0][0][0] = 1
for i,c in enumerate(K):
    for less in range(2):
        lim = 9 if less else int(c)
        for d in range(lim+1):
            dp[i+1][less or d < lim][(r+d)%D] += dp[i][less][r]
            dp[i+1][less or d < lim][(r+d)%D] %= MOD

print((dp[-1][0][0] + dp[-1][1][0] - 1) % MOD)

Fixed submission:
K = input()
D = int(input())
L = len(K)
MOD = 10**9+7
dp = [[[0]*D for j in range(2)] for i in range(L+1)] #dp[i][less][modD]
dp[0][0][0] = 1
for i,c in enumerate(K):
    for less in range(2):
        lim = 9 if less else int(c)
        for r in range(D):
            for d in range(lim+1):
                dp[i+1][less or d < lim][(r+d)%D] += dp[i][less][r]
                dp[i+1][less or d < lim][(r+d)%D] %= MOD

print((dp[-1][0][0] + dp[-1][1][0] - 1) % MOD)

Buggy python submission (verdict: Time Limit Exceeded):
K = input()
D = int(input())
L = len(K)
MOD = 10**9+7
dp = [[[0]*D for j in range(2)] for i in range(L+1)] #dp[i][less][modD]
dp[0][0][0] = 1
for i,c in enumerate(K):
    for less in range(2):
        lim = 9 if less else int(c)
        for r in range(D):
            for d in range(lim+1):
                dp[i+1][less or d < lim][(r+d)%D] += dp[i][less][r]
                dp[i+1][less or d < lim][(r+d)%D] %= MOD

print((dp[-1][0][0] + dp[-1][1][0] - 1) % MOD)

Fixed submission:
K = input()
D = int(input())
L = len(K)
MOD = 10**9+7
dp = [[[0]*D for j in range(2)] for i in range(L+1)] #dp[i][less][modD]
dp[0][0][0] = 1
for i,c in enumerate(K):
    for less in range(2):
        lim = 9 if less else int(c)
        for r in range(D):
            for d in range(lim+1):
                dp[i+1][less or d < lim][(r+d)%D] += dp[i][less][r]
                dp[i+1][less or d < lim][(r+d)%D] %= MOD

print((dp[-1][0][0] + dp[-1][1][0] - 1) % MOD)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
src = [tuple(map(int,input().split())) for i in range(N)]
src.sort(key=lambda x:x[0]+x[1])

dp = [None] * (20001)
dp[0] = 0
for w,s,v in src:
    for s0 in range(s,-1,-1):
        if dp[s0] is None: continue
        if dp[s0+w] is None:
            dp[s0+w] = dp[s0] + v
        else:
            dp[s0+w] = max(dp[s0+w], dp[s0] + v)
print(max(v for v in dp if v is not None))

Fixed submission:
N = int(input())
src = [tuple(map(int,input().split())) for i in range(N)]
src.sort(key=lambda x:x[0]+x[1])

dp = [None] * (20001)
dp[0] = 0
for w,s,v in src:
    for s0 in range(s,-1,-1):
        if dp[s0] is None: continue
        if dp[s0+w] is None:
            dp[s0+w] = dp[s0] + v
        else:
            dp[s0+w] = max(dp[s0+w], dp[s0] + v)
print(max(v for v in dp if v is not None))
Buggy python 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)

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 python submission (verdict: Runtime Error):
N,K = map(int,input().split())
X = list(map(int,input().split()))
ls = []
rs = []
for x in X:
    if x < 0:
        ls.append(-x)
    else:
        rs.append(x)
ls.reverse()

ans = float('inf')
if len(ls) >= K:
    ans = min(ans, ls[K-1])
if len(rs) >= K:
    ans = min(ans, rs[K-1])

for l in range(1,K):
    if l >= len(ls): break
    r = K-l
    ans = min(ans, ls[l-1]*2 + rs[r-1])
    ans = min(ans, rs[r-1]*2 + ls[l-1])
print(ans)

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

ans = float('inf')
for l in range(N-K+1):
    r = l + K - 1
    if src[l] > 0:
        ans = min(ans, src[r])
    elif src[r] < 0:
        ans = min(ans, -src[l])
    else:
        ans = min(ans, src[r]*2 - src[l], -src[l]*2 + src[r])
print(ans)
Buggy python submission (verdict: Runtime Error):
A = int(input())
B = int(input())
print(A*B - A - B + 1)

Fixed submission:
A,B = map(int,input().split())
print(A*B - A - B + 1)
Buggy python submission (verdict: Runtime Error):
S = input()
K = int(input())

i = 0
while S[i]=='1':
    i += 1

if i >= K:
    print(1)
else:
    print(S[i])

Fixed submission:
S = input()
K = int(input())

ones = 0
for c in S:
    if c == '1':
        ones += 1
    else:
        n = c
        break

print(1 if ones >= K else n)
Buggy python submission (verdict: Runtime Error):
R = int(input())
if R < 1200:
    print('ABC')
elif R < 2800:
    print('ARC')
else:
    prnit('AGC')

Fixed submission:
R = int(input())
if R < 1200:
    print('ABC')
elif R < 2800:
    print('ARC')
else:
    print('AGC')
Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter
N = int(input())
S = input()
ctr1 = Counter()
ctr2 = Counter()

for b in range(2**N):
    blue = red = ''
    for k in range(N):
        if b & (1<<k):
            blue += S[k]
        else:
            red += S[k]
    ctr1[(blue,red)] += 1

T = S[::-1][:N]
for b in range(2**N):
    blue = red = ''
    for k in range(N):
        if b & (1<<k):
            blue += T[k]
        else:
            red += T[k]
    ctr2[(blue,red)] += 1

ans = 0
for (b1,r1),v1 in ctr1.items():
    for (b2,r2),v2 in ctr2.items():
        if b1==r2 and b2==r1:
            ans += v1*v2
print(ans)

Fixed submission:
from collections import Counter
N = int(input())
S = input()
ctr1 = Counter()
ctr2 = Counter()

for b in range(2**N):
    blue = red = ''
    for k in range(N):
        if b & (1<<k):
            blue += S[k]
        else:
            red += S[k]
    ctr1[(blue,red)] += 1

T = S[:-N-1:-1]
for b in range(2**N):
    blue = red = ''
    for k in range(N):
        if b & (1<<k):
            blue += T[k]
        else:
            red += T[k]
    ctr2[(blue,red)] += 1

ans = 0
for k,v in ctr1.items():
    ans += v * ctr2[k]
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
N,M = map(int,input().split())
XYZ = [tuple(map(int,input().split())) for _ in range(N)]
ans = 0
for b in range(8):
	dp = [0]
	for x,y,z in XYZ:
		if b&1: x *= -1
		if b&2: y *= -1
		if b&4: z *= -1
		v = x+y+z
		dp2 = dp[:]
		for i in range(len(dp)-1,-1,-1):
			if i < M:
				if i+1 >= len(dp2):
					dp2.append(dp[i] + v)
				else:
					dp2[i+1] = max(dp2[i+1], dp[i] + v)
		dp = dp2
	ans = max(ans, dp[-1])
print(ans)

Fixed submission:
N,M = map(int,input().split())
src = [tuple(map(int,input().split())) for i in range(N)]

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

def dp(type):
    minimize_x = type&1
    minimize_y = (type>>1)&1
    minimize_z = (type>>2)&1
    dp = [0]
    for i,(x,y,z) in enumerate(src):
        if minimize_x: x *= -1
        if minimize_y: y *= -1
        if minimize_z: z *= -1
        v = x+y+z
        dp.append(dp[-1] + v)
        for j in reversed(range(i)):
            dp[j+1] = max(dp[j+1], dp[j] + v)
    return dp[M]

ans = 0
for i in range(8):
    ans = max(ans, dp(i))
print(ans)
Buggy python submission (verdict: Runtime Error):
H,W = map(int,input().split())
src = [input() for i in range(H)]

dxy = [(0,1),(1,0),(0,-1),(-1,0)]
def is_isolated(x,y):
    if src[y][x] == '.': return False
    for dx,dy in dxy:
        if not 0 <= x+dx < W: continue
        if not 0 <= y+dy < H: continue
        if src[y+dy][x+dx] == '#': return False
    else:
        return True

for y in range(H):
    for x in range(W):
        if is_isolated(y,x):
            print('No')
            exit()
print('Yes')

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

dxy = [(-1,0),(0,-1),(1,0),(0,1)]
for y in range(H):
    for x in range(W):
        if src[y][x] == '.': continue
        for dx,dy in dxy:
            nx,ny = x+dx,y+dy
            if not 0 <= nx < W: continue
            if not 0 <= ny < H: continue
            if src[ny][nx] == '#':
                break
        else:
            print('No')
            exit()
print('Yes')
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
src = [input() for i in range(N)]

def isok(b):
    for i in range(N-1):
        for j in range(i+1, N):
            i2 = (i+b)%N
            j2 = (j+b)%N
            if src[i][j2] != src[j][i2]:
                return False
    return True

ans = 0
for i in range(-N+1,N):
    if isok(i):
        ans += N-abs(i)
print(ans)

Fixed submission:
N = int(input())
src = [input() for i in range(N)]

def isok(b):
    for i in range(N-1):
        for j in range(i+1, N):
            i2 = (i+b)%N
            j2 = (j+b)%N
            if src[i][j2] != src[j][i2]:
                return False
    return True

ans = 0
for i in range(-N+1,N):
    if isok(i):
        ans += N-abs(i)
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())

MOD = 10**9+7
fac = [1,1] + [0]*N
finv = [1,1] + [0]*N
inv = [0,1] + [0]*N
for i in range(2,N+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

ans = 0
prev = 0
for k in range((N+1)//2, N):
    tmp = ncr(k-1, N-1-k) * fac[k] * fac[N-1-k]
    tmp %= MOD
    ans += (tmp - prev) * k
    ans %= MOD
    prev = tmp
print(ans)

Fixed submission:
N = int(input())

MOD = 10**9+7
fac = [1,1] + [0]*N
finv = [1,1] + [0]*N
inv = [0,1] + [0]*N
for i in range(2,N+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

ans = 0
prev = 0
for k in range((N+1)//2, N):
    tmp = ncr(k-1, N-1-k) * fac[k] * fac[N-1-k]
    tmp %= MOD
    ans += (tmp - prev) * k
    ans %= MOD
    prev = tmp
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
N,C = map(int,input().split())
src = [tuple(map(int,input().split())) for i in range(N)]

c1 = []
c2 = []
cumv = 0
for x,v in src:
    cumv += v
    c1.append(cumv - x)
    c2.append(cumv - 2*x)

r1 = []
r2 = []
cumv = 0
for x,v in reversed(src):
    cumv += v
    x = C-x
    r1.append(cumv - x)
    r2.append(cumv - 2*x)

ans = max(c1+r1+[0])
for i,c in enumerate(c2):
    for r in r1[:N-i-1]:
        ans = max(ans, c+r)
for i,r in enumerate(r2):
    for c in c1[:N-i-1]:
        ans = max(ans, c+r)

print(ans)


Fixed submission:
N,C = map(int,input().split())
src = [tuple(map(int,input().split())) for i in range(N)]

c1 = []
c2 = []
c1x = []
cumv = 0
c1max = -float('inf')
for x,v in src:
    cumv += v
    c1.append(cumv - x)
    c2.append(cumv - 2*x)
    c1max = max(c1max, c1[-1])
    c1x.append(c1max)

r1 = []
r2 = []
r1x = []
cumv = 0
r1max = -float('inf')
for x,v in reversed(src):
    cumv += v
    x = C-x
    r1.append(cumv - x)
    r2.append(cumv - 2*x)
    r1max = max(r1max, r1[-1])
    r1x.append(r1max)

ans = max(c1+r1+[0])
for i,c in enumerate(c2[:-1]):
    ans = max(ans, c + r1x[N-i-2])
for i,r in enumerate(r2[:-1]):
    ans = max(ans, r + c1x[N-i-2])

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import itertools

S = input()
N = len(S)
if all(S[0] == c for c in S):
    print(1)
    exit()
if N == 2:
    print(1 if S[0]==S[1] else 2)
    exit()

if N == 3:
    def another(a,b):
        s = set('abc')
        s -= set([a,b])
        return list(s)[0]
    ptn = set()
    stack = [S]
    while stack:
        a = stack.pop()
        ptn.add(a)
        if a[0] != a[1]:
            b = another(a[0],a[1])*2 + a[2]
            if not b in ptn:
                stack.append(b)
        if a[1] != a[2]:
            c = a[0] + another(a[1],a[2])*2
            if not c in ptn:
                stack.append(c)
    print(len(ptn))
    exit()

#N >= 4
MOD = 998244353
dp = [[[[0 for u in range(2)] for l in range(3)] for s in range(3)] for n in range(N+1)]
#dp[n][sum%3][last][exist'xx'?]
for ptn in itertools.product(range(3),repeat=4):
    seq = (ptn[0]==ptn[1] or ptn[1]==ptn[2] or ptn[2]==ptn[3])
    dp[4][sum(ptn)%3][ptn[3]][seq] += 1

for n in range(4,N):
    for s in range(3):
        for l in range(3):
            for u in range(2):
                for l2 in range(3):
                    s2 = (s+l2)%3
                    u2 = u or l==l2
                    dp[n+1][s2][l2][u2] += dp[n][s][l][u]
                    dp[n+1][s2][l2][u2] %= MOD

sm = 0
for c in S:
    sm += ord(c) - ord('a')
seq = False
for c1,c2 in zip(S,S[1:]):
    if c1 == c2:
        seq = True
        break
ans = sum([dp[N][sm%3][i][1] for i in range(3)])
if not seq: ans += 1
print(ans % MOD)

Fixed submission:
import itertools

S = input()
N = len(S)
if all(S[0] == c for c in S):
    print(1)
    exit()
if N == 2:
    print(1 if S[0]==S[1] else 2)
    exit()

if N == 3:
    def another(a,b):
        s = set('abc')
        s -= set([a,b])
        return list(s)[0]
    ptn = set()
    stack = [S]
    while stack:
        a = stack.pop()
        ptn.add(a)
        if a[0] != a[1]:
            b = another(a[0],a[1])*2 + a[2]
            if not b in ptn:
                stack.append(b)
        if a[1] != a[2]:
            c = a[0] + another(a[1],a[2])*2
            if not c in ptn:
                stack.append(c)
    print(len(ptn))
    exit()

#N >= 4
MOD = 998244353
dp = [[[0 for u in range(2)] for l in range(3)] for s in range(3)]
#dp[sum%3][last][exist'xx'?]
for ptn in itertools.product(range(3),repeat=4):
    seq = (ptn[0]==ptn[1] or ptn[1]==ptn[2] or ptn[2]==ptn[3])
    dp[sum(ptn)%3][ptn[3]][seq] += 1

for n in range(4,N):
    dp2 = [[[0 for u in range(2)] for l in range(3)] for s in range(3)]
    for s in range(3):
        for l in range(3):
            for u in range(2):
                for l2 in range(3):
                    s2 = (s+l2)%3
                    u2 = u or l==l2
                    dp2[s2][l2][u2] += dp[s][l][u]
                    dp2[s2][l2][u2] %= MOD
    dp = dp2

sm = 0
for c in S:
    sm += ord(c) - ord('a')
seq = False
for c1,c2 in zip(S,S[1:]):
    if c1 == c2:
        seq = True
        break
ans = sum([dp[sm%3][i][1] for i in range(3)])
if not seq: ans += 1
print(ans % MOD)
Buggy python submission (verdict: Runtime Error):
S = input()
if S == 'zyxwvutsrqponmlkjihgfedcba':
    print(-1)
    exit()
if len(S) < 26:
    for i in range(26):
        c = chr(ord('a') + i)
        if c in S: continue
        print(S + c)
        exit()

assert len(S) == 26

cset = set()
for i,(a,b) in enumerate(reversed(list(zip(S,S[1:])))):
    if a > b:
        cset.add(b)
    else:
        print(S[:-i-2] + min(cset))
        exit()

Fixed submission:
S = input()

def itoc(i): return chr(ord('a') + i)
def ctoi(c): return ord(c) - ord('a')

if S == 'zyxwvutsrqponmlkjihgfedcba':
    print(-1)
    exit()
if len(S) == 26:
    i = 25
    while i > 0:
        if S[i-1] > S[i]:
            i -= 1
        else:
            ans = S[:i-1]
            for ci in range(ctoi(S[i-1])+1, 27):
                if itoc(ci) in ans: continue
                ans += itoc(ci)
                print(ans)
                exit()
else:
    for i in range(26):
        c = itoc(i)
        if c in S: continue
        print(S + c)
        break
Buggy python submission (verdict: Runtime Error):
N = int(input())
ans = []
total = 0
for n in range(30001):
    if (n%2 == 0) ^ (n%3 == 0):
        ans.append(n)
        total += n
    if len(ans) == N: break

if total%2 == 0:
    i = N - 1
    while ans[i]%3 > 0:
        i -= 1
    ans[i] += 3

if total%3 == 0:
    i = N - 1
    while ans[i]%2 > 0:
        i -= 1
    ans[i] += 2

print(*ans)


Fixed submission:
N = int(input())
if N == 3:
    print('2 5 63')
    exit()

ans = []
total = 0
for n in range(1,30001):
    if n%2 == 0 or n%3 == 0:
        ans.append(n)
        total += n
    if len(ans) == N: break

if total%6 == 1:
    i = N - 1
    while ans[i]%3 > 0:
        i -= 1
    ans[i] += 3
    i = N - 1
    while ans[i]%2 > 0:
        i -= 1
    ans[i] += 2

elif total%6 == 2:
    i = N - 1
    while ans[i]%2 > 0:
        i -= 1
    ans[i] += 4

elif total%6 == 3:
    i = N - 1
    while ans[i]%3 > 0:
        i -= 1
    ans[i] += 3

elif total%6 == 4:
    i = N - 1
    while ans[i]%2 > 0:
        i -= 1
    ans[i] += 2

elif total%6 == 5:
    i = N - 1
    while ans[i]%3 > 0:
        i -= 1
    ans[i] += 3
    i = N - 1
    while ans[i]%2 > 0:
        i -= 1
    ans[i] += 4

print(*ans)
Buggy python submission (verdict: Runtime Error):
A,B = map(int,input().split())
n = 50
m = n*2

grid = []
for i in range(n):
    grid.append([‘.’] * m)
for i in range(n):
    grid.append([‘#’] * m)

x = 0
y = 0
for i in range(B-1):
    grid[y][x] = ‘#’
    x += 2
    if x >= m:
        x -= m
        y += 2

x = 0
y = n+1
for i in range(A-1):
    grid[y][x] = ‘.’
    x += 2
    if x >= m:
        x -= m
        y += 2

print(str(m) + ‘ ‘ + str(m))
for row in grid:
    print(‘’.join(row))

Fixed submission:
A,B = map(int,input().split())
n = 50
m = n*2

grid = []
for i in range(n):
    grid.append(['.'] * m)
for i in range(n):
    grid.append(['#'] * m)

x = 0
y = 0
for i in range(B-1):
    grid[y][x] = '#'
    x += 2
    if x >= m:
        x -= m
        y += 2

x = 0
y = n+1
for i in range(A-1):
    grid[y][x] = '.'
    x += 2
    if x >= m:
        x -= m
        y += 2

print(str(m) + ' ' + str(m))
for row in grid:
    print(''.join(row))
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))

def count_kth_bit(k):
    ret = 0
    t = 2**k
    am = list(sorted([a%(2*t) for a in A]))
    bm = list(sorted([b%(2*t) for b in B]))

    r1 = r2 = r3 = 0
    for a in reversed(am):
        while r1 < N and a + bm[r1] < t:
            r1 += 1
        while r2 < N and a + bm[r2] < 2*t:
            r2 += 1
        while r3 < N and a + bm[r3] < 3*t:
            r3 += 1
        ret += (r2-r1) + (N-r3)
    return ret

ans = 0
for k in range(29):
    c = count_kth_bit(k)
    ans += ((c%2) << k)
print(ans)

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

def count_kth_bit(k):
    ret = 0
    t = 2**k
    am = list(sorted([a%(2*t) for a in A]))
    bm = list(sorted([b%(2*t) for b in B]))

    r1 = r2 = r3 = 0
    for a in reversed(am):
        while r1 < N and a + bm[r1] < t:
            r1 += 1
        while r2 < N and a + bm[r2] < 2*t:
            r2 += 1
        while r3 < N and a + bm[r3] < 3*t:
            r3 += 1
        ret += (r2-r1) + (N-r3)
    return ret

ans = 0
for k in range(29):
    c = count_kth_bit(k)
    ans += ((c%2) << k)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from bisect import bisect_left as bil
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))

def count_kth_bit(k):
    ret = 0
    t = 2**k
    bs = list(sorted([b%(2*t) for b in B]))
    for a in A:
        a %= (2*t)
        i1 = bil(bs, t-a)
        i2 = bil(bs, 2*t-a)
        i3 = bil(bs, 3*t-a)
        ret += (i2-i1) + (N-i3)
    return ret

ans = 0
for k in range(29):
    c = count_kth_bit(k)
    ans += ((c%2) << k)
print(ans)

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

def count_kth_bit(k):
    ret = 0
    t = 2**k
    am = list(sorted([a%(2*t) for a in A]))
    bm = list(sorted([b%(2*t) for b in B]))

    r1 = r2 = r3 = 0
    for a in reversed(am):
        while r1 < N and a + bm[r1] < t:
            r1 += 1
        while r2 < N and a + bm[r2] < 2*t:
            r2 += 1
        while r3 < N and a + bm[r3] < 3*t:
            r3 += 1
        ret += (r2-r1) + (N-r3)
    return ret

ans = 0
for k in range(29):
    c = count_kth_bit(k)
    ans += ((c%2) << k)
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
N,A,B = map(int,input().split())
if not (A+B-1 <= N <= A*B):
    print(-1)
    exit()
if B == 1:
    print(*list(range(1,A+1)))
    exit()

n = (N-A)//(B-1)
m = (N-A)%(B-1)

ans = list(range(1,A+1))
tmp = A+1
lds = 1
while tmp <= N:
    l = n if lds > m else n+1
    ans = list(range(tmp,tmp+l)) + ans
    tmp += l
    lds += 1
print(*ans)

Fixed submission:
N,A,B = map(int,input().split())
if not (A+B-1 <= N <= A*B):
    print(-1)
    exit()
if B == 1:
    print(*list(range(1,A+1)))
    exit()

n = (N-A)//(B-1)
m = (N-A)%(B-1)

stack = [1,A+1]
lds_cnt = 1
while stack[-1] <= N:
    l = n if lds_cnt > m else n+1
    stack.append(stack[-1] + l)
    lds_cnt += 1

ans = []
while len(stack) > 1:
    ans += list(range(stack[-2], stack[-1]))
    stack.pop()
print(*ans)
Buggy python 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')

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])