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

Fixed submission:
H, N = map(int,input().split())
magic = []
for i in range(N):
    a,b = map(int,input().split())
    magic.append((a, b))
magic.sort(reverse=True)
amax, _ = magic[0]

INF = 1<<60

def solve():
    btab = [INF] * (H+1)
    btab[0] = 0
    oneshot = min(amax, H)
    for h in range(1, oneshot+1):
        btab[h] = min([b for a, b in magic if a >= h])
    for h in range(2, H+1):
        for a, b in magic:
            c = b + btab[max(h-a, 0)]
            if c < btab[h]:
                btab[h] = c
    return btab[H]

print(solve())

Buggy submission (verdict: Runtime Error):
H, N = map(int,input().split())
magic = []
for i in range(N):
    a,b = map(int,input().split())
    magic.append((a, b))
magic.sort(reverse=True)

INF = 1<<60

def solve():
    btab = [INF] * (H+1)
    btab[0] = 0
    for h in range(1, magic[0][0]+1):
        btab[h] = min([b for a, b in magic if a >= h])
    for h in range(2, H+1):
        for a, b in magic:
            c = b + btab[max(h-a,0)]
            if c < btab[h]:
                btab[h] = c
    return btab[H]

print(solve())

Buggy submission (verdict: Runtime Error):
H, N = map(int,input().split())
magic = []
for i in range(N):
    a,b = map(int,input().split())
    magic.append((a, b))
magic.sort(reverse=True)

INF = 1<<60

def solve():
    btab = [INF] * (H+1)
    btab[0] = 0
    for h in range(1, magic[0][0]+1):
        btab[h] = min([b for a, b in magic if a >= h])
    for h in range(2, H+1):
        for s in range(1, magic[0][0]+1):
            c = btab[s] + btab[h-s]
            if c < btab[h]:
                btab[h] = c
    return btab[H]

print(solve())

Buggy submission (verdict: Runtime Error):
H, N = map(int,input().split())
magic = []
for i in range(N):
    a,b = map(int,input().split())
    magic.append((a, b))
magic.sort(reverse=True)

INF = 1<<60

def solve():
    btab = [INF] * (H+1)
    btab[0] = 0
    for h in range(1, magic[0][0]+1):
        btab[h] = min([b for a, b in magic if a >= h])
    for h in range(1, H+1):
        cand = [btab[s] + btab[h-s] for s in range(1, h) if s <= h-s]
        if not cand:
            continue
        cost = min(cand)
        if cost < btab[h]:
            btab[h] = cost
    return btab[H]

print(solve())
Problem p02900 (python)

Fixed submission:
import math
from fractions import gcd

a,b = map(int,input().split())
g = gcd(a,b)
gr = int(math.ceil(math.sqrt(g)))

facs = [1]
i = 2
while i*i <= g:
    if g == 1:
        break
    if g % i == 0:
        facs.append(i)
        while g % i == 0:
            g //= i
    i += 1
if g != 1:
    facs.append(g)

print(len(facs))

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

a,b = map(int,input().split())
g = gcd(a,b)

gcr = int(math.sqrt(g))
is_p = [True] * (gcr+1)
is_p[0] = False
is_p[1] = False
for i in range(2, gcr+1):
    if is_p[i]:
        for j in range(2*i, gcr+1, i):
            is_p[j] = False

ps = [i for i, v in enumerate(is_p) if v and i >= 2]
ans = len([1 for p in ps if gc % p == 0]) + 1
if g > 1 and ans == 1:
    ans += 1
print(ans)

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

a,b = map(int,input().split())
gc = gcd(a,b)
gcr = int(math.sqrt(gc))
is_p = [True] * (gcr+1)
is_p[0] = False
for i in range(2, gcr+1):
    if is_p[i]:
        for j in range(2*i, gcr+1, i):
            is_p[j] = False

ps = [i for i, v in enumerate(is_p) if v]
ans = len([1 for p in ps if gc % p == 0])
if gcr not in ps and all(gcr % p != 0 for p in ps and p >= 2):
    ans += 1
print(ans)

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

a,b = map(int,input().split())
gcr = gcd(a,b)
is_p = [True] * (gcr+1)
is_p[0] = False
for i in range(2, gcr+1):
    if is_p[i]:
        for j in range(2*i, gcr+1, i):
            is_p[j] = False

