Problem p03245 (python)

Fixed submission:
import sys

N = int(input())
XY = [[int(_) for _ in input().split()] for i in range(N)]

mc = [0, 0]
maxl = 0

for x, y in XY:
    l = abs(x) + abs(y)
    maxl = max(maxl, l)
    mc[l % 2] += 1

if mc[0] > 0 and mc[1] > 0:
    print(-1)
    sys.exit()

#print(maxl)

def calc(sx, sy, ex, ey, d):
    dx, dy = ex - sx, ey - sy
    rx, ry = dx - dy, dx + dy
    if rx > 0:
        if ry > 0:
            d, x, y = "R", sx + d, sy
        else:
            d, x, y = "D", sx, sy - d
    else:
        if ry > 0:
            d, x, y = "U", sx, sy + d
        else:
            d, x, y = "L", sx - d, sy
    return d, x, y

import math

l = math.ceil(math.log(maxl + 1, 2))

ms = [2 ** (l - i - 1) for i in range(l)]

rms = ms

if mc[0] > 0:
    rms = [1] + ms

print(len(rms))
print(*rms)

for ex, ey in XY:
    x, y = 0, 0
    w = ""
    if mc[0] > 0:
        w = "R"
        x += 1
    for m in ms:
        d, x, y = calc(x, y, ex, ey, m)
        w += d
    print(w)

Buggy submission (verdict: Runtime Error):
import sys

N = int(input())
XY = [[int(_) for _ in input().split()] for i in range(N)]

mc = [0, 0]
maxl = 0

for x, y in XY:
    l = abs(x) + abs(y)
    maxl = max(maxl, l)
    mc[l % 2] += 1

if mc[0] > 0 and mc[1] > 0:
    print(-1)
    sys.exit()

#if maxl > 20:
#    raise

