Problem:
 Score : 500 points    Problem Statement Jumbo Takahashi will play golf on an infinite two-dimensional grid.  The ball is initially at the origin (0, 0) , and the goal is a grid point (a point with integer coordinates) (X, Y) . In one stroke, Jumbo Takahashi can perform the following operation:   Choose a grid point whose Manhattan distance from the current position of the ball is K , and send the ball to that point.   The game is finished when the ball reaches the goal, and the score will be the number of strokes so far. Jumbo Takahashi wants to finish the game with the lowest score possible.  Determine if the game can be finished. If the answer is yes, find one way to bring the ball to the goal with the lowest score possible.  What is Manhattan distance?  The Manhattan distance between two points (x_1, y_1) and (x_2, y_2) is defined as |x_1-x_2|+|y_1-y_2| .

Solution (Python):
import sys

K = int(input())
X, Y = map(int, input().split())
dest = (X, Y)
ans = 0
path = []


def sign(i):
    return 2*(i > 0)-1


def dist(p1, p2):
    return abs(p1[0]-p2[0])+abs(p1[1]-p2[1])


curpos = [0, 0]
if dist(curpos, dest) == K:
    print(1)
    print(X, Y)
    sys.exit()

while dist(curpos, dest) > 2*K:
    ans += 1
    xdiff = abs(curpos[0] - dest[0])
    curpos[0] += min(K, xdiff) * sign(dest[0] - curpos[0])
    curpos[1] += max(0, K - xdiff) * sign(dest[1] - curpos[1])
    path.append((curpos[0], curpos[1]))

if dist(curpos, dest) % 2 == 1:
    if K % 2 == 0:
        print(-1)
        sys.exit()
    ans += 1
    xdiff = abs(curpos[0] - dest[0])
    curpos[0] += min(K, xdiff) * sign(dest[0] - curpos[0])
    curpos[1] += max(0, K - xdiff) * sign(dest[1] - curpos[1])
    path.append((curpos[0], curpos[1]))
d = dist(curpos, dest)
rest = (2*K-d)//2
ans += 1
xdiff = abs(curpos[0] - dest[0])
if xdiff+rest >= K:
    curpos[0] += (K - rest) * sign(dest[0] - curpos[0])
    curpos[1] -= rest * sign(dest[1] - curpos[1])
else:
    curpos[0] += (xdiff+rest) * sign(dest[0]-curpos[0])
    curpos[1] += (K-(xdiff+rest)) * sign(dest[1] - curpos[1])
path.append((curpos[0], curpos[1]))
path.append(dest)
ans += 1
print(ans)
for p in path:
    print(p[0], p[1])

Solution (Python):
K = int(input())
X,Y = map(int,input().split())
dis = abs(X)+abs(Y)
if K % 2 == 0 and dis % 2 == 1:
    print(-1)
    exit()