ps = [i for i, v in enumerate(is_p) if v and gcr % i == 0]
print(len(ps))
Problem p02903 (python)

Fixed submission:
h, w, a, b = map(int, input().split())

for _ in range(b):
  print("0" * a + "1" * (w - a))
for _ in range(b, h):
  print("1" * a + "0" * (w - a))

Buggy submission (verdict: Runtime Error):
from fractions import Fraction

h, w, a, b = map(int, input().split())


def search():
    for _ in range(b):
        print("0" * a + "1" * (w - a))
    for _ in range(b, H):
        print("1" * a + "0" * (w - a))
    return True


def solve():
    if a == 0 and b == 0:
        for _ in range(h):
            print("0" * w)
        return True
    if b == 0:
        for _ in range(h):
            print("0" * a + "1" * (w - a))
        return True
    if a == 0:
        for _ in range(b):
            print("0" * w)
        for _ in range(h - b):
            print("1" * w)
        return True
    if Fraction(w, h) == Fraction(a, b):
        for r in range(h):
            for c in range(w):
                if c // a == r // b:
                    print("1", end="")
                else:
                    print("0", end="")
            print()
        return True
    return search()


if not search():
    print("No")

Buggy submission (verdict: Runtime Error):
from fractions import Fraction

h, w, a, b = map(int, input().split())


def search():
    for _ in range(b):
        print("0" * a + "1" * (w - a))
    for _ in range(b, H):
        print("1" * a + "0" * (w - a))
    return True


def solve():
    if a == 0 and b == 0:
        for _ in range(h):
            print("0" * w)
        return True
    if b == 0:
        for _ in range(h):
            print("0" * a + "1" * (w - a))
        return True
    if a == 0:
        for _ in range(b):
            print("0" * w)
        for _ in range(h - b):
            print("1" * w)
        return True
    if Fraction(w, h) == Fraction(a, b):
        for r in range(h):
            for c in range(w):
                if c // a == r // b:
                    print("1", end="")
                else:
                    print("0", end="")
            print()
        return True
    return search()


if not solve():
    print("No")

Buggy submission (verdict: Runtime Error):
from fractions import Fraction

h, w, a, b = map(int, input().split())


def search(r, c):
    for _ in range(b):
        print("0" * a + "1" * (w - a))
    for _ in range(b, H):
        print("1" * a + "0" * (w - a))
    return True


def solve():
    if a == 0 and b == 0:
        for _ in range(h):
            print("0" * w)
        return True
    if b == 0:
        for _ in range(h):
            print("0" * a + "1" * (w - a))
        return True
    if a == 0:
        for _ in range(b):
            print("0" * w)
        for _ in range(h - b):
            print("1" * w)
        return True
    if Fraction(w, h) == Fraction(a, b):
        for r in range(h):
            for c in range(w):
                if c // a == r // b:
                    print("1", end="")
                else:
                    print("0", end="")
            print()
        return True
    return search()


if not solve():
    print("No")
Problem p02936 (python)

Fixed submission:
import sys
from collections import defaultdict
sys.setrecursionlimit(100000)
N, Q = map(int, input().split())

neighbor = defaultdict(list)
counter = [0] * N
pcounter = [None] * N
depth = {0: 0}

for i in range(N-1):
    a, b = map(int, input().split())
    a_deper, b_deeper = False, False
    if a in depth and b not in depth:
        depth[b] = depth[a] + 1
    if b in depth and a not in depth:
        depth[a] = depth[b] + 1
    if a in depth and b in depth:
        if depth[a] < depth[b]:
            neighbor[a-1].append(b-1)
        else:
            neighbor[b-1].append(a-1)
    else:
            neighbor[a-1].append(b-1)
            neighbor[b-1].append(a-1)

for i in range(Q):
    p, x = map(int, input().split())
    counter[p-1] += x

def solve():
    stack = [(0, None)]
    while stack:
        (node, parent) = stack.pop()
        pcounter[node] = counter[node]
        if parent is not None:
            pcounter[node] += pcounter[parent]
        for c in neighbor[node]:
            if pcounter[c] is None:
                stack.append((c, node))

solve()
print(*pcounter, sep=' ')

Buggy submission (verdict: Runtime Error):
import sys
from collections import defaultdict
sys.setrecursionlimit(100000)
N, Q = map(int, input().split())

