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):
K = int(input())
X, Y = map(int, input().split())

def golf(a, b):
  if a < 0:
    t = golf(-a, b)
    return (-t[0], t[1])
  if b < 0:
    t = golf(a, -b)
    return (t[0], -t[1])
  if a > b:
    t = golf(b, a)
    return (t[1], t[0])
  
  if a + b >= K * 2:
    return (a, b - K)
  if a + b == K:
    return (0, 0)
  r = K - (a + b) // 2
  return (-r, a + b + r - K)

if K % 2 == 0 and (X + Y) % 2 == 1:
  print(-1)
else:
  res = []
  while X or Y:
    res.append((X, Y))
    X, Y = golf(X, Y)
  print(len(res))
  for r in res[: : -1]:
    print(r[0], r[1])

Solution (Python):
def solve(k,x,y):
    if k%2==0 and (x+y)%2==1:
        print(-1)
        return
    x_sign=1; y_sign=1
    xy_inv=False
    if x<0:
        x*=-1
        x_sign=-1
    if y<0:
        y*=-1
        y_sign=-1
    if x<y:
        xy_inv=True
        x,y=y,x

    def print_xy(x,y):
        if xy_inv:
            print(y*x_sign,x*y_sign)
        else:
            print(x*x_sign,y*y_sign)
        return

    if x+y<k and (x+y)%2==1:
        print(3)
        now_y=x-k
        print_xy(x,now_y)
        now_x=x+(k+x-y)//2
        now_y+=k-(k+x-y)//2
        print_xy(now_x,now_y)
        print_xy(x,y)
        return
    c=(k-((x+y)%k))%k
    if x+y<k:
        n=2
    else:
        n=((x+y+k-1)//k)+(c%2)
    print(n)
    ans=0
    now_y=-k
    l=-(k*n-x-y)//2
    while now_y>=l:
        print_xy(0,now_y)
        now_y-=k
    now_x=l-now_y
    while now_x<=x:
        print_xy(now_x,l)
        now_x+=k
    now_y=l+now_x-x
    while now_y<=y:
        print_xy(x,now_y)
        now_y+=k
    return

k=int(input())
x,y=map(int,input().split())
solve(k,x,y)

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

swap = False
if abs(X) < abs(Y) :
    swap = True
    X, Y = Y, X
    
signX, signY = 1, 1
if X < 0 : 
    signX = -1
    X = -X
if Y < 0 :
    signY = -1
    Y = -Y

def output(x, y) :
    if swap :
        print(y*signY, x*signX)
    else :
        print(x*signX, y*signY)

if K % 2 == 0 and (X + Y) % 2 == 1 :
    print(-1)
else :
    if K > X + Y and (X + Y) % 2 == 1 :
        print(3)
        output(X, -(K - X))
        output(X + (K + X - Y) // 2, -(K - X) + (K - X + Y) // 2)
        output(X, Y)
    else :
        if K > X + Y and (X + Y) % 2 == 0 :
            n = 2
        else :
            n = (X + Y + K - 1) // K
            if (X + Y) % 2 != n * K % 2 :
                n += 1
        print(n)
        a = (K*n-X-Y)//2//K
        b = (K*n+X-Y)//2//K
        x0 = K*(a+1)-(K*n-X-Y)//2
        y0 = K*(b+1)-X-(K*n-X-Y)
        for i in range(a) :
            output(0, -(i+1)*K)
        for i in range(b-a) :
            output(x0+i*K, -(K*n-X-Y)//2)
        for i in range(n-b) :
            output(X, y0 + i*K)

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

def solve(K, X, Y):
    if (X + Y) % 2 and K % 2 == 0:
        return None
    def gen(a, b, c, d):
        ans = []
        x = y = 0
        assert c-a == Y and b-d == X
        while a >= K:
            y -= K; a -= K
            ans.append((0, y))
        if a:
            x += K - a; y -= a
            b -= K - a
            ans.append((x, y))
        while b >= K:
            x += K; b -= K
            ans.append((x, y))
        if b:
            x += b; y += K - b
            c -= K - b
            ans.append((x, y))
        while c >= K:
            y += K; c -= K
            ans.append((x, y))
        if c:
            x -= K - c; y += c
            d -= K - c
            ans.append((x, y))
        while d >= K:
            x -= K; d -= K
            ans.append((x, y))
        assert X == x and Y == y, (x, y, ans)
        assert d == 0
        return ans

    ans = []
    if X + Y < K:
        if (X + Y) % 2 == 1:
            n = 3
            ans = gen(K-X, X + (K + X - Y)//2, K - X + Y, (K + X - Y)//2)
        else:
            n = 2
            ans = gen((K*n-X-Y)//2, X, (K*n-X+Y)//2, 0)
    else:
        c = (K - (X + Y)) % K
        if c % 2 == 1:
            n = (X + Y + K-1) // K + 1
        else:
            n = (X + Y + K-1) // K
        ans = gen((K*n-X-Y)//2, X, (K*n-X+Y)//2, 0)
    assert len(ans) == n
    return ans
rx = X < 0
if rx:
    X = -X
ry = Y < 0
if ry:
    Y = -Y
r = X < Y
if r:
    X, Y = Y, X
ans = solve(K, X, Y)
if ans:
    print(len(ans))
    for x, y in ans:
        if r:
            x, y = y, x
        if rx:
            x = -x
        if ry:
            y = -y
        print(x, y)
else:
    print(-1)

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

P = []
x, y = 0, 0

while (x, y) != (X, Y):
    dx = abs(X - x)
    dy = abs(Y - y)
    D = dx + dy
    if D == K:
        x, y = X, Y
    elif D % 2 != 0 and K % 2 == 0:
        print(-1)
        exit()
    elif D % 2 == 0 and D <= 2 * K:
        pos   = D // 2
        shift = (2 * K - D) // 2
        if pos <= dx:
            x += pos   if X > x else -pos
            y -= shift if Y > y else -shift
        else:
            x = X + (shift if X > x else -shift)
            y += pos - dx if Y > y else -(pos - dx)
    elif D < K:
        x -= K if X > x else -K
    elif dx >= K:
        x += K if X > x else -K
    else:
        x = X
        y += K - dx if Y > y else -(K - dx)
    P.append((x, y))

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

Solution (Python):
import math

k = int(input())
x, y = map(int, input().split())

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

a = 1
b = 1
d = 0
if x < 0:
    x *= -1
    a = -1
if y < 0:
    y *= -1
    b = -1
if x < y:
    d = 1
    x, y = y, x

def show_xy(tx, ty):
    if d == 1:
        print(ty * a, tx * b)
    else:
        print(tx * a, ty * b)

if x + y < k and (x + y) % 2 == 1:
    print(3)
    temp_x = x
    temp_y = x - k
    show_xy(temp_x, temp_y)
    temp_x = x + (k+x-y)//2
    temp_y += k - (k+x-y)//2
    show_xy(temp_x, temp_y)
    temp_x = x
    temp_y = y
    show_xy(temp_x, temp_y)
    exit()

c = (k - ((x + y) % k)) % k
if x + y < k:
    n = 2
else:
    n = math.ceil((x + y) / k) + (c % 2)

print(n)

ans = 0
temp_x = 0
temp_y = 0

temp = - (k * n - x - y) // 2
while 1:
    temp_y -= k
    if temp_y >=  temp:
        show_xy(temp_x, temp_y)
    else:
        temp_x = temp - temp_y
        temp_y = temp
        
        break

while temp_x <= x:
    show_xy(temp_x, temp_y)
    temp_x += k

temp_y += temp_x - x
temp_x = x

while temp_y <= y:
    show_xy(temp_x, temp_y)
    temp_y += k

Solution (Python):
k = int(input())
x, y = [int(item) for item in input().split()]


x_minus = False
if x < 0:
    x *= -1
    x_minus = True
y_minus = False
if y < 0:
    y *= -1
    y_minus = True

y_lt_x = False
if y > x:
    x, y = y, x
    y_lt_x = True


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

step_num = ((x + y) + (k - 1)) // k
d = (step_num * k - x - y) // 2
while d != 0 and (k > x + d or k > y + d + x):
    step_num += 1
    d = (step_num * k - x - y) // 2
if step_num * k % 2 != (x + y) % 2:
    step_num += 1
    d = (step_num * k - x - y) // 2



ans = []
step_len = 0
on_top = False
for i in range(step_num):
    step_len += k
    if step_len <= y + d:
        ans.append([0, step_len])
    elif step_len <= y + d + x:
        on_top = True
        ans.append([step_len - y - d, y + d])
    else:
        ans.append([x, (y + d) - (step_len - y - d - x)])
        if not on_top:
            newx, newy = ans[-1]
            prevx, prevy = ans[-2]
            if newy > prevy:
                b = abs(prevy - newy)
                a = k - abs(prevx - newx) - abs(prevy - newy)
                ans.pop() 
                ans.append([newx + b + a // 2, newy - b - a // 2])
            else:
                a = k - abs(prevx - newx) - abs(prevy - newy)
                ans.pop() 
                ans.append([newx + a // 2, newy - a // 2])
            on_top = True
print(len(ans))
for x, y in ans:
    if y_lt_x:
        x, y = y, x
    if x_minus:
        x *= -1
    if y_minus:
        y *= -1
    print(x, y)

Solution (Python):
def editarr(x,y,arr):
  l=len(arr)
  if abs(x)>=abs(y):
    for i in range(l):
      arr[i][0],arr[i][1]=arr[i][1],arr[i][0]
  if x<0:
    for i in range(l):
      arr[i][0]*=-1
  if y<0:
    for i in range(l):
      arr[i][1]*=-1

k=int(input())
x,y=map(int,input().split())
ans=[]
tx=abs(x)
ty=abs(y)
if tx>=ty:
  tx,ty=ty,tx
if k%2==0 and (tx+ty)%2!=0:
  print(-1)
else:
  if tx+ty<k:
    if (tx+ty)%2==0:
      m=(ty-tx)//2
      ans.append([k-m,m])
    else:
      td=k-(tx+ty)
      ans.append([tx,ty+td])
      ans.append([tx+(k-td//2),ty+td//2])
    ans.append([tx,ty])
  elif ty-(k-tx%k)<k and (ty-(k-tx%k))%2!=0:
    if (2*k-(tx+ty))%2==0:
      td=2*k-(tx+ty)
      ans.append([tx+td//2,k-(tx+td//2)])
      ans.append([tx,ty])
    else:
      ans.append([tx,k-tx])
      td=ty-(k-tx)
      ans.append([tx+(k-td//2),ans[-1][1]+td//2])
      ans.append([tx,ty])
  else:
    ans.append([0,0])
    for i in range(tx//k):
      ans.append([ans[-1][0]+k,0])
    if ans[-1][0]!=tx:
      ans.append([tx,k-abs(tx-ans[-1][0])])
    td=ty-ans[-1][1]
    if td%k==0:
      flag=True
    else:
      flag=False
    for i in range(td//k):
      if flag==False and (ty-ans[-1][1])%2==0 and (ty-ans[-1][1])<=2*k:
          break
      ans.append([tx,ans[-1][1]+k])
    if ans[-1][1]!=ty:
      td=ty-ans[-1][1]
      ans.append([tx+(k-td//2),ans[-1][1]+td//2])
      ans.append([tx,ty])
    ans.pop(0)
  editarr(x,y,ans)
  print(len(ans))
  for a,b in ans:
    print(a,b)

Solution (Python):
def editarr(x,y,arr):
  if abs(x)>=abs(y):
    for i in range(len(arr)):
      arr[i][0],arr[i][1]=arr[i][1],arr[i][0]
  if x<0:
    for i in range(len(arr)):
      arr[i][0]*=-1
  if y<0:
    for i in range(len(arr)):
      arr[i][1]*=-1

k=int(input())
x,y=map(int,input().split())
ans=[]
tx=abs(x)
ty=abs(y)
if tx>=ty:
  tx,ty=ty,tx
if k%2==0 and (tx+ty)%2!=0:
  print(-1)
else:
  if tx+ty<k:
    if (tx+ty)%2==0:
      m=(ty-tx)//2
      ans.append([k-m,m])
    else:
      td=k-(tx+ty)
      ans.append([tx,ty+td])
      ans.append([tx+(k-td//2),ty+td//2])
    ans.append([tx,ty])
    n=len(ans)
  elif ty-(k-tx%k)<k and (ty-(k-tx%k))%2!=0:
    if tx<k and ty<k:
      if (2*k-(tx+ty))%2==0:
        td=2*k-(tx+ty)
        ans.append([tx+td//2,k-(tx+td//2)])
        ans.append([tx,ty])
      else:
        ans.append([tx,k-tx])
        td=ty-(k-tx)
        ans.append([tx+(k-td//2),ans[-1][1]+td//2])
        ans.append([tx,ty])
      n=len(ans)
    else:
      if tx+ty<2*k:
        if (2*k-(tx+ty))%2==0:
          td=2*k-(tx+ty)
          ans.append([tx+td//2,k-(tx+td//2)])
          ans.append([tx,ty])
        else:
          ans.append([tx,k-tx])
          td=ty-(k-tx)
          ans.append([tx+(k-td//2),ans[-1][1]+td//2])
          ans.append([tx,ty])
        n=len(ans)
      else:
        ans.append([0,0])  
  else:
    ans.append([0,0])
    for i in range(tx//k):
      ans.append([ans[-1][0]+k,0])
    if ans[-1][0]!=tx:
      ans.append([tx,k-abs(tx-ans[-1][0])])
    td=ty-ans[-1][1]
    if td%k==0:
      flag=True
    else:
      flag=False
    for i in range(td//k):
      if flag==False and (ty-ans[-1][1])%2==0 and (ty-ans[-1][1])<=2*k:
          break
      ans.append([tx,ans[-1][1]+k])
    if ans[-1][1]!=ty:
      td=ty-ans[-1][1]
      ans.append([tx+(k-td//2),ans[-1][1]+td//2])
      ans.append([tx,ty])
    ans.pop(0)
    n=len(ans)
  editarr(x,y,ans)
  print(n)
  for a,b in ans:
    print(a,b)

Solution (Python):



import itertools


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


Mdist=abs(X)+abs(Y)



if Mdist%2==1 and K%2==0:
    print(-1)
    exit()



































if abs(X)+abs(Y)==K:
    print(1)
    print(X,Y)
    exit()
    




def searcheven(x,y):
    if abs(x)+abs(y)>2*K:
        return None
    if (abs(x)+abs(y))%2==1:
        return None
    
    for ps in itertools.product([-1,1],repeat=4):
        p1,p2,p3,p4=ps
        if p1*p4==p2*p3:
            
            
            if p3*(K-p1*x-p2*y)==-p1*K:
                a,b=0,p4*K
                if abs(x-a)+abs(y-b)==K and abs(a)+abs(b)==K:
                    return (a,b)
                a,b=p3*K,0
                if abs(x-a)+abs(y-b)==K and abs(a)+abs(b)==K:
                    return (a,b)
            else:
                pass
        else:
            
            tmp1=K-p1*x-p2*y
            tmp2=K
            tmp3=p4*tmp1+p2*tmp2
            tmp4=-p3*tmp1-p1*tmp2
            det=-p1*p4+p2*p3
            a,b=(tmp3//det,tmp4//det)
            
            if abs(x-a)+abs(y-b)==K and abs(a)+abs(b)==K:
                return (a,b)
            else:
                pass
    return None


ans=[(0,0)]
while(True):
    
    nx,ny=ans[-1]
    if abs(nx-X)+abs(ny-Y)<=2*K:
        
        if (abs(nx-X)+abs(ny-Y))%2==0:
            p,q=searcheven(X-nx,Y-ny)
            ans.append((p+nx,q+ny))
            ans.append((X,Y))
            break
        
        else:
            tmpx=K//2
            tmpy=K-tmpx
            nextkouho=[(tmpx,tmpy),(tmpx,-tmpy),(-tmpx,tmpy),(-tmpx,-tmpy),(K,0),(-K,0),(0,K),(0,-K)]
            for point in nextkouho:
                ptmp,qtmp=point
                p=ptmp+nx;q=qtmp+ny
                if abs(X-p)+abs(Y-q)<=2*K:
                    ans.append((p,q))
                    break
    
    
    elif abs(nx)+K<=abs(X):
        if X<0:
            ans.append((nx-K,ny))
            continue
        else:
            ans.append((nx+K,ny))
            continue
    elif abs(ny)+K<=abs(Y):
        if Y<0:
            ans.append((nx,ny-K))
            continue
        else:
            ans.append((nx,ny+K))
            continue





s=len(ans)-1
flag=1
for i in range(s):
    ax,ay=ans[i]
    bx,by=ans[i+1]
    if abs(ax-bx)+abs(ay-by)==K:
        pass
    else:
        flag=0
        break
if flag==0:
    print("NG")



print(s)
for i in range(1,s+1):
    print(ans[i][0],ans[i][1])

Solution (Python):
def e_golf(K, X, Y):
    
    

    
    is_xpos_negative = is_ypos_negative = is_swap = False
    x, y = X, Y
    if x < 0:
        x *= -1
        is_xpos_negative = True
    if y < 0:
        y *= -1
        is_ypos_negative = True
    if x < y:
        x, y = y, x
        is_swap = True

    if (x + y) % 2 == 1 and K % 2 == 0:
        return -1  

    ans = []
    shot_min = max((x + y + K - 1) // K, 2)
    if (x + y) % 2 != (shot_min * K) % 2:
        shot_min += 1
    if x + y == K:
        ans.append((x, y))
    elif shot_min == 3 and x < K:
        mid = (K + x - y) // 2
        ans.extend(((x, x - K), (x + mid, y - K + mid), (x, y)))
    else:
        over = (shot_min * K - x - y) // 2
        for i in range(1, shot_min + 1):
            d = i * K
            if d <= y + over:
                ans.append((0, d))
            elif d <= y + over + x:
                ans.append((d - y - over, y + over))
            else:
                ans.append((x, y + (shot_min - i) * K))

    
    if is_swap:
        ans = [(second, first) for first, second in ans]
    if is_xpos_negative:
        ans = [(-first, second) for first, second in ans]
    if is_ypos_negative:
        ans = [(first, -second) for first, second in ans]
    return '\n'.join([str(len(ans))] + ['{} {}'.format(f, s) for f, s in ans])

K = int(input())
X, Y = [int(i) for i in input().split()]
print(e_golf(K, X, Y))

Solution (Python):
import os
import sys
from operator import add

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7

K = int(sys.stdin.readline())
X, Y = list(map(int, sys.stdin.readline().split()))


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


def add(a, b):
    if a >= 0:
        return a + b
    else:
        return a - b


def sub(a, b):
    return add(a, -b)


def closer(x, y, k):
    assert mh(x, y) >= k
    
    if abs(x) >= k:
        x = sub(x, k)
    elif abs(y) >= k:
        y = sub(y, k)
    else:
        x = sub(x, k - abs(y))
        y = 0
    return x, y



rem = K - mh(X, Y) % K
if rem % 2 == 0:
    d = rem // 2
elif K % 2 == 1:
    d = (rem + K) // 2
else:
    print(-1)
    exit()
    assert False


x = X
y = Y
xy = [(x, y)]
if mh(x, y) % K != 0:
    while abs(x) + d < K and abs(y) + d < K:
        if K % 2 == 0:
            d += K // 2
        else:
            if abs(x) < abs(y):
                x = add(x, K)
            else:
                y = add(y, K)
            xy.append((x, y))

    if abs(x) + d >= K:
        y = add(y, d)
        x = sub(x, K - d)
        xy.append((x, y))
    elif abs(y) + d >= K:
        x = add(x, d)
        y = sub(y, K - d)
        xy.append((x, y))
    else:
        assert False

while x != 0 or y != 0:
    x, y = closer(x, y, K)
    xy.append((x, y))

x, y = xy[0]
xsign = 1 if x == X else -1
ysign = 1 if y == Y else -1

print(len(xy) - 1)
for x, y in reversed(xy[:-1]):
    print(x * xsign, y * ysign)

Solution (Python):
K = int(input())
def calc(a, b):
    if a < 0:
        t = calc(-a, b)
        return (-t[0], t[1])
    if b < 0:
        t = calc(a, -b)
        return (t[0], -t[1])
    if a > b:
        t = calc(b, a)
        return (t[1], t[0])
    if a + b >= 2*K:
        return (a, b-K)
    if a + b == K:
        return (0, 0)
    r = K - (a + b)//2
    return (-r, b-K+a+r)

X, Y = map(int, input().split())
if K % 2 == 0 and (X+Y) % 2:
    print(-1)
else:
    ANS = []
    while X or Y:
        ANS.append((X, Y))
        X, Y = calc(X, Y)
    print(len(ANS))
    for ans in ANS[::-1]:
        print(*ans)

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

ans = [(X,Y)]
x,y = ans[-1]
while abs(x)>=3*K//2:
    x,y = ans[-1]
    if x>=K:
        x -= K
    elif x<=-K:
        x += K
    ans.append((x,y))
dx = abs(x)

while abs(y)>2*K-dx:
    x,y = ans[-1]
    if y>0:
        y -= K
    else:
        y += K
    ans.append((x,y))

x,y = ans[-1]
if abs(y)+abs(x)!=K:
    if (x+y)%2==0 and K%2==0:
        d2 = (abs(x)+abs(y))//2
        if abs(x)>abs(y):
            x -= d2 * (1 if x>0 else -1)
            y += (K-d2) * (1 if y>0 else -1)
        else:
            x += (K-d2) * (1 if x>0 else -1)
            y -= d2 * (1 if y>0 else -1)
        ans.append((x,y))
    else:
        if (x+y+K)%2 == 0:
            y -= K if y>0 else (-K)
            ans.append((x,y))
        d = abs(x)+abs(y)
        if abs(x)>abs(y):
            x -= d//2 * (1 if x>0 else -1)
            y += (K-d//2) * (1 if y>0 else -1)
        else:
            x += (K-d//2) * (1 if x>0 else -1)
            y -= d//2 * (1 if y>0 else -1)
        ans.append((x,y))
x,y = ans[-1]

if abs(x)+abs(y)!=K:
    print(-1)
else:
    ans = ans[::-1]
    print(len(ans))
    for out in ans:
        print(*out)

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

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

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

ANS=[]
NOWX=0
NOWY=0

def dis(NOWX,NOWY,X,Y):
    return abs(NOWX-X)+abs(NOWY-Y)

def app():
    ANS.append((NOWX,NOWY))

if dis(NOWX,NOWY,X,Y)<K and dis(NOWX,NOWY,X,Y)%2==1:
    if X>0:
        NOWX-=K
        app()
    elif X<0:
        NOWX+=K
        app()
    elif Y>0:
        NOWY-=K
        app()
    elif Y<0:
        NOWY+=K
        app()
        

while dis(NOWX,NOWY,X,Y)>2*K:
    if X-NOWX>K:
        NOWX+=K
        app()
    elif NOWX-X>K:
        NOWX-=K
        app()
    elif Y-NOWY>K:
        NOWY+=K
        app()
    elif NOWY-Y>K:
        NOWY-=K
        app()



if dis(NOWX,NOWY,X,Y)!=K and dis(NOWX,NOWY,X,Y)%2==1:
    if X-NOWX>0:
        NOWX+=K
        app()
    elif NOWX-X>0:
        NOWX-=K
        app()
    elif Y-NOWY>0:
        NOWY+=K
        app()
    elif NOWY-Y>0:
        NOWY-=K
        app()



if dis(NOWX,NOWY,X,Y)==K:
    ANS.append((X,Y))

else:
    t=dis(NOWX,NOWY,X,Y)
    u=(K*2-t)//2

    if abs(NOWY-Y)<=abs(NOWX-X):

        if X<NOWX and Y>=NOWY:
                ANS.append((NOWX-(K-u),NOWY-u))
                ANS.append((X,Y))
        elif X<NOWX and Y<=NOWY:
                ANS.append((NOWX-(K-u),NOWY+u))
                ANS.append((X,Y))
                
        elif X>NOWX and Y>=NOWY:
                ANS.append((NOWX+(K-u),NOWY-u))
                ANS.append((X,Y))
        elif X>NOWX and Y<=NOWY:
                ANS.append((NOWX+(K-u),NOWY+u))
                ANS.append((X,Y))

    else:
        if X<=NOWX and Y>NOWY:
                ANS.append((NOWX+u,NOWY+(K-u)))
                ANS.append((X,Y))
        elif X<=NOWX and Y<NOWY:
                ANS.append((NOWX+u,NOWY-(K-u)))
                ANS.append((X,Y))
                
        elif X>=NOWX and Y>NOWY:
                ANS.append((NOWX-u,NOWY+(K-u)))
                ANS.append((X,Y))
        elif X>=NOWX and Y<NOWY:
                ANS.append((NOWX-u,NOWY-(K-u)))
                ANS.append((X,Y))
        
            
print(len(ANS))
for x,y in ANS:
    print(x,y)

Solution (Python):



import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline 

k = int(input())
x,y = [int(i) for i in readline().split()]

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

ans = [(x,y)]

while (abs(x)+abs(y) > 2*k):
    if x >= k: x -= k
    elif x <= -k: x += k
    else: 
        d = abs(x)
        x = 0
        if y > 0: y -= (k-d)
        else: y += (k-d)

    ans.append((x,y))        



if abs(x) + abs(y) == k: pass
else:
    if (x+y)%2:
        if x >= k: x -= k
        elif x <= -k: x += k
        else: 
            d = abs(x)
            x = 0
            if y > 0: y -= (k-d)
            else: y += (k-d)
        
        ans.append((x,y))        

        
    sx = 1 if x>0 else -1
    sy = 1 if y>0 else -1
    x,y = abs(x),abs(y)
    swap = 0
    if x > y:
        swap = 1
        x,y = y,x

    x,y= (x-y+2*k)//2, (y-x)//2   
    
    if swap: x,y=y,x
    ans.append((x*sx,y*sy))


print(len(ans))
for i in reversed(ans):
    print(*i)

Solution (Python):
i=input;k = int(i());x, y = map(int, i().split());x,y=x-y,x+y
def cc(x, xs):
    if abs(x-xs)<=2*k and abs(x-xs)%2==0:
        return 1,x+(k if abs(x+k-xs) <= abs(x-k-xs) else -k)
    return 0,xs+(k if x-xs>0 else -k)
if (k%2,x%2)==(0,1):
    print(-1)
else:
    rs = []
    xs, ys = 0, 0
    while True:
        if abs(x-xs) == k or abs(y-ys) == k:
            if abs(x-xs) <= k and abs(y-ys) <= k:
                rs+=["{} {}".format((x+y)//2,(y-x)//2)]
                break
        mm,ys=cc(y,ys)
        if mm:
            xs = xs+(k if x > xs else -k)
        else:
            mm, xs = cc(x, xs)
        rs+=["{} {}".format((xs+ys)//2,(ys-xs)//2)]
    print(len(rs))
    print("\n".join(rs))

Solution (Python):
k = int(input())
x, y = [int(x) for x in input().strip().split(" ")]
x, y = x-y, x+y

def main():
    if k % 2 == 0:
        if x % 2 != 0:
            return -1
    return to(x,y)

def cc(x, xs):
        if abs(x-xs) <= 2*k:
            if abs(x+k-xs) % 2 == k % 2:
                if abs(x+k-xs) <= abs(x-k-xs):
                    xs = x+k
                else:
                    xs = x-k
                return (True,xs)
            else:
                if x > xs:
                    xs += k
                else:
                    xs -= k
        else:
            if x > xs:
                xs += k
            else:
                xs -= k
        return (False,xs)


def to(x, y):
    rs = []
    xs, ys = 0, 0
    while True:
        if abs(x-xs) == k or abs(y-ys) == k:
            if abs(x-xs) <= k and abs(y-ys) <= k:
                return rs + [(x, y)]
        mm, ys = cc(y, ys)
        if mm:
            if x > xs:
                xs += k
            else:
                xs -= k
        else:
            mm, xs = cc(x, xs)
        rs.append((xs, ys))

if __name__ == '__main__':
    jj = main()
    if jj == -1:
        print(-1)
    else:
        print(len(jj))
        for r in jj:
            print((r[0]+r[1])//2, (r[1]-r[0])//2)

Solution (Python):
import sys

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  

k = ni()
x, y = na()

def go(x, y, k):
    if (x+y) % 2 != 0 and k % 2 == 0:
        return None
    if x < 0:
        res = go(-x, y, k)
        for u in res:
            u[0] = -u[0]
        return res
    if y < 0:
        res = go(x, -y, k)
        for u in res:
            u[1] = -u[1]
        return res

    ops = []
    while x+y >= 2*k and x >= k:
        ops.append([k, 0])
        x -= k
    while x + y >= 2 * k and y >= k:
        ops.append([0, k])
        y -= k
    if (x+y) % k == 0:
        while x >= k:
            ops.append([k, 0])
            x -= k
        while y >= k:
            ops.append([0, k])
            y -= k
        if x > 0 and y > 0:
            ops.append([x, y])
        return ops

    if (x+y) % 2 == 0:
        mid = (x + y) // 2
        if mid <= x:
            ops.append([mid, -(k-mid)])
            ops.append([x-mid, y+(k - mid)])
        else:
            ops.append([x+(k-mid), mid-x])
            ops.append([-(k-mid), y-(mid-x)])
        return ops

    if x >= k:
        ops.append([k, 0])
        res = go(x-k, y, k)
        ops.extend(res)
        return ops

    ops.append([x, k-x])
    res = go(0, y-(k-x), k)
    ops.extend(res)
    return ops

res = go(x, y, k)
if not res:
    print(-1)
    sys.exit(0)

print(len(res))
cx, cy = 0, 0
for o in res:
    cx += o[0]
    cy += o[1]
    print(cx, cy)

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

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

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

def strategy(x,y,K):
    if x < 0:
        dx, dy = strategy(-x,y,K)
        return -dx, dy
    if y < 0:
        dx, dy = strategy(x,-y,K)
        return dx,-dy
    if x > y:
        dx, dy = strategy(y,x,K)
        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 (x+y) % 2 == 0:
        
        
        t = (y - x) // 2
        dx,dy = K-t, t
        return dx,dy
    
    
    dx = min(K,x)
    dy = K - dx
    return dx,dy


x,y = 0,0
answer = []
while x != X or y != Y:
    dx,dy = strategy(X-x, Y-y, K)
    x += dx
    y += dy
    answer.append((x,y))

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

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

M = abs(X) + abs(Y)

if M % 2 == 1 and K % 2 == 0:
    r = -1
elif K % 2 == 0:
    if M < K:
        r = 2
    elif M % K == 0:
        r = M // K
    else:
        r = M // K + 1
else:
    if M < K:
        if M % 2 == 0:
            r = 2
        else:
            r = 3
    elif M % K == 0:
        r = M // K
    else:
        r = M // K
        if (M - r) % 2 == 1:
            r += 1
        else:
            r += 2

print(r)


def act(now_x, now_y, target_x, target_y):

    dist = abs(target_x - now_x) + abs(target_y - now_y)
    if target_x - now_x >= 0:
        sign_x = 1
    else:
        sign_x = -1
    if target_y - now_y >= 0:
        sign_y = 1
    else:
        sign_y = -1

    if K % 2 == 0:
        if dist < K:
            if abs(target_x - now_x) >= dist // 2:
                next_x = now_x + sign_x * dist // 2
                next_y = now_y - sign_y * (K - (dist // 2))
            else:
                next_y = now_y + sign_y * dist // 2
                next_x = now_x - sign_x * (K - (dist // 2))
        elif dist % K == 0:
            if abs(target_x - now_x) >= K:
                next_x = now_x + sign_x * K
                next_y = now_y
            else:
                next_y = now_y + sign_y * (K - abs(target_x - now_x))
                next_x = target_x
        else:
            waste_dist = (K - (dist % K)) // 2
            if abs(target_x - now_x) >= dist // 2:
                next_x = now_x + sign_x * (K - waste_dist)
                next_y = now_y - sign_y * waste_dist
            else:
                next_y = now_y + sign_y * (K - waste_dist)
                next_x = now_x - sign_x * waste_dist
    else:
        if dist < K:
            if dist % 2 == 0:
                if abs(target_x - now_x) >= dist // 2:
                    next_x = now_x + sign_x * dist // 2
                    next_y = now_y - sign_y * (K - (dist // 2))
                else:
                    next_y = now_y + sign_y * dist // 2
                    next_x = now_x - sign_x * (K - (dist // 2))
            else:
                next_x = now_x + K
                next_y = now_y
        elif dist % K == 0:
            if abs(target_x - now_x) >= K:
                next_x = now_x + sign_x * K
                next_y = now_y
            else:
                next_y = now_y + sign_y * (K - abs(target_x - now_x))
                next_x = target_x
        else:
            if dist % 2 == 0:
                waste_dist = (K - (dist % K)) // 2
                if abs(target_x - now_x) >= dist // 2:
                    next_x = now_x + sign_x * (K - waste_dist)
                    next_y = now_y - sign_y * waste_dist
                else:
                    next_y = now_y + sign_y * (K - waste_dist)
                    next_x = now_x - sign_x * waste_dist

            else:
                if abs(target_x - now_x) >= K:
                    next_x = now_x + sign_x * K
                    next_y = now_y
                else:
                    next_y = now_y + sign_y * (K - abs(target_x - now_x))
                    next_x = target_x

    
    return next_x, next_y


if r > 0:
    res_x = [0] * r
    res_y = [0] * r
    for i in range(r):
        x, y = act(x, y, X, Y)
        res_x[i] = x
        res_y[i] = y
    for i in range(r):
        print(str(res_x[i]) + " " + str(res_y[i]))