time = -(-dis//K)
if time == 1:
    if dis != K:
        time += 1
if (time*K) % 2 != dis % 2:
    time += 1
print(time)
detour = (time*K-dis)//2
Xdir = 1
Ydir = 1
Xdis = abs(X)
Ydis = abs(Y)
Xcur = 0
Ycur = 0
if X < 0:
    Xdir = -1
if Y < 0:
    Ydir = -1
while time:
    if detour > 0:
        if detour <= K:
            if Ydis >= K-detour:
                Xcur += -Xdir*detour
                Xdis += detour
                Ycur += Ydir*(K-detour)
                Ydis -= K-detour
                detour = 0
            else:
                Ycur += -Ydir*detour
                Ydis += detour
                Xcur += Xdir*(K-detour)
                Xdis -= K-detour
                detour = 0
        else:
            Ycur += -Ydir*K
            Ydis += K
            detour -= K
    else:
        if Xdis >= K:
            Xdis -= K
            Xcur += Xdir*K
        else:
            Ycur += Ydir*(K-Xdis)
            Ydis -= K-Xdis
            Xcur += Xdir*Xdis
            Xdis = 0
    print(Xcur,Ycur)
    time -= 1

Solution (Python):
k=int(input())
x,y=map(int,input().split())
d=abs(x)+abs(y)
if k%2==0 and d%2==1:
  print(-1)
  exit()

nx,ny=0,0
ans=[]
flg=True
xty=False
if abs(x)<abs(y):
  pass
else:
  x,y=y,x
  xty=True
while d>2*k:
  dx=min(k,abs(x-nx))
  dy=k-dx
  if abs(x-nx):nx+=dx*(x-nx)//abs(x-nx)
  if abs(y-ny):ny+=dy*(y-ny)//abs(y-ny)
  ans.append([nx,ny])
  d=abs(x-nx)+abs(y-ny)
if d%2==1:
  dx=min(k,abs(x-nx))
  dy=k-dx
  if abs(x-nx):nx+=dx*(x-nx)//abs(x-nx)
  if abs(y-ny):ny+=dy*(y-ny)//abs(y-ny)
  ans.append([nx,ny])
  d=abs(x-nx)+abs(y-ny)


dd=d//2
dx=min(dd,abs(x-nx))
dy=dd-dx
mx,my=nx,ny
if abs(x-nx):mx=nx+dx*(x-nx)//abs(x-nx)
if abs(y-ny):my=ny+dy*(y-ny)//abs(y-ny)
flg=False


while [nx,ny]!=[x,y]:
  d=abs(x-nx)+abs(y-ny)
  
  
  
  if d%k==0:
    dx=min(k,abs(x-nx))
    dy=k-dx
    if abs(x-nx):nx+=dx*(x-nx)//abs(x-nx)
    if abs(y-ny):ny+=dy*(y-ny)//abs(y-ny)

  
  elif flg:
    if abs(x-nx):
      nx-=k*(x-nx)//abs(x-nx)
    else:
      ny-=k*(x-ny)//abs(x-ny)
      
  
  else:
    flg=True
    
    dy=min(k,abs(my-ny))
    dx=k-dy
    ny=my
    if mx-nx>0:
      nx+=dx
    else:
      nx-=dx

  ans.append([nx,ny])
print(len(ans))
for a,b in ans:
  if xty:
    print(b,a)
  else:
    print(a,b)

Solution (Python):
def solve(v, u, k):
    sign_v = sign_u = 1
    swap = 1
    if abs(v) < abs(u):
        v, u = u, v
        swap = -1
    if v < 0:
        v = -v
        sign_v = -1
    if u < 0:
        u = -u
        sign_u = -1
    if v % k == 0:
        n = v // k
        res = []
        cum_u = 0
        for i in range(n):
            du = min(k, max(-k, u-cum_u))
            if (k+du)%2:
                du += 1 if du < 0 else -1
            cum_u += du
            res.append([k*sign_v, du*sign_u][::swap])
        assert cum_u == u, (cum_u, u)
        return res
    elif v < k and v%2==0:
        res = [[k, u-k], [v-k, k]]
        res = [[dv * sign_v, du * sign_u][::swap] for dv, du in res]
        return res
    else:
        m = v % k
        if (k+m)%2:
            m -= 1
        res = [[m, k]] + solve(v-m, u-k, k)
        res = [[dv*sign_v, du*sign_u][::swap] for dv, du in res]
        return res

def test():
    inf = -1
    M = [[inf]*1000 for _ in range(1000)]
    K = 9
    q = [(0, 0)]
    R = 100
    ans = 1
    M[0][0] = 0
    while q:
        q_new = []
        for x, y in q:
            for i in range(-K, K+1, 2):
                ux = x+i
                uy = y+i
                if -R <= ux <= R:
                    if y + K <= R:
                        if M[ux][y+K] == inf:
                            M[ux][y+K] = ans
                            q_new.append((ux, y+K))
                    if y - K >= -R:
                        if M[ux][y-K] == inf:
                            M[ux][y-K] = ans
                            q_new.append((ux, y-K))
                if -R <= uy <= R:
                    if x + K <= R:
                        if M[x+K][uy] == inf:
                            M[x+K][uy] = ans
                            q_new.append((x+K, uy))
                    if x - K >= -R:
                        if M[x-K][uy] == inf:
                            M[x-K][uy] = ans
                            q_new.append((x-K, uy))
        q = q_new
        ans += 1
    for x in range(-R, R+1):
        for y in range(-R, R+1):
            if M[x][y] != -1:
                if len(solve(x, y, K)) != M[x][y]:
                    print(f"!! x={x} y={y} len(solve(x, y, K))={len(solve(x, y, K))} M[x][y]={M[x][y]}")
    print("test finished")



def main():
    K = int(input())
    X, Y = map(int, input().split())
    if K % 2 == 0 and (X + Y) % 2:
        print(-1)
        exit()
    V, U = X + Y, X - Y
    Ans = solve(V, U, K)
    print(len(Ans))
    cum_x = cum_y = 0
    for v, u in Ans:
        x, y = (v+u)//2, (v-u)//2
        assert abs(x)+abs(y) == K
        cum_x += x
        cum_y += y
        print(f"{cum_x} {cum_y}")
    assert cum_x == X and cum_y == Y

main()

Solution (Python):
import sys
import itertools

import time
import math
import heapq
from collections import defaultdict
from collections import Counter
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines



import sys
import itertools

import time
import math
import heapq
from collections import defaultdict
from collections import Counter
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines


K = int(input())
X, Y = map(int, input().split())
if K % 2 == 0 and (X + Y) % 2 == 1:
    print(-1)
    exit() 

def solve(x, y):
    if x < 0:
        dx, dy = solve(-x, y)
        return (-dx, dy)
    if y < 0:
        dx, dy = solve(x, -y)
        return (dx, -dy)
    if x > y:
        dx, dy = solve(y, x)
        return (dy, dx)

    l = x + y
    if l > 2 * K:
        dx = min(x, K)
        dy = K - dx
        return (dx, dy)
    if l == K:
        return x, y
    if l % 2 == 0:
        t = (y - x) // 2
        dx = K - t
        dy = t
        return (dx, dy)
    dx = min(x, K)
    dy = K - dx
    return dx, dy

ans = []
cur_x, cur_y = (0, 0)
while cur_x != X or cur_y != Y:
    dx, dy = solve(X - cur_x, Y - cur_y)
    cur_x += dx
    cur_y += dy
    ans.append((cur_x, cur_y))
print(len(ans))
for x, y in ans:
    print(x, y)

Solution (Python):
k = int(input())
x, y = map(int, input().split())
ans = []
if k % 2 == 0:
    if (x + y) % 2 == 1:
        print(-1)
        exit()
x_reverse = False
y_reverse = False
if x < 0:
    x *= -1
    x_reverse = True
if y < 0:
    y *= -1
    y_reverse = True


def app(a, b):
    global ans
    if x_reverse:
        a *= -1
    if y_reverse:
        b *= -1

    ans.append((a, b))


nowx = 0
nowy = 0

while abs(x - nowx) + abs(y - nowy) >= 2 * k:
    if abs(x - nowx) > k:
        nowx += k
    else:
        nowy += k
    app(nowx, nowy)


def ok():
    app(x, y)
    print(len(ans))
    for a, b in ans:
        print(a, b)
    exit()


rest = abs(x - nowx) + abs(y - nowy)
if rest == k:
    ok()

if rest % 2 == 0:
    delta = 2 * k - rest
    assert delta % 2 == 0
    if abs(x - nowx) < abs(y - nowy):
        temp = abs(x - nowx) + delta // 2
        nowx += temp
        nowy += k - temp
    else:
        temp = abs(y - nowy) + delta // 2
        nowy += temp
        nowx += k - temp
    app(nowx, nowy)
    ok()




if abs(x - nowx) < abs(y - nowy):
    nowx += k
    app(nowx, nowy)
else:
    nowy += k
    app(nowx, nowy)

x_delta = x - nowx
x_pm = x_delta // abs(x_delta)
y_delta = y - nowy
y_pm = y_delta // abs(y_delta)
if abs(x_delta) < abs(y_delta):
    delta = 2 * k - (abs(x_delta) + abs(y_delta))
    temp = abs(x - nowx) + delta // 2
    nowx += x_pm * temp
    nowy += y_pm * (k - temp)
    app(nowx, nowy)
    ok()
else:
    delta = 2 * k - (abs(x_delta) + abs(y_delta))
    temp = abs(y - nowy) + delta // 2
    nowy += y_pm * temp
    nowx += x_pm * (k - temp)
    app(nowx, nowy)
    ok()

Solution (Python):
K=int(input())
X,Y=map(int,input().split())
P=[]
A,B=0,0
V,W=1,1
if X<0:
  V=-1
  X=-X
if Y<0:
  W=-1
  Y=-Y
Z=0
if X<Y:
  X,Y=Y,X
  Z=1
if K&1==0 and (X+Y)&1==1:
  print(-1)
  exit()
def f(x):
  if x>=2*K:
    return ((x-K)//K)+f((x%K)+K)
  if x==K:
    return 1
  elif x==0:
    return 0
  elif x&1:
    return 3
  else:
    return 2

def g(X):
  if Z:
    for i in range(len(X)):
      X[i]=X[i][::-1]
  for i in range(len(X)):
    X[i][0],X[i][1]=X[i][0]*V,X[i][1]*W
  for i in range(len(X)-1):
    if K!=abs(X[i+1][0]-X[i][0])+abs(X[i+1][1]-X[i][1]):
      return 0
  return X

print(f(X+Y))
while Y-B>=K:
  B+=K
  P.append([0,B])
while X-A+Y-B>=2*K:
  A+=K
  P.append([A,B])
C=X-A+Y-B
if C%K==0:
  if C==K:
    P.append([X,Y])
  P=g(P)
  for i in range(len(P)):
    print(*P[i])
  exit()
Q=0
if f(C)==3:
  if A+K<=X:
    A+=K
    Q=1
  else:
    B+=K
    Q=-1
  P.append([A,B])
  C=X-A+abs(Y-B)
if C%K==0:
  if C==K:
    P.append([X,Y])
  P=g(P)
  for i in range(len(P)):
    print(*P[i])
  exit()
P.append(((A-B-X+Y)//2)%K)
P[-1]=[A+(K-P[-1]),B+P[-1]]
P.append([X,Y])
if g(P):
  for i in range(len(P)):
    print(*P[i])
  exit()
P=[]
A,B=0,0
while Y-B>=K:
  B+=K
  P.append([0,B])
while X-A+Y-B>=2*K:
  A+=K
  P.append([A,B])
C=X-A+Y-B
if C%K==0:
  if C==K:
    P.append([X,Y])
  P=g(P)
  for i in range(len(P)):
    print(*P[i])
  exit()
if f(C)==3:
  if A+K<=X:
    if Q==1:
      A-=2*K
    A+=K
  else:
    if Q==-1:
      B-=2*K
    B+=K
    Q=-1
  P.append([A,B])
  C=X-A+abs(Y-B)
if C%K==0:
  if C==K:
    P.append([X,Y])
  P=g(P)
  for i in range(len(P)):
    print(*P[i])
  exit()
P.append(((A-B-X+Y)//2)%K)
P[-1]=[A+(K-P[-1]),B+P[-1]]
P.append([X,Y])
if g(P):
  for i in range(len(P)):
    print(*P[i])
  exit()

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

k=II()
x,y=MI()


if k%2==0 and (x+y)%2:
    print(-1)
    exit()


cx=-1 if x<0 else 1
cy=-1 if y<0 else 1
x,y=abs(x),abs(y)
swap=False
if x>y:
    swap=True
    x,y=y,x

ans=[]
if (x+y)%2 and x+y<k:
    ans.append((x,y))
    y+=k
l=(x+y+k-1)//k*k
if (l-x-y)%2:l+=k
d=(l-x-y)//2
if d>0 and d+y<k:d+=k//2

if d:
    ans.append((x,y))
    x+=d
    y-=k-d

while y-k>=0:
    ans.append((x, y))
    y-=k

if y:
    ans.append((x,y))
    x-=k-y
    y=0

while x>0:
    ans.append((x, y))
    x-=k

print(len(ans))
for x,y in ans[::-1]:
    if swap:x,y=y,x
    print(cx*x,cy*y)

Solution (Python):
K = int(input())
X, Y = [int(_) for _ in input().split()]
x, signx = abs(X), -1 if X < 0 else 1
y, signy = abs(Y), -1 if Y < 0 else 1
ans = [(x, y)]


def calcxy(x, y):
    dx = min(K, x)
    dy = K - dx
    x -= dx
    if y > dy:
        y -= dy
    else:
        y += dy
    return x, y


def calczw(x, y):
    
    if x > y:
        
        z = (x - y) // 2
        w = K - z
    else:
        
        w = (y - x) // 2
        z = K - w
    return (z, w)


if x + y != K:
    while (x + y) > 2 * K:
        x, y = calcxy(x, y)
        ans += [(x, y)]
    z, w = calczw(x, y)
    if abs(z - x) + abs(w - y) != K:
        x, y = calcxy(x, y)
        ans += [(x, y)]
        z, w = calczw(x, y)
    if abs(z - x) + abs(w - y) != K:
        print(-1)
        exit()
    ans += [(z, w)]
print(len(ans))
for x, y in ans[::-1]:
    print(x * signx, y * signy)

Solution (Python):
K = int(input())
X, Y = [int(_) for _ in input().split()]
x, signx = abs(X), -1 if X < 0 else 1
y, signy = abs(Y), -1 if Y < 0 else 1
ans = [(x, y)]
if x + y != K:
    while (x + y) > 2 * K:
        dx = min(K, x)
        dy = K - dx
        x -= dx
        y -= dy
        ans += [(x, y)]

    def calczw(x, y):
        
        if x > y:
            
            z = (x - y) // 2
            w = K - z
        else:
            
            w = (y - x) // 2
            z = K - w
        return (z, w)

    z, w = calczw(x, y)
    if abs(z - x) + abs(w - y) != K:
        dx = min(K, x)
        dy = K - dx
        x -= dx
        if y > dy:
            y -= dy
        else:
            y += dy
        ans += [(x, y)]
        z, w = calczw(x, y)
    if abs(z - x) + abs(w - y) != K:
        print(-1)
        exit()
    ans += [(z, w)]
print(len(ans))
for x, y in ans[::-1]:
    print(x * signx, y * signy)

Solution (Python):
k = int(input())
X,Y = map(int,input().split())
x = max(abs(X),abs(Y))
y = min(abs(X),abs(Y))
c = (k-(x+y)%k)%k
if k%2 == 0 and (x+y)%2 == 1:
  print(-1)
  exit()
  
a = []
m = ((x+y-1)//k+1)
if x+y < k:
  if (x+y)%2 == 1:
    a.append((x,x-k))
    a.append((x+(k+x-y)//2,(-k+x+y)//2))
  else:
    a.append(((x+y)//2,-k+(x+y)//2))
  
if c%2 == 1 and x+y >= k:
  m += 1
A = abs((k*m-x-y)//2)
for i in range(m-1):
  l = k*i+k
  if A >= l:
    a.append((0,-1))
  elif A < l <= x+A:
    a.append((l-A,-A))
  elif x+A < l:
    a.append((x,l-x-2*A))
    
a.append((x,y))
print(len(a))
for i,j in a:
  if abs(X) < abs(Y):
    i,j = j,i
  if X < 0:
    i *= -1
  if Y < 0:
    j *= -1
  print(i,j)

Solution (Python):
k = int(input())
X,Y = map(int,input().split())
x = max(abs(X),abs(Y))
y = min(abs(X),abs(Y))
c = (k-(x+y)%k)%k
if k%2 == 0 and (x+y)%2 == 1:
  print(-1)
  exit()
  
import math
ans = []
m = math.ceil((x+y)/k)
if (x+y)/k == int((x+y)/k):
  for i in range(m):
    ans.append((min(x,k*i+k),max(0,k*i+k-x)))
    
elif m == 1:
  if (x+y)%2 == 1:
    ans.append((x,x-k))
    ans.append((x+(k+x-y)//2,(-k+x+y)//2))
    ans.append((x,y))
  else:
    ans.append(((x+y)//2,-k+(x+y)//2))
    ans.append((x,y))
               
elif c%2 == 0 and x+y >= k:
  a = abs((k*m-x-y)//2)
  for i in range(m-1):
    l = k*i+k
    if a >= l:
      ans.append((0,-l))
    elif a < l <= x+a:
      ans.append((l-a,-a))
    elif x+a < l:
      ans.append((x,l-x-2*a))
  ans.append((x,y))
  
else:
  m += 1
  a = abs((k*m-x-y)//2)
  for i in range(m-1):
    l = k*i+k
    if a >= l:
      ans.append((0,-1))
    elif a < l <= x+a:
      ans.append((l-a,-a))
    elif x+a < l:
      ans.append((x,l-x-2*a))
  ans.append((x,y))
  
print(len(ans))
for i,j in ans:
  if abs(X) < abs(Y):
    i,j = j,i
  if X < 0:
    i *= -1
  if Y < 0:
    j *= -1
  print(i,j)

Solution (Python):
import sys
readline = sys.stdin.readline

def mhd(x, y):
    return abs(x) + abs(y)

def calc(j, x, y):
    sx, sy = 1, 1
    if x < 0:
        x = -x
        sx = -1
    if y < 0:
        y = -y
        sy = -1
    
    if ((j*K) + K - y + x)%2 == 0:
        a = ((j*K) + K - y + x)//2
        b = j*K-a
        a *= sx
        b *= sy
        if mhd(a, b) == j*K:
            return (a, b)
    if ((j*K) - K - y + x)%2 == 0:
        a = ((j*K) - K - y + x)//2
        b = j*K-a
        a *= sx
        b *= sy
        if mhd(a, b) == j*K:
            return (a, b)
    return None

K = int(readline())
X, Y = map(int, readline().split())
ans = None
if mhd(X, Y) == K:
    print(1)
    print(X, Y)
elif (X+Y)&1 and K%2 == 0:
    print(-1)
else:
    ans = [(X, Y)]
    while True:
        px, py = ans[-1]
        md = mhd(px, py)
        if md == K:
            break
        j = -(-md//K)
        r1 = calc(j-1, px, py)
        if r1 is not None:
            ans.append(r1)
            continue
        r2 = calc(j, px, py)
        if r2 is not None:
            ans.append(r2)
            continue
        ans.append((px+K, py))
    ans.reverse()
    print(len(ans))
    print('\n'.join('{} {}'.format(*a) for a in ans))

Solution (Python):
def solve():
    k = int(input())
    x, y = map(int, input().split())
    if k % 2 == 0 and (x + y) % 2 == 1:
        print(-1)
        return 0
    if x < 0:
        flipX = -1
        x = -x
    else:
        flipX = 1
    if y < 0:
        flipY = -1
        y = -y
    else:
        flipY = 1
    if x < y:
        flipXY = True
        x, y = y, x
    else:
        flipXY = False
    x, y = x - y, x + y
    ans = [(x, y)]
    while y - k >= k:
        y = y - k
        if x > k:
            x = x - k
        else:
            x = 0
        if (x + y) % 2:
            x = 1
        ans.append((x, y))
    if y != k:
        if y % 2 == 1: 
            if x < k:
                x += k
                y += 1
            else:
                x -= k
                y += 1
            ans.append((x, y))
        ans.append((k, y - k))
    ans = map(lambda z: ((z[0] + z[1]) // 2, (-z[0] + z[1]) // 2) , reversed(ans))
    if flipXY:
        ans = map(lambda z: (z[1], z[0]), ans)
    ans = list(map(lambda z: (flipX * z[0], flipY * z[1]), ans))
    print(len(ans))
    for x, y in ans:
        print(x, y)
    return 0
solve()

Solution (Python):
from math import ceil

K=int(input())
X,Y=map(int,input().split())
s=X+Y
t=Y-X

if K%2==1:
    if s%2==1:
        test=max(abs(s),abs(t))
        count=0
        if K>test:
            count=3
        elif K==test:
            count=1
        else:
            i=ceil((test-K)/(2*K))
            if i==0:
                count=3
            else:
                count=2*i+1
        ans=[(s,t)]
        while count>0:
            posx,posy=ans[-1]
            nposx=0;nposy=0
            if count>2:
                if posx>=0:
                    nposx=posx-K
                else:
                    nposx=posx+K
                if posy>=0:
                    nposy=posy-K
                else:
                    nposy=posy+K
            elif count==2:
                if posx>=0:
                    if 0>=posy:
                        nposx=K
                        nposy=posy+K
                    else:
                        nposx=K
                        nposy=posy-K
                else:
                    if 0>=posy:
                        nposx=-K
                        nposy=posy+K
                    else:
                        nposx=-K
                        nposy=posy-K
            else:
                nposx=nposy=0
            count-=1
            ans.append((nposx,nposy))
        print(len(ans)-1)
        ans=ans[::-1]
        for s,t in ans:
            if (s,t)!=(0,0):
                print((s-t)//2,(s+t)//2)
    else:
        test=max(abs(s),abs(t))
        count=0
        if K>test:
            count=2
        else:
            i=ceil((test)/(2*K))
            if i==1:
                count=2
            else:
                count=2*i
        ans=[(s,t)]
        while count>0:
            posx,posy=ans[-1]
            nposx=0;nposy=0
            if count>2:
                if posx>=0:
                    nposx=posx-K
                else:
                    nposx=posx+K
                if posy>=0:
                    nposy=posy-K
                else:
                    nposy=posy+K
            elif count==2:
                if posx>=0:
                    if 0>=posy:
                        nposx=K
                        nposy=posy+K
                    else:
                        nposx=K
                        nposy=posy-K
                else:
                    if 0>=posy:
                        nposx=-K
                        nposy=posy+K
                    else:
                        nposx=-K
                        nposy=posy-K
            else:
                nposx=nposy=0
            count-=1
            ans.append((nposx,nposy))
        print(len(ans)-1)
        ans=ans[::-1]
        for s,t in ans:
            if (s,t)!=(0,0):
                print((s-t)//2,(s+t)//2)
else:
    if s%2==1:
        print(-1)
    else:
        test=max(abs(s),abs(t))
        count=0
        if K>test:
            count=2
        elif K==test:
            count=1
        else:
            i=ceil((test)/(K))
            if i==1:
                count=2
            else:
                count=i
        ans=[(s,t)]
        while count>0:
            posx,posy=ans[-1]
            nposx=0;nposy=0
            if count>2:
                if posx>=0:
                    nposx=posx-K
                else:
                    nposx=posx+K
                if posy>=0:
                    nposy=posy-K
                else:
                    nposy=posy+K
            elif count==2:
                if posx>=0:
                    if 0>=posy:
                        nposx=K
                        nposy=posy+K
                    else:
                        nposx=K
                        nposy=posy-K
                else:
                    if 0>=posy:
                        nposx=-K
                        nposy=posy+K
                    else:
                        nposx=-K
                        nposy=posy-K
            else:
                nposx=nposy=0
            count-=1
            ans.append((nposx,nposy))
        print(len(ans)-1)
        ans=ans[::-1]
        for s,t in ans:
            if (s,t)!=(0,0):
                print((s-t)//2,(s+t)//2)

Solution (Python):



def main():
    K = int(input())
    X, Y = map(int, input().split())

    if K % 2 == 0 and (X + Y) % 2 != 0:
        print(-1)
        return

    def next_vec(x, y):
        
        if x < 0:
            dx, dy = next_vec(-x, y)
            return -dx, dy
        if y < 0:
            dx, dy = next_vec(x, -y)
            return dx, -dy
        if x < y:
            dx, dy = next_vec(y, x)
            return dy, dx
        
        if x + y == K:
            return x, y
        
        if x + y <= K + K and (x + y) % 2 == 0:
            return (x + y) // 2, (x + y) // 2 - K
        
        return K, 0

    x, y = 0, 0
    ans = []
    while (x, y) != (X, Y):
        dx, dy = next_vec(X - x, Y - y)
        x += dx
        y += dy
        ans.append('{} {}'.format(x, y))

    print(len(ans))
    print(*ans, sep='\n')


if __name__ == '__main__':
    main()

"""
写経して計算の意味を考えた

(1)
x+y=D<2K
2手要する
S(A,B)->M(?,?)->G(X,Y)
S->M: 距離K未満=D-K詰め
M->G: 距離K詰め

S,Gから等距離Lの点M'
M'からGから離れる方向に距離Fの点M
SM'=M'G: 距離L
MM': 距離F

S->M'->M->M'->G
(S->M'->M)->M'->G: 初手
S->M': Gに距離L近づく
M'->M: Gから距離F離れる
dD_1=L-F=D-K...1

S->M'->(M->M'->G): 次手
M->M': Gに距離F近づく
M'->G: Gに距離L近づく
dD_2=L+F=K...2

1,2の連立で
L=D/2<K
F=K-D/2...(1)と符合逆だがこれは大きさ

SからGに向かって
L=D/2近づき
F離れることは可能か？
D=x+y
x>=yとすると
2D=2(x+y)<=2(2x)
D<=2x
D/2<=x
x,y方向の離れている距離のうちより離れている方向xでは
距離D/2以上離れているので、
この方向でSからGに向かってL=D/2近づくこと(=Mに到達)はできる
その後、y方向でMからGに向かうのと逆方向に
F=K-D/2進めば
次にGに向かって最短経路を進むと
距離KでGに到達できるようなM'に到達できる
"""

Solution (Python):

import sys
from itertools import product
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
input=lambda: sys.stdin.readline().rstrip()
YesNo=lambda b: bool([print('Yes')] if b else print('No'))
YESNO=lambda b: bool([print('YES')] if b else print('NO'))
int1=lambda x:int(x)-1

def main():
    K=int(input())
    X,Y=map(int,input().split())
    
    def is_on_square(x0,y0,x1,y1):
        return (abs(x1-x0)==K and y0-K<=y1<=y0+K) or (abs(y1-y0)==K and x0-K<=x1<=x0+K)
    
    rX=X-Y
    rY=X+Y
    x,y=0,0
    ans=[]
    while True:
        
        if max(abs(x-rX),abs(y-rY))==K:
            ans.append((rX,rY))
            break
        
        elif abs(x-rX)<=K*2 and abs(y-rY)<=K*2:
            for nx,ny in list(product((x+K,x-K),(rY+K,rY-K)))+list(product((rX+K,rX-K),(y+K,y-K))):
                if is_on_square(x,y,nx,ny) and is_on_square(rX,rY,nx,ny) and (nx+ny)%2==0:
                    x,y=nx,ny
                    break
            else:
                
                if K%2:
                    if x<rX:
                        x+=K
                    else:
                        x-=K
                    if y<rY:
                        y+=K
                    else:
                        y-=K
                else:
                    ans=[]
                    break
        
        else:
            
            if abs(x-rX)<abs(y-rY):
                if abs(x-rX)>=K:
                    if x>rX:
                        x-=K
                    else:
                        x+=K
                    if y>rY:
                        y-=K
                    else:
                        y+=K
                else:
                    x=rX
                    if y>rY:
                        y-=K
                    else:
                        y+=K
                    if (x+y)%2:
                        x+=1
            else:
                if abs(y-rY)>=K:
                    if x>rX:
                        x-=K
                    else:
                        x+=K
                    if y>rY:
                        y-=K
                    else:
                        y+=K
                else:
                    y=rY
                    if x>rX:
                        x-=K
                    else:
                        x+=K
                    if (x+y)%2:
                        y+=1
        ans.append((x,y))
    
    if not ans:
        print(-1)
    else:
        print(len(ans))
        for x,y in ans:
            print((y+x)//2,(y-x)//2)
                
        

if __name__ == '__main__':
    main()

Solution (Python):
K = int(input())
X, Y = map(int,input().split())

if K%2 == 0 and (X+Y)%2:
    print(-1)
    exit(0)

def next(x,y):
    if x < 0:
        dx, dy = next(-x, y)
        return -dx, dy
    if y < 0:
        dx, dy = next(x, -y)
        return dx, -dy
    if x < y:
        dx, dy = next(y, x)
        return dy, dx
    if x + y == K:
        return x, y
    if x + y <= K * 2 and (x + y) % 2 == 0:
        return (x + y)//2, (x + y)//2 - K
    return K, 0

x, y = 0,0
ans = list()
while x != X or y != Y:
    dx, dy = next(X - x, Y - y)
    x += dx; y += dy
    ans.append((x, y))

print(len(ans))
for x, y in ans:
  print(x, y)

Solution (Python):
K=int(input())
X,Y=map(int,input().split())

sgn_X,sgn_Y=1,1
if X<0:
  X=-X
  sgn_X=-1
if Y<0:
  Y=-Y
  sgn_Y=-1

if K%2==0 and (X+Y)%2==1:
  print(-1)
else:
  xx,yy=0,0
  vec_list=[]
  while((X-xx)+(Y-yy)>2*K):
    if X-xx>K:
      xx+=K
      vec_list.append((K,0))
    elif Y-yy>K:
      yy+=K
      vec_list.append((0,K))
  
  diff_x=X-xx
  diff_y=Y-yy
  
  if diff_x+diff_y==K:
    vec_list.append((diff_x,diff_y))
  else:
    if (diff_x+diff_y)%2==1:
      if diff_x<=K:
        xx+=diff_x
        if K-diff_x<diff_y:
          yy+=K-diff_x
          vec_list.append((diff_x,K-diff_x))
        else:
          yy-=K-diff_x
          vec_list.append((diff_x,-K+diff_x))
      elif diff_y<=K:
        yy+=diff_y
        if K-diff_y<diff_x:
          xx+=K-diff_y
          vec_list.append((K-diff_y,diff_y))
        else:
          xx-=K-diff_y
          vec_list.append((-K+diff_y,diff_y))
      diff_x=X-xx          
      diff_y=Y-yy
      
    buf=2*K-(diff_x+diff_y)  
    if diff_x+buf//2<=K:
      vec_list.append((diff_x+buf//2,K-(diff_x+buf//2)))
      vec_list.append((-buf//2,K-buf//2))
    elif diff_y+buf//2<=K:
      vec_list.append((K-(diff_y+buf//2),diff_y+buf//2))
      vec_list.append((K-buf//2,-buf//2))
    else:
      raise("Error")    

  
  print(len(vec_list))
  pos_x,pos_y=0,0
  for x,y in vec_list:
    pos_x+=sgn_X*x
    pos_y+=sgn_Y*y      
    print(pos_x,pos_y)

Solution (Python):
K=int(input())
X,Y=map(int,input().split())

sgn_X,sgn_Y=1,1
if X<0:
  X=-X
  sgn_X=-1
if Y<0:
  Y=-Y
  sgn_Y=-1

if K%2==0 and (X+Y)%2==1:
  print(-1)
else:
  xx,yy=0,0
  vec_list=[]
  while((X-xx)+(Y-yy)>2*K):
    if X-xx>K:
      xx+=K
      vec_list.append((K,0))
    elif Y-yy>K:
      yy+=K
      vec_list.append((0,K))
  
  diff_x=X-xx
  diff_y=Y-yy
  
  if K%2==0:
    if diff_x+diff_y==K:
      vec_list.append((diff_x,diff_y))
    else:
      buf=2*K-(diff_x+diff_y)
      if diff_x+buf//2<=K:
        vec_list.append((diff_x+buf//2,K-(diff_x+buf//2)))
        vec_list.append((-buf//2,K-buf//2))
      elif diff_y+buf//2<=K:
        vec_list.append((K-(diff_y+buf//2),diff_y+buf//2))
        vec_list.append((K-buf//2,-buf//2))
      else:
        raise("Error")    
          
  else:
    if diff_x+diff_y==K:
      vec_list.append((diff_x,diff_y))
    else:
      if (diff_x+diff_y)%2==1:
        if diff_x<=K:
          xx+=diff_x
          if K-diff_x<diff_y:
            yy+=K-diff_x
            vec_list.append((diff_x,K-diff_x))
          else:
            yy-=K-diff_x
            vec_list.append((diff_x,-K+diff_x))
        elif diff_y<=K:
          yy+=diff_y
          if K-diff_y<diff_x:
            xx+=K-diff_y
            vec_list.append((K-diff_y,diff_y))
          else:
            xx-=K-diff_y
            vec_list.append((-K+diff_y,diff_y))

      diff_x=X-xx
      diff_y=Y-yy
      buf=2*K-(diff_x+diff_y)  
      if diff_x+buf//2<=K:
        vec_list.append((diff_x+buf//2,K-(diff_x+buf//2)))
        vec_list.append((-buf//2,K-buf//2))
      elif diff_y+buf//2<=K:
        vec_list.append((K-(diff_y+buf//2),diff_y+buf//2))
        vec_list.append((K-buf//2,-buf//2))
      else:
        raise("Error")    

  
  print(len(vec_list))
  pos_x,pos_y=0,0
  for x,y in vec_list:
    pos_x+=sgn_X*x
    pos_y+=sgn_Y*y
      
    print(pos_x,pos_y)