child = defaultdict(list)
counter = [0] * N
pcounter = [0] * N

for i in range(N-1):
    a, b = map(int, input().split())
    child[a-1].append(b-1)
for i in range(Q):
    p, x = map(int, input().split())
    counter[p-1] += x

def propagate(node, acc):
    counter[node] += acc
    acc = counter[node]
    for c in child[node]:
        propagate(c, acc)

propagate(0, 0)
print(*counter, sep=' ')

Buggy submission (verdict: Runtime Error):
from collections import defaultdict
N, Q = map(int, input().split())

parent = [None] * N
child = defaultdict(list)
counter = [0] * N
pcounter = [0] * N

for i in range(N-1):
    a, b = map(int, input().split())
    parent[b-1] = a-1
    child[a-1].append(b-1)
for i in range(Q):
    p, x = map(int, input().split())
    counter[p-1] += x

def propagate(node, acc):
    pcounter[node] = counter[node] + acc
    for c in child[node]:
        propagate(c, pcounter[node])

propagate(0, 0)
print(*pcounter, sep=' ')
Problem p02963 (python)

Fixed submission:
import math
S = int(input())
L = 10**9

def search():
    p = int(math.sqrt(S))
    q = p
    i = 0
    while p*q < S:
        if i % 2 == 0 or q == L: p += 1
        if i % 2 == 1 or p == L: q += 1
        i += 1
    assert p <= L and q <= L
    target = p*q - S
    divisor = 1
    #assert target < L
    return (0, 0, p, divisor, target, q)

print(*search())

Buggy submission (verdict: Runtime Error):
import math
S = int(input())
L = 10**9

def search():
    p = int(math.sqrt(S))
    q = p
    i = 0
    while p*q < S:
        if i % 2 == 0 or q == L-1: p += 1
        if i % 2 == 1 or p == L-1: q += 1
        i += 1
    assert p <= L and q <= L
    target = p*q - S
    divisor = 1
    #assert target < L
    return (0, 0, p, divisor, target, q)

print(*search())

Buggy submission (verdict: Runtime Error):
import math
S = int(input())
L = 10**9

def search():
    p = int(math.sqrt(S))
    q = p
    i = 0
    while p*q < S:
        if i % 2 == 0 or q == L-1: p += 1
        if i % 2 == 1 or p == L-1: q += 1
        i += 1
    assert p < L and q < L
    target = p*q - S
    divisor = 1
    #assert target < L
    return (0, 0, p, divisor, target, q)

print(*search())

Buggy submission (verdict: Runtime Error):
import math
S = int(input())
L = 10**9

def search():
    p = int(math.sqrt(S))
    q = p
    i = 0
    while p*q < S:
        if i % 2 == 0 or q == L-1: p += 1
        if i % 2 == 1 or p == L-1: q += 1
        i += 1
    if S%2 == 0 and (p*q)%2 == 1:
        q += 1
    elif S%2 == 1 and (p*q)%2 == 0:
        if p%2 == 0: p += 1
        if q%2 == 0: q += 1
    target = (p*q - S) // 2
    divisor = 2
    assert p < L
    assert q < L
    return (0, 0, p, divisor, target, q)

print(*search())

Buggy submission (verdict: Runtime Error):
import math
S = int(input())
L = 10**9

def search():
    p = int(math.sqrt(S))
    q = p
    i = 0
    while p*q < S:
        if i % 2 == 0 or q == L-1: p += 1
        if i % 2 == 1 or p == L-1: q += 1
        i += 1
    target = p*q - S
    assert target < L
    return (0, 0, p, 1, target, q)

print(*search())
Problem p03007 (python)

Fixed submission:
import sys

sys.setrecursionlimit(10 ** 8)
ini = lambda: int(sys.stdin.readline())
inm = lambda: map(int, sys.stdin.readline().split())
inl = lambda: list(inm())
ins = lambda: sys.stdin.readline().rstrip()
debug = lambda *a, **kw: print("\033[33m", *a, "\033[0m", **dict(file=sys.stderr, **kw))

N = ini()
A = inl()
A.sort()


