Problem p03245 (python)

Fixed submission:
N = int(input())

XY = []
parity = []
for _ in range(N):
    xy = list(map(int, input().split()))
    XY.append(xy)
    parity.append(sum(xy))


def check(ds, l, xy):
    x = 0
    y = 0
    for i in range(len(ds)):
        if l[i] == "R":
            x += ds[i]
        elif l[i] == "L":
            x -= ds[i]
        elif l[i] == "U":
            y += ds[i]
        elif l[i] == "D":
            y -= ds[i]
        else:
            raise Exception

    return (x == xy[0]) & (y == xy[1])


if len(list(set([i % 2 for i in parity]))) == 2:
    print(-1)

elif parity[0] % 2 == 0:
    print(33)
    ds = [1, 1] + [2 ** k for k in range(1, 32)]
    print(*ds)
    for xy in XY:
        rev_ans = ""
        curr_x, curr_y = 0, 0
        for d in ds[::-1]:
            x_diff = xy[0] - curr_x
            y_diff = xy[1] - curr_y
            if abs(x_diff) >= abs(y_diff):
                if x_diff >= 0:
                    rev_ans += "R"
                    curr_x += d
                else:
                    rev_ans += "L"
                    curr_x -= d
            else:
                if y_diff >= 0:
                    rev_ans += "U"
                    curr_y += d
                else:
                    rev_ans += "D"
                    curr_y -= d

        print(rev_ans[::-1])
        # print(check(ds, list(rev_ans[::-1]), xy))


else:  # odd
    print(32)
    ds = [2 ** k for k in range(32)]
    print(*ds)
    for xy in XY:
        rev_ans = ""
        curr_x, curr_y = 0, 0
        for d in ds[::-1]:
            x_diff = xy[0] - curr_x
            y_diff = xy[1] - curr_y
            if abs(x_diff) >= abs(y_diff):
                if x_diff >= 0:
                    rev_ans += "R"
                    curr_x += d
                else:
                    rev_ans += "L"
                    curr_x -= d
            else:
                if y_diff >= 0:
                    rev_ans += "U"
                    curr_y += d
                else:
                    rev_ans += "D"
                    curr_y -= d

        print(rev_ans[::-1])
        # print(check(ds, list(rev_ans[::-1]), xy))

Buggy submission (verdict: Runtime Error):
N = int(input())

XY = []
parity = []
for _ in range(N):
    xy = list(map(int, input().split()))
    XY.append(xy)
    parity.append(sum(xy))


def hor(x):
    ans = ""
    if x == 0:
        return ""
    if x > 0:
        ans += abs(x) * "R"
        # print(ans)
        return ans
    else:
        ans += abs(x) * "L"
        # print(ans)
        return ans


def ver(x):
    ans = ""
    if x == 0:
        return ""
    if x > 0:
        ans += abs(x) * "U"
        # print(ans)
        return ans
    else:
        ans += abs(x) * "D"
        # print(ans)
        return ans


if len(list(set([i % 2 for i in parity]))) == 2:
    print(-1)

else:
    # 300点狙いm=20, d=1とする
    par = parity[0] % 2
    if par == 0:
        # print("parity 0")
        print(20)
        print("1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1")
        for xy in XY:
            # print("test {}".format(xy))
            ans = ""
            s = abs(xy[0]) + abs(xy[1])
            N = (20 - s)//2
            ans += "RL" * N

            ans = ans + hor(xy[0])
            ans = ans + ver(xy[1])
            print(ans)

    elif par == 1:
        # print("parity 1")
        print(21)
        print("1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1")
        for xy in XY:
            ans = ""
            s = abs(xy[0]) + abs(xy[1])
            # print("test {}".format(xy))
            N = (21 - s)//2
            ans += "RL" * N
            ans = ans + hor(xy[0])
            ans = ans + ver(xy[1])
            print(ans)