def calc(sx, sy, dx, dy, d):
    #print("#", sx, sy, dx, dy)
    x = dx - sx
    y = dy - sy
    tx = x + 3 * d - y
    ty = x + 3 * d + y
    dirs = ("LL", "LD", "DD"), ("LU", "UD", "RD"), ("UU", "RU", "RR")
    ds = {"R": (+d, 0), "L": (-d, 0), "U": (0, +d), "D": (0, -d)}
    dir = dirs[ty // (2 * d)][tx // (2 * d)]
    ex, ey = sx, sy
    for d in dir:
        ex += ds[d][0]
        ey += ds[d][1]
    #print("*", dir, ex, ey)
    return dir, ex, ey


ds = []
ds0 = []

DM = 19

for i in range(DM):
    d = 3 ** (DM - i - 1)
    ds.append(d)
    ds0.append(d)
    ds0.append(d)

w0 = ""
x0, y0 = 0, 0

if mc[1] > 0:
    ds0 = [1] + ds0
    w0 += "R"
    x0 += 1

print(len(ds0))
print(*ds0)

for i in range(N):
    w = w0
    x, y = x0, y0
    for j, d in enumerate(ds):
        wc, x, y = calc(x, y, XY[i][0], XY[i][1], d)
        w += wc
    print(w)

Buggy submission (verdict: Runtime Error):
import sys

N = int(input())
XY = [[int(_) for _ in input().split()] for i in range(N)]

mc = [0, 0]
maxl = 0

for x, y in XY:
    l = abs(x) + abs(y)
    maxl = max(maxl, l)
    mc[l % 2] += 1

if mc[0] > 0 and mc[1] > 0:
    print(-1)
    sys.exit()

if maxl > 20:
    raise

def calc(sx, sy, dx, dy, d):
    #print("#", sx, sy, dx, dy)
    x = dx - sx
    y = dy - sy
    tx = x + 3 * d - y
    ty = x + 3 * d + y
    dirs = ("LL", "LD", "DD"), ("LU", "UD", "RD"), ("UU", "RU", "RR")
    ds = {"R": (+d, 0), "L": (-d, 0), "U": (0, +d), "D": (0, -d)}
    dir = dirs[ty // (2 * d)][tx // (2 * d)]
    ex, ey = sx, sy
    for d in dir:
        ex += ds[d][0]
        ey += ds[d][1]
    #print("*", dir, ex, ey)
    return dir, ex, ey


ds = [9, 3, 1]
ds0 = [9, 9, 3, 3, 1, 1]

w0 = ""
x0, y0 = 0, 0

if mc[1] > 0:
    ds0 = [1] + ds0
    w0 += "R"
    x0 += 1

print(len(ds0))
print(*ds0)

for i in range(N):
    w = w0
    x, y = x0, y0
    for j, d in enumerate(ds):
        wc, x, y = calc(x, y, XY[i][0], XY[i][1], d)
        w += wc
    print(w)

Buggy submission (verdict: Runtime Error):
import sys

N = int(input())
XY = [[int(_) for _ in input().split()] for i in range(N)]

mc = [0, 0]
maxl = 0

for x, y in XY:
    l = abs(x) + abs(y)
    maxl = max(maxl, l)
    mc[l % 2] += 1

if mc[0] > 0 and mc[1] > 0:
    print(-1)
    sys.exit()

if maxl > 20:
    raise

def calc(sx, sy, dx, dy, d):
    #print("#", sx, sy, dx, dy)
    x = dx - sx
    y = dy - sy
    tx = x + 3 * d - y
    ty = x + 3 * d + y
    dirs = ("LL", "LD", "DD"), ("LU", "UD", "RD"), ("UU", "RU", "RR")
    ds = {"R": (+d, 0), "L": (-d, 0), "U": (0, +d), "D": (0, -d)}
    dir = dirs[ty // (2 * d)][tx // (2 * d)]
    ex, ey = sx, sy
    for d in dir:
        ex += ds[d][0]
        ey += ds[d][1]
    #print("*", dir, ex, ey)
    return dir, ex, ey


ds = [9, 3, 1]
ds0 = ds

w0 = ""
x0, y0 = 0, 0

if mc[1] > 0:
    ds0 = [1] + ds0
    w0 += "R"
    x0 += 1

print(len(ds0))
print(*ds0)

for i in range(N):
    w = w0
    x, y = x0, y0
    for j, d in enumerate(ds):
        wc, x, y = calc(x, y, XY[i][0], XY[i][1], d)
        w += wc
    print(w)

Buggy submission (verdict: Runtime Error):
import sys

N = int(input())
XY = [[int(_) for _ in input().split()] for i in range(N)]

mc = [0, 0]
maxl = 0

for x, y in XY:
    l = abs(x) + abs(y)
    maxl = max(maxl, l)
    mc[l % 2] += 1

if mc[0] > 0 and mc[1] > 0:
    print(-1)
    sys.exit()

if maxl > 20:
    raise

def calc(sx, sy, dx, dy, d):
    #print("#", sx, sy, dx, dy)
    x = dx - sx
    y = dy - sy
    tx = x + 3 * d - y
    ty = x + 3 * d + y
    dirs = ("RR", "RD", "DD"), ("RU", "RL", "LD"), ("UU", "LU", "LL")
    ds = {"R": (-d, 0), "L": (d, 0), "U": (0, d), "D": (0, -d)}
    dir = dirs[ty // (2 * d)][tx // (2 * d)]
    ex, ey = sx, sy
    for d in dir:
        ex += ds[d][0]
        ey += ds[d][1]
    #print("*", dir, ex, ey)
    return dir, ex, ey


ds = [9, 3, 1]
ds0 = ds

w0 = ""
x0, y0 = 0, 0

if mc[1] > 0:
    ds0 = [1] + ds0
    w0 += "L"
    x0 += 1

print(len(ds0))
print(*ds0)

for i in range(N):
    w = w0
    x, y = x0, y0
    for j, d in enumerate(ds):
        wc, x, y = calc(x, y, XY[i][0], XY[i][1], d)
        w += wc
    print(w)

Buggy submission (verdict: Runtime Error):
import sys

N = int(input())
XY = [[int(_) for _ in input().split()] for i in range(N)]

mc = [0, 0]
maxl = 0

for x, y in XY:
    l = abs(x) + abs(y)
    maxl = max(maxl, l)
    mc[l % 2] += 1

if mc[0] > 0 and mc[1] > 0:
    print(-1)
    sys.exit()

if maxl > 20:
    raise
    
def calc(sx, sy, dx, dy, d):
    #print("#", sx, sy, dx, dy)
    x = dx - sx
    y = dy - sy
    tx = x + 3 * d - y
    ty = x + 3 * d + y
    dirs = ("RR", "RD", "DD"), ("RU", "RL", "LD"), ("UU", "LU", "LL")
    ds = {"R": (-d, 0), "L": (d, 0), "U": (0, d), "D": (0, -d)}
    dir = dirs[ty // (2 * d)][tx // (2 * d)]
    ex, ey = sx, sy
    for d in dir:
        ex += ds[d][0]
        ey += ds[d][1]
    #print("*", dir, ex, ey)
    return dir, ex, ey

ds = [9, 3, 1]

print(len(ds))
print(*ds)

for i in range(N):
    w = ""
    x, y = 0, 0
    for j, d in enumerate(ds):
        wc, x, y = calc(x, y, XY[i][0], XY[i][1], d)
        w += wc
    print(w)

Buggy submission (verdict: Runtime Error):
import sys

N = int(input())
XY = [[int(_) for _ in input().split()] for i in range(N)]

mc = [0, 0]
maxl = 0

for x, y in XY:
    l = abs(x) + abs(y)
    maxl = max(maxl, l)
    mc[l % 2] += 1

if mc[0] > 0 and mc[1] > 0:
    print(-1)
    sys.exit()

if maxl > 20:
    raise

print(maxl)
print(*([1]*maxl))

for x, y in XY:
    w = ""
    if x < 0:
        w += "L" * (-x)
    if x > 0:
        w += "R" * (+x)
    if y < 0:
        w += "D" * (-y)
    if y > 0:
        w += "U" * (+y)
    a = (maxl - len(w)) // 2
    if a > 0:
        w = "R"*a + w + "L"*a
    print(w)

Buggy submission (verdict: Runtime Error):
import sys

N = int(input())
XY = [[int(_) for _ in input().split()] for i in range(N)]

mc = [0, 0]
maxl = 0

for x, y in XY:
    l = abs(x) + abs(y)
    maxl = max(maxl, l)
    mc[l % 2] += 1

if mc[0] > 0 and mc[1] > 0:
    print(-1)
    sys.exit()

if maxl > 20:
    raise
    
print(maxl)
print(*([1]*maxl))

for x, y in XY:
    w = ""
    if x < 0:
        w += "L" * (-x)
    if x > 0:
        w += "R" * (+x)
    if y < 0:
        w += "D" * (-y)
    if y > 0:
        w += "U" * (+y)
    a = (maxl - len(w)) // 2
    if a > 0:
        if x == 0:
            w = "R"*a + w + "L"*a
        elif y < 0:
            w = "U"*a + w + "D"*a
        else:
            w = "D"*a + w + "U"*a
    print(w)

Buggy submission (verdict: Runtime Error):
import sys

N = int(input())
XY = [[int(_) for _ in input().split()] for i in range(N)]

mc = [0, 0]
maxl = 0

for x, y in XY:
    l = abs(x) + abs(y)
    maxl = max(maxl, l)
    mc[l % 2] += 1

if mc[0] > 0 and mc[1] > 0:
    print(-1)
    sys.exit()

print(maxl)
print(*([1]*maxl))

for x, y in XY:
    w = ""
    if x < 0:
        w += "L" * (-x)
    if x > 0:
        w += "R" * (+x)
    if y < 0:
        w += "D" * (-y)
    if y > 0:
        w += "U" * (+y)
    a = (maxl - len(w)) // 2
    if a > 0:
        if x == 0:
            w = "R"*a + w + "L"*a
        elif y < 0:
            w = "U"*a + w + "D"*a
        else:
            w = "D"*a + w + "U"*a
    print(w)