def solve():
    big = A[-1]
    small = A[0]
    res = []
    for i in range(1, N - 1):
        if A[i] > 0:
            res.append((small, A[i]))
            small -= A[i]
        else:
            res.append((big, A[i]))
            big -= A[i]
    res.append((big, small))
    print(big - small)
    for x, y in res:
        print(x, y)


solve()

Buggy submission (verdict: Runtime Error):
import sys

sys.setrecursionlimit(10 ** 8)
ini = lambda: int(sys.stdin.readline())
inm = lambda: map(int, sys.stdin.readline().split())
inl = lambda: list(inm())
ins = lambda: sys.stdin.readline().rstrip()
debug = lambda *a, **kw: print("\033[33m", *a, "\033[0m", **dict(file=sys.stderr, **kw))

N = ini()
A = inl()
A.sort()


def solve():
    i = N - 1
    j = 0
    big = A[i]
    small = A[j]
    i -= 1
    j += 1
    res = []
    while i <= j:
        if i == j:
            res.append((small, A[i]))
            small -= A[i]
            break
        res.append((big, A[j]))
        big -= A[j]
        res.append((small, A[i]))
        small -= A[i]
        i -= 1
        j += 1

    print(big - small)
    for x, y in res:
        print(x, y)
    print(big, small)


solve()

Buggy submission (verdict: Runtime Error):
import sys

sys.setrecursionlimit(10 ** 8)
ini = lambda: int(sys.stdin.readline())
inm = lambda: map(int, sys.stdin.readline().split())
inl = lambda: list(inm())
ins = lambda: sys.stdin.readline().rstrip()
debug = lambda *a, **kw: print("\033[33m", *a, "\033[0m", **dict(file=sys.stderr, **kw))

N = ini()
A = inl()
A.sort()


def solve():
    i = N - 1
    j = 0
    big = A[i]
    small = A[j]
    i -= 1
    j += 1
    res = []
    while i <= j:
        if i == j:
            small -= A[i]
            res.append((small, A[i]))
            break
        big -= A[j]
        res.append((big, A[j]))
        small -= A[i]
        res.append((small, A[i]))
        i -= 1
        j += 1

    print(big - small)
    for x, y in res:
        print(x, y)


solve()
Problem p03044 (python)

Fixed submission:
import sys

sys.setrecursionlimit(10 ** 9)
input = sys.stdin.readline

N = int(input())
en = [[] for _ in range(N)]
on = [[] for _ in range(N)]
for i in range(N - 1):
    u, v, w = map(int, input().split())
    if w % 2:
        on[u - 1].append(v - 1)
        on[v - 1].append(u - 1)
    else:
        en[u - 1].append(v - 1)
        en[v - 1].append(u - 1)

color = [None] * N


def dfs(node, parent, col):
    if color[node] is not None:
        return
    color[node] = col
    for k in en[node]:
        if k != parent:
            dfs(k, node, col)
    for k in on[node]:
        if k != parent:
            dfs(k, node, (col + 1) % 2)


dfs(0, -1, 0)
print(*color, sep="\n")

Buggy submission (verdict: Runtime Error):
N = int(input())
en = [[] for _ in range(N)]
on = [[] for _ in range(N)]
for i in range(N - 1):
    u, v, w = map(int, input().split())
    if w % 2:
        on[u - 1].append(v - 1)
        on[v - 1].append(u - 1)
    else:
        en[u - 1].append(v - 1)
        en[v - 1].append(u - 1)

color = [None] * N


def dfs(node, parent, col):
    if color[node] is not None:
        return
    color[node] = col
    for k in en[node]:
        if k != parent:
            dfs(k, node, col)
    for k in on[node]:
        if k != parent:
            dfs(k, node, (col + 1) % 2)


dfs(0, -1, 0)
print(*color, sep="\n")
Problem p03047 (python)

Fixed submission:
import sys

sys.setrecursionlimit(10 ** 8)
ni = lambda: int(sys.stdin.readline())
nm = lambda: map(int, sys.stdin.readline().split())
nl = lambda: list(nm())
ns = lambda: sys.stdin.readline().rstrip()

N, K = nm()


def solve():
    return N - K + 1


if __name__ == "__main__":
    print(solve())

Buggy submission (verdict: Runtime Error):
import sys
N = int(sys.stdin.readline())
ss = []
for i in range(N):
    ss.append(sys.stdin.readline().rstrip())

incount = 0
has_ba = 0
has_b = 0
has_a = 0
for s in ss:
    incount += s.count('AB')
    if s[0] == 'B' and s[-1] == 'A':
        has_ba += 1
    elif s[0] == 'B':
        has_b += 1
    elif s[-1] == 'A':
        has_a += 1

res = incount
if has_a:
    res += has_ba
elif has_ba:
    has_a += 1
    has_ba -= 1
    res += has_ba
res += min(has_a, has_b)

print(res)
Problem p03160 (python)

Fixed submission:
import sys
f = sys.stdin
n = int(f.readline())
h = list(map(int, f.readline().split()))
dp = [0]*n
dp[1] = abs(h[1] - h[0])
for i in range(2, n):
    dp[i] = min(dp[i - 1] + abs(h[i] - h[i - 1]), dp[i - 2] + abs(h[i] - h[i - 2]))
print(dp[-1])

Buggy submission (verdict: Runtime Error):
import sys

# import numpy as np


def main():
    f = sys.stdin.buffer
    N, K = map(int, f.readline().split())
    H = list(map(int, f.readline().split()))
    dp = [0] * N
    for i in range(1, N):
        dp[i] = min(
            dp[i - k - 1] + abs(H[i] - H[i - k - 1]) for k in range(K) if i - k - 1 >= 0
        )
    print(dp[-1])


if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
import sys
import numpy as np


def main():
    f = sys.stdin.buffer
    N, K = map(int, f.readline().split())
    H = list(map(int, f.readline().split()))
    dp = [0] * N
    for i in range(1, N):
        dp[i] = min(
            dp[i - k - 1] + abs(H[i] - H[i - k - 1]) for k in range(K) if i - k - 1 >= 0
        )
    print(dp[-1])


if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
import numpy as np
import sys
f = sys.stdin
n = int(f.readline())
h = list(map(int, f.readline().split()))
h = np.array(h)
dp = np.zeros(n, int)
dp[1] = abs(h[1] - h[0])
for i in range(2, n):
    dp[i] = min(dp[i - 1] + abs(h[i] - h[i - 1]), dp[i - 2] + abs(h[i] - h[i - 2]))
print(dp[-1])
Problem p03161 (python)

Fixed submission:
import sys

# import numpy as np


def main():
    f = sys.stdin.buffer
    N, K = map(int, f.readline().split())
    H = list(map(int, f.readline().split()))
    dp = [0] * N
    for i in range(1, N):
        dp[i] = min(
            dp[i - k - 1] + abs(H[i] - H[i - k - 1]) for k in range(K) if i - k - 1 >= 0
        )
    print(dp[-1])


if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
N, K = map(int, input().split())
H = list(map(int, input().split()))
dp = [1 << 62] * N
dp[0] = 0
for i in range(1, N):
    for j in range(i, min(i + 1 + K, N)):
        cost = abs(H[i - 1] - H[i + j])
        dp[i + j] = min(dp[i + j], dp[i - 1] + cost)
print(dp[-1])
Problem p03163 (python)

Fixed submission:
import sys

sys.setrecursionlimit(10 ** 8)
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N, W = map(int, readline().split())
things = []
for i in range(N):
    weight, value = map(int, readline().split())
    things.append((weight, value))


def solve():
    dp = [0] * (W + 1)
    w0, v0 = things[0]
    for i in range(w0, W + 1):
        dp[i] = v0
    for i in range(1, N):
        dn = list(dp)
        weight, value = things[i]
        for j in range(weight, W + 1):
            dn[j] = max(dn[j], dp[j - weight] + value)
        dp = dn
    return dp[W]


if __name__ == "__main__":
    print(solve())

Buggy submission (verdict: Runtime Error):
import sys

sys.setrecursionlimit(10 ** 8)
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N, W = map(int, readline().split())
things = []
for i in range(N):
    weight, value = map(int, readline().split())
    things.append((weight, value))


def solve():
    dp = [0] * (W + 1)
    w0, v0 = things[0]
    for i in range(w0, W + 1):
        dp[i] = v0
    for i in range(1, N):
        dn = dp.copy()
        weight, value = things[i]
        for j in range(weight, W + 1):
            dn[j] = max(dn[j], dp[j - weight] + value)
        dp = dn
    return dp[W]


if __name__ == "__main__":
    print(solve())
Problem p03169 (python)

Fixed submission:
import collections
import sys

sys.setrecursionlimit(10 ** 8)
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N = int(readline())
A = [int(x) for x in readline().split()]
ac = collections.Counter(A)
assert set(ac.keys()).issubset({1, 2, 3})


def solve(c1, c2, c3):
    K = c1 + c2 + c3
    L = K + 1
    L2 = L ** 2
    dp = [0.0] * L ** 3
    for i3 in range(c3 + 1):
        for i2 in range(c2 + c3 + 1):
            for i1 in range(N - i2 - i3 + 1):
                isum = i1 + i2 + i3
                if isum == 0:
                    continue
                i0 = N - i1 - i2 - i3
                if i0 < 0:
                    continue
                e = i0
                if i1 > 0:
                    e += i1 * (1.0 + dp[(i1 - 1) * L2 + i2 * L + i3])
                if i2 > 0 and i1 <= K:
                    e += i2 * (1.0 + dp[(i1 + 1) * L2 + (i2 - 1) * L + i3])
                if i3 > 0 and i2 + 1 <= c2 + c3:
                    e += i3 * (1.0 + dp[i1 * L2 + (i2 + 1) * L + i3 - 1])
                dp[i1 * L2 + i2 * L + i3] = e / isum
    return dp[c1 * L2 + c2 * L + c3]


if __name__ == "__main__":
    print(solve(ac[1], ac[2], ac[3]))

Buggy submission (verdict: Runtime Error):
import collections
import sys
import numpy as np

sys.setrecursionlimit(10 ** 8)
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N = int(readline())
A = [int(x) for x in readline().split()]
ac = collections.Counter(A)
assert set(ac.keys()).issubset({1, 2, 3})
C1, C2, C3 = ac[1], ac[2], ac[3]
K = C1 + C2 + C3
dp = np.zeros((K + 3, C2 + C3 + 2, C3 + 1), dtype=np.float64)


def solve(c1, c2, c3):
    for i3 in range(c3 + 1):
        for i2 in range(c2 + c3 + 1):
            for i1 in range(K + 1):
                isum = i1 + i2 + i3
                i0 = N - i1 - i2 - i3
                if i0 < 0:
                    continue
                e = i0
                if i1 > 0:
                    e += i1 * (1.0 + dp[i1 - 1, i2, i3])
                if i2 > 0:
                    e += i2 * (1.0 + dp[i1 + 1, i2 - 1, i3])
                if i3 > 0:
                    e += i3 * (1.0 + dp[i1, i2 + 1, i3 - 1])
                dp[i1, i2, i3] = e / isum
    return dp[c1, c2, c3]


if __name__ == "__main__":
    print(solve(C1, C2, C3))
Problem p03263 (python)

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

out = []


def move(x1, y1, x2, y2):
    if 0 <= x2 < H and 0 <= y2 < W:
        coins[x1][y1] -= 1
        coins[x2][y2] += 1
        out.append((x1 + 1, y1 + 1, x2 + 1, y2 + 1))


for i in range(0, H, 2):
    for j in range(W):
        if coins[i][j] % 2:
            if j < W - 1:
                move(i, j, i, j + 1)
            else:
                move(i, j, i + 1, j)
    for j in range(W - 1, -1, -1):
        if i < H - 1 and coins[i + 1][j] % 2:
            if j > 0:
                move(i + 1, j, i + 1, j - 1)
            else:
                move(i + 1, j, i + 2, j)

print(len(out))
for c in out:
    print(*c)

Buggy submission (verdict: Runtime Error):
H, W = map(int, input().split())
coins = []
for i in range(H):
    a = list(map(int, input().split()))
    coins.append(a)


def move(x1, y1, x2, y2):
    print(x1 + 1, y1 + 1, x2 + 1, y2 + 1)
    coins[x1][y1] -= 1
    coins[x2][y2] += 1


for i in range(0, H, 2):
    for j in range(W):
        if coins[i][j] % 2:
            if j < W - 1:
                move(i, j, i, j + 1)
            else:
                move(i, j, i + 1, j)
    for j in range(W - 1, -1, -1):
        if coins[i + 1][j] % 2:
            if j > 0:
                move(i + 1, j, i + 1, j - 1)
            else:
                move(i + 1, j, i + 2, j)