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())
gx,gy = map(int,input().split())
if k%2 == 0 and (gx+gy)%2:
  print(-1)
  exit()
flgx = 1
flgy = 1
if gx < 0:
  flgx -= 2
  gx *= -1
if gy < 0:
  flgy -= 2
  gy *= -1
px = 0
py = 0
ansls = []
while True:
  if (gx-px+gy-py) <= 2*k:
    break
  if gx-px >= k:
    px += k
  elif gy-py >= k:
    py += k
  ansls.append((px,py))
if gx-px+gy-py <= 2*k:
  if gx-px+gy-py == 2*k:
    if px+k <= gx:
      px += k
    else:
      py += k
    ansls.append((px,py))
  if gx-px+gy-py == k:
    ansls.append((gx,gy))
  else:
    if (gx-px+gy-py)%2:
      if px+k <= gx:
        px += k
      elif py+k <= gy:
        py += k
      elif px+py+k <= gx+gy:
        py += k-(gx-px)
        px = gx
      else:
        py -= gx-px
        px -= k-(gx-px)
      ansls.append((px,py))
    if gx-px <= gy-py:
      ansls.append((px+(gx-px+gy-py)//2-k,py+(gx-px+gy-py)//2))
    else:
      ansls.append((px+(gx-px+gy-py)//2,py+(gx-px+gy-py)//2-k))
    ansls.append((gx,gy))
for i in range(len(ansls)-1):
  if abs(ansls[i+1][0]-ansls[i][0])+abs(ansls[i+1][1]-ansls[i][1]) != k:
    print("error")
print(len(ansls))
for x,y in ansls:
  print(x*flgx,y*flgy)

Solution (Python):
'''
分かりやすい図解ページ
https://blog.bobuhiro11.net/2019/08-03-abc135ef.html
https://betrue12.hateblo.jp/entry/2019/07/28/215024
'''
import math

def swap(X,Y):
  temp=X
  X=Y
  Y=temp
  return X,Y

def keiro(count,X,Y,K):
  keirolist=[]
  oneway_surplus=int(((count*K)-X-Y)/2)
  move=0
  turncount=0
  prevX=0
  prevY=0
  while(move<count):
    if turncount==0:
      if K-prevY>=oneway_surplus:
        prevX=K-oneway_surplus
        prevY-=oneway_surplus
        turncount+=1
      else:
        prevX=0
        prevY-=K
    elif turncount==1:
      if K+prevX>X:
        prevY+=(K-(X-prevX))
        prevX=X
        turncount+=1
      else:
        prevX+=K
    else:
      prevY+=K
    keirolist.append([prevX,prevY])
    
    move+=1
  return keirolist

def ans(K,X,Y):
  anslist=[]
  count=0
  Xminus=0
  Yminus=0
  swapflag=0
  if X<0:
    Xminus=1
    X=abs(X)
  if Y<0:
    Yminus=1
    Y=abs(Y)
  if (X+Y)==K:
    count=1
    anslist.append([X,Y])
  else:
    if X<Y:
      X,Y=swap(X,Y)
      swapflag=1
    if X+Y>K:
      count=math.ceil((X+Y)/K)
      lastflag=0
      if (count*K-X-Y)%2==1:
        count+=1
        lastflag=1
      anslist=keiro(count,X,Y,K)
      
    else:
      if (X+Y)%2==0:
        count=2
        anslist=keiro(count,X,Y,K)
      else:
        count=3
        anslist.append([X,-K+X])
        Xsurplus=int((X+K-Y)/2)
        anslist.append([X+Xsurplus,X-Xsurplus])
        anslist.append([X,Y])
  for ans in anslist:
    if swapflag==1:
      ans[0],ans[1]=swap(ans[0],ans[1])
    if Xminus==1:
      ans[0]=-ans[0]
    if Yminus==1:
      ans[1]=-ans[1]
  return count,anslist



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

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

count,anslist=ans(K,X,Y)
print(count)
for ans in anslist:
  print(ans[0],ans[1])

Solution (Python):
k=int(input())
x,y=map(int,input().split())
if x<0:fx=-1
else:fx=1
if y<0:fy=-1
else:fy=1
ax,ay=abs(x),abs(y)
if ax<ay:
  x,y=y,x
  ax,ay=ay,ax
  fx,fy=fy,fx
  flag=1
else:flag=0
r=ax+ay
if k%2==0 and r%2==1:print(-1);exit()
if r==k:
  print(1)
  if flag:x,y=y,x
  print(x,y)
  exit()
if r%2==1 and r<k:
  print(3)
  anss=[]
  anss.append(((ax+(k-ay))//2,(k-(ax+(k-ay))//2)))
  anss.append((ax+(k-ay),0))
  anss.append((ax,ay))
  for i,j in anss:
    ii,jj=i*fx,j*fy
    if flag:ii,jj=jj,ii
    print(ii,jj)
  exit()
if r%2==0 and r<k:
  rm=(2*k-ax-ay)//2
else:
  rn=(k-(r%k))%k
  if rn%2==1:rn+=k
  rm=rn//2
ans=(r+rm*2)//k
anss=[]*ans
print(ans)
first=[0,-rm]
second=[ax,-rm]
third=[ax,ay]
xx=yy=0
while yy>first[1]:
  if yy-k<=first[1]:
    xx+=k-(yy-first[1])
    yy=first[1]
  else:
    yy-=k
  anss.append((xx*fx,yy*fy))
while xx<second[0]:
  if xx+k>=second[0]:
    yy+=k-(second[0]-xx)
    xx=second[0]
  else:
    xx+=k
  anss.append((xx*fx,yy*fy))
while yy!=third[1]:
  yy+=k
  anss.append((xx*fx,yy*fy))
for i,j in anss:
  ii,jj=i,j
  if flag:ii,jj=jj,ii
  print(ii,jj)

Solution (Python):
k=int(input())
x,y=map(int,input().split())
if x<0:fx=-1
else:fx=1
if y<0:fy=-1
else:fy=1
ax,ay=abs(x),abs(y)
if ax<ay:
  x,y=y,x
  ax,ay=ay,ax
  fx,fy=fy,fx
  flag=1
else:flag=0
r=ax+ay
if k%2==0 and r%2==1:print(-1);exit()
if r==k:
  print(1)
  if flag:x,y=y,x
  print(x,y)
  exit()
if r%2==1 and r<k:
  print(3)
  anss=[]
  anss.append(((ax+(k-ay))//2,(k-(ax+(k-ay))//2)))
  anss.append((ax+(k-ay),0))
  anss.append((ax,ay))
  for i,j in anss:
    ii,jj=i*fx,j*fy
    if flag:ii,jj=jj,ii
    print(ii,jj)
  exit()
if r%2==0 and r<k:
  rm=(2*k-ax-ay)//2
else:
  rn=(k-(r%k))%k
  if rn%2==1:rn+=k
  rm=rn//2
ans=(r+rm*2)//k
anss=[]*ans
print(ans)
first=[0,-rm]
second=[ax,-rm]
third=[ax,ay]
xx=yy=0
while yy>first[1]:
  if yy-k<=first[1]:
    xx+=k-(yy-first[1])
    yy=first[1]
  else:
    yy-=k
  anss.append((xx*fx,yy*fy))
while xx<second[0]:
  if xx+k>=second[0]:
    yy+=k-(second[0]-xx)
    xx=second[0]
  else:
    xx+=k
  anss.append((xx*fx,yy*fy))
while yy!=third[1]:
  yy+=k
  anss.append((xx*fx,yy*fy))
for i,j in anss:
  ii,jj=i,j
  if flag:ii,jj=jj,ii
  print(ii,jj)

Solution (Python):
k = int(input())
x,y = map(int,input().split())
if abs(x)+abs(y) == k:
    print(1)
    print(x,y)
    exit()
cx = cy = cxy = False
if x < 0:
    x = -x
    cx = True
if y < 0:
    y = -y
    cy = True
if x < y:
    x,y =y,x
    cxy = True

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

ans = []
n = max((x+y+k-1)//k,2)

if (x+y)%2 != (n*k)%2:
    n += 1

if x < k and n==3:
    mid = (k+x-y)//2
    ans.append([x,x-k])
    ans.append([x+mid,y-k+mid])
    ans.append([x,y])

else:
    over = (n*k-x-y)//2
    for i in range(1,n+1):
        d = i*k
        if d <= y+over:
            ans.append([0,d])
        elif d <= y+over+x:
            ans.append([d-over-y,y+over])
        else:
            ans.append([x,y+(n-i)*k])

if cxy:
    ans = [[i,j] for j,i in ans]
if cx:
    ans = [[-i,j] for i,j in ans]
if cy:
    ans = [[i,-j] for i,j in ans]


print(n)
for i,j in ans:
    print(i,j)

Solution (Python):











import sys
input=sys.stdin.readline
from math import floor,ceil,sqrt,factorial,log 
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
inf=float('inf')
mod = 10**9+7
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    global X,Y,now_X,now_Y
    K=I()
    X,Y=MI()
    ans = []
    now_X=0
    now_Y=0
    
    def move_greedy():
        global X,Y,now_X,now_Y
        move = min(K,abs(now_X-X))
        if(X-now_X<0):
            now_X -= move
        else:
            now_X += move
        if(Y-now_Y<0):
            now_Y -= K-move
        else:
            now_Y += K-move
        ans.append((now_X,now_Y))
    
    def move_last():
        global X,Y,now_X,now_Y
        distance = abs(X-now_X) + abs(Y-now_Y)
        move = (2*K-distance)//2 
        if(abs(X-now_X) < abs(Y-now_Y)):
            if(X-now_X<0):
                now_X += move
            else:
                now_X -= move
            if(Y-now_Y<0):
                now_Y -= K - move
            else:
                now_Y += K - move
        else:
            if(Y-now_Y<0):
                now_Y += move
            else:
                now_Y -= move
            if(X-now_X<0):
                now_X -= K - move
            else:
                now_X += K - move
        ans.append((now_X,now_Y))
        ans.append((X,Y))
    
    
    if (abs(X) + abs(Y)) % 2 == 1 and K %2 == 0:
        print(-1)
        return
    while abs(now_X-X) + abs(now_Y-Y) > 2*K:
        move_greedy()

    if(K%2==0):
        move_last()
    else:
        if(abs(X-now_X) + abs(Y-now_Y) == K):
            ans.append((X,Y))
        else:
            distance = abs(X-now_X) + abs(Y-now_Y)
            if(distance&1):
                move_greedy()
            move_last()
            
    print(len(ans))
    for a in ans:
        print(*a)


    
if __name__ == '__main__':
    main()

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

def sign(x):
    return 1 if x > 0 else -1

if (abs(x) + abs(y)) % 2 == 1 and k % 2 == 0:
    print(-1)
else:
    cx, cy = 0, 0
    ret = []
    while x != cx or y != cy:
        if abs(x - cx) + abs(y - cy) == k:
            cx, cy = x, y
        elif abs(x - cx) + abs(y - cy) <= 2 * k:
            dx = abs(x - cx)
            dy = abs(y - cy)
            rem = 2 * k - dx - dy
            if dx > dy:
                cy += (dy + rem // 2) * sign(y - cy)
                cx += (k - dy - rem // 2) * sign(x - cx)
            else:
                cx += (dx + rem // 2) * sign(x - cx)
                cy += (k - dx - rem // 2) * sign(y - cy)
        else:
            if abs(x - cx) > abs(y - cy):
                cx += sign(x - cx) * k
            else:
                cy += sign(y - cy) * k
        ret.append((cx, cy))

    print(len(ret))
    for cx, cy in ret:
        print(cx, cy)

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

    def solve(ans, xflg, yflg, xyflg):
        print(len(ans) - 1)
        for x, y in ans[1:]:
            if xyflg:
                x, y = y, x
            if xflg:
                x *= -1
            if yflg:
                y *= -1
            print(x, y)
        exit()

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

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

    xflg = 0
    yflg = 0
    xyflg = 0
    if X < 0:
        xflg = 1
        X *= -1
    if Y < 0:
        yflg = 1
        Y *= -1
    if X < Y:
        xyflg = 1
        X, Y = Y, X

    ans = [(0, 0)]
    x = y = 0
    while abs(X - x) + abs(Y - y) >= K:
        if y < Y:
            y_new = min(Y, y + K)
            x_new = x + (K - y_new + y)
        else:
            y_new = y
            x_new = x + K
        x, y = x_new, y_new
        ans.append((x, y))

    if ans[-1][0] == X and ans[-1][1] == Y:
        solve(ans, xflg, yflg, xyflg)

    x_last, y_last = ans[-1]
    if (x_last+y_last)%2 != (X+Y)%2 or K%2 == 0:
        ans.pop()
        if ans:
            x_last, y_last = ans[-1]
        else:
            if K&1:
                x_last, y_last = -K, 0
                ans.append((0, 0))
                ans.append((-K, 0))
            else:
                x_last, y_last = 0, 0
                ans.append((0, 0))
    A = X - x_last
    B = Y - y_last
    ans.append((x_last + (A+B) // 2, y_last - K + (A+B) // 2))
    ans.append((X, Y))
    solve(ans, xflg, yflg, xyflg)



if __name__ == '__main__':
    main()

Solution (Python):
import sys
sys.setrecursionlimit(300000)
def solve(x,y,k):
    if x==y==0: return []
    if x<0: return [(-x,y) for x,y in solve(-x,y,k)]
    if y<0: return [(x,-y) for x,y in solve(x,-y,k)]
    if x>y: return [(x,y) for y,x in solve(y,x,k)]
    if (x+y)%k==0: return [(k,0)]*(x//k)+[(0,k)]*(y//k)+([]if x%k==y%k==0 else[(x%k,y%k)])
    if x+y>=2*k:
        if x%k+y%k>=k: return [(k,0)]*(x//k)+[(0,k)]*(y//k)+solve(x%k,y%k,k)
        else: return ([(k,0)]*(x//k)+[(0,k)]*(y//k))[:-1]+solve(x%k,y%k+k,k)
    if x+y<K and (x+y)%2==1: return solve(x,y+k,k)+[(0,-k)]
    if K&1 and x+y>k and (x+y)%k%2==0: return solve(x,y-k,k)+[(0,k)]
    z=(x+y)//2
    return [(k-z+x,z-x),(z-k,z)]
K,X,Y=map(int, open(0).read().split())
if K%2==0 and (X+Y)%2==1:
    print(-1)
else:
    ans=solve(X,Y,K)
    print(len(ans))
    ax,ay=0,0
    for x,y in ans:
        ax+=x
        ay+=y
        print(ax,ay)

Solution (Python):

import sys, math, itertools, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
inf = float('inf') ;mod = 10**9+7
mans = inf ;ans = [] ;count = 0 ;pro = 1

def ceil(x,y):
  return (x-1)//y+1

def redef(x,y):
  global swapsign
  if x < 0:
    x = -x; sign[0] = -1
  if y < 0:
    y = -y; sign[1] = -1
  if x < y:
    swapsign = 1; x,y = y,x
  return x,y

def return_case(k,D):
  if (k % 2 == 0) and (D % 2 == 1):
    print(-1); exit()
  elif (D % k == 0):
    case = -1
  elif (k % 2 == 0) and (D % 2 == 0):
    case = 0
  elif (k % 2 == 1) and (D % 2 == 1):
    case = 1
  elif (k % 2 == 1) and (D % 2 == 0):
    case = 2
  return case

def How_manytime(case,k,D):
  if case == -1:
    m = D//k
  elif case == 0:
    m=max(2,ceil(D,k))
  elif case == 1:
    m =max(3, ceil(D,k) + ((ceil(D,k) + 1) % 2) )
  elif case == 2:
    m = max(2, ceil(D,k)+ (ceil(D,k) % 2) )
  return m

def move(dx,dy):
  global x,y
  x += dx; y += dy
  ans.append([x,y])
  return

k=int(input())
X,Y=map(int,input().split())
sign = [1]*2
swapsign = 0
X,Y=redef(X,Y)
D=X+Y
case = return_case(k,D)
x = y = 0
m = How_manytime(case,k,D)

l = (k*m-D)//2

while k <= l:
  move(-k,0); l-=k
if l != 0:
  move(k-l,-l)

while (x,y) != (X,Y):
  s = min(k, X - x)
  move(s,k-s)

print(len(ans))
for x,y in ans:
  if swapsign: x,y = y,x
  x*=sign[0]; y*=sign[1]
  print(x,y)

Solution (Python):

import sys, math, itertools, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
inf = float('inf') ;mod = 10**9+7
mans = inf ;ans = [] ;count = 0 ;pro = 1
def ceil(x,y):
  return (x-1)//y+1
k=int(input())
X,Y=map(int,input().split())
sign = [1]*2
swapsign = 0
def redef(x,y):
  global swapsign
  if x < 0:
    x = -x; sign[0] = -1
  if y < 0:
    y = -y; sign[1] = -1
  if x < y:
    swapsign = 1; x,y = y,x
  return x,y
X,Y=redef(X,Y)
D=X+Y

if (k % 2 == 0) and (D % 2 == 1):
  print(-1); exit()
elif k % 2 == D % 2:
  case = 0
elif (k % 2 == 1) and (D % 2 == 0):
  case = 1
x = y = 0
if case == 0:
  m=ceil(D,k)
  if k % 2 == 1 and D % 2 == 1 and D % k != 0:
    m += (ceil(D,k)+1) % 2
    m = max (m,3)
  elif D % k !=0:
    m = max(m,2)
else:
  T = ceil(D,k)%2
  m = ceil(D,k)+T
  
  

l = (k*m-D)//2
while k <= l:
  x-=k; l-=k
  ans.append([x,y])
if l != 0:
  x+=k-l; y -= l;
  ans.append([x,y])




while (x,y) != (X,Y):
  s = min(k, X - x)
  x += s; y += k - s
  ans.append([x,y])
  
  
  

print(len(ans))
for x,y in ans:
  if swapsign: x,y = y,x
  x*=sign[0]; y*=sign[1]
  print(x,y)

Solution (Python):
def solve(x, y):
    if x < 0:
        x, y = solve(-x, y)
        return -x, y
    if y < 0:
        x, y = solve(x, -y)
        return x, -y
    if x > y:
        x, y = solve(y, x)
        return y, x

    if x + y >= 2 * K:
        return x, y - K
    if x + y == K:
        return 0, 0

    r = K - (x + y) // 2
    return -r, (x + y + r - K)



K, X, Y = map(int, open(0).read().split())

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

A = []
while X or Y:
    A.append((X, Y))
    X, Y = solve(X, Y)

print(len(A))
for a in reversed(A):
    print(*a)

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

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

def f(x,y):
    if x<0:
        a,b=f(-x,y)
        return -a,b
    if y<0:
        a,b=f(x,-y)
        return a,-b
    if x<y:
        a,b=f(y,x)
        return b,a
    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

p,q=0,0
ans=[]
while (p,q)!=(x,y):
    a,b=f(x-p,y-q)
    p+=a
    q+=b
    ans.append(str(p)+" "+str(q))

print(len(ans))
print('\n'.join(ans))

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

def dist(x,y,z=X,w=Y):
    return abs(z-x)+abs(w-y)

def shot(x,y):
    k = K
    if abs(Y-y)<=K:
        k -= abs(Y-y)
        y = Y
    elif Y>y:
        y += K
        k -= K
    else:
        y -= K
        k -= K
    if k>0:
        if X>x:
            x += k
        else:
            x -= k
    return x,y

x = 0
y = 0
ans = []
if (x+X+y+Y)%2==1 and K%2==0:
    print(-1)
else:
    while x!=X or y!=Y:
        if dist(x,y)==K:
            ans.append([X,Y])
            x,y = X,Y
        elif dist(x,y) > 2*K:
            x,y = shot(x,y)
            ans.append([x,y])
        else:
            if (x+X+y+Y)%2==0:
                cand = [[(x+X+y-Y)//2+K,(x-X+y+Y)//2],[(x+X+y-Y)//2-K,(x-X+y+Y)//2],[(x+X+y-Y)//2,(x-X+y+Y)//2+K],[(x+X+y-Y)//2,(x-X+y+Y)//2-K],
                [(X+x+Y-y)//2+K,(X-x+Y+y)//2],[(X+x+Y-y)//2-K,(X-x+Y+y)//2],[(X+x+Y-y)//2,(X-x+Y+y)//2+K],[(X+x+Y-y)//2,(X-x+Y+y)//2-K]]
                for a,b in cand:
                    if dist(a,b,x,y)==K and dist(a,b)==K:
                        ans.append([a,b])
                        ans.append([X,Y])
                        x,y = X,Y
                        break
            else:
                x,y = shot(x,y)
                ans.append([x,y])
    print(len(ans))
    for x,y in ans:
        print(x,y)

Solution (Python):
def main():
    k=int(input())
    x,y=map(int,input().split())

    def calc(x,y,k):
        x_sign,y_sign,xy_swap=1,1,0
        if x<0:
            x_sign=-1
            x*=-1
        if y<0:
            y_sign=-1
            y*=-1
        if x<y:
            xy_swap=1
            x,y=y,x
        ans=[]
        d=x+y
        cnt=d//k
        
        def pattern_0(x,y):
            x_now,y_now=0,0
            ans=[]
            cnt=(x+y)//k
            j=cnt
            for i in range(cnt):
                if x_now+k<x:
                    x_now+=k
                    ans.append([x_now,y_now])
                else:
                    y_now=k-x+x_now
                    x_now=x
                    ans.append([x_now,y_now])
                    j=i+1
                    break
            return ans+[[x,y_now+(i-j+1)*k] for i in range(j,cnt)]

        
        
        def pattern_1_even(x,y):
            return [[(x+y)//2,(x+y)//2-k],[x,y]]
        
        def pattern_1_odd(x,y):
            if k%2==0:
                return -1
            return [[k,0]]+[[-i+k,j] for i,j in pattern_1_even(k-x,y)]

        
        
        def pattern_2_even(x,y):
            cnt=(x+y)//k
            if k%2==0 or cnt%2==1:
                l=(k*(cnt+1)-(x+y))//2
            else:
                l=(k*(cnt+2)-(x+y))//2
            if cnt!=1:
                temp=[min(2*(k-l),x),max(2*(k-l)-x,0)]
                return [[k-l,-l],temp]+[[i+temp[0],j+temp[1]] for i,j in pattern_0(x-temp[0],y-temp[1])]
            else:
                return [[k-l,-l],[x,y]]
            
        
        def pattern_2_odd(x,y):
            cnt=(x+y)//k
            if k%2==0:
                return -1
            else:
                if (x-k+y)<k:
                    if x-k<y:
                        return [[k,0]]+[[j+k,i] for i,j in pattern_1_even(y,x-k)]
                    else:
                        return [[k,0]]+[[i+k,j] for i,j in pattern_1_even(x-k,y)]
                if x-k<y:
                    return [[k,0]]+[[j+k,i] for i,j in pattern_2_even(y,x-k)]
                else:
                    return [[k,0]]+[[i+k,j] for i,j in pattern_2_even(x-k,y)]
        if d%k==0:
            temp=pattern_0(x,y)
        elif cnt==0:
            if d%2==0:
                temp=pattern_1_even(x,y)
            else:
                temp=pattern_1_odd(x,y)
        else:
            if d%2==0:
                temp=pattern_2_even(x,y)
            else:
                temp=pattern_2_odd(x,y)
        if temp==-1:
            return -1
        if xy_swap:
            temp=[[j*x_sign,i*y_sign] for i,j in temp]
        else:
            temp=[[i*x_sign,j*y_sign] for i,j in temp]
        return temp

    def out(x,y,k):
        temp=calc(x,y,k)
        if temp==-1:
            print(-1)
            return 0
        print(len(temp))
        for i in temp:
            print(*i)

    out(x,y,k)
main()

Solution (Python):
import sys
K = int(input())
X, Y = map(int, input().split())
is_changed = False
if abs(X)<abs(Y):
    X, Y = Y, X
    is_changed = True
sgnX = 1 if X>=0 else -1
sgnY = 1 if Y>=0 else -1
X, Y = abs(X), abs(Y)
s = X+Y
r = s%K
n = s//K
def make(x, y, n):
    res = []
    for _ in range(x//K):
        res.append((x, y))
        x-=K
    res.append((x, y))
    y -= K-x%K
    x = 0
    for _ in range(y//K):
        res.append((x, y))
        y-=K
    return res
if r == 0:
    print(n)
    res = make(X, Y, n)
    if is_changed:
        for a, b in res[::-1]:
            print(b*sgnY, a*sgnX)
    else:
        for a, b in res[::-1]:
            print(a*sgnX, b*sgnY)
    sys.exit()
if n == 0:
    if s%2 == 0:
        print(2)
        m = K-s//2
        if is_changed:
            print(-m*sgnY, (K-m)*sgnX)
            print(Y*sgnY, X*sgnX)
        else:
            print((K-m)*sgnX, -m*sgnY)
            print(X*sgnX, Y*sgnY)
    else:
        if K%2 == 0:
            print(-1)
        else:
            print(3)
            m = (K*3-s)//2-K
            if is_changed:
                print(0, -K*sgnX)
                print(-m*sgnY, -m*sgnX)
                print(Y*sgnY, X*sgnX)
            else:
                print(-K*sgnX, 0)
                print(-m*sgnX, -m*sgnY)
                print(X*sgnX, Y*sgnY)

    sys.exit()

if s%2 == 0:
    if K%2 == 0:
        ans = n+1
        res = [(X, Y)]
        X-=(K+r)//2
        Y+=(K-r)//2
        res+=make(X, Y, n)
    else:
        if n%2 == 0:
            ans = n+2
            res = [(X, Y)]
            X-=r//2
            Y+=K-r//2
            res+=make(X, Y, n+1)
        else:
            ans = n+1
            res = [(X, Y)]
            X-=(K+r)//2
            Y+=(K-r)//2
            res+=make(X, Y, n)
else:
    if K%2 == 0:
        ans = -1
        res = []
    else:
        if n%2 == 0:
            ans = n+1
            res = [(X, Y)]
            X-=(K+r)//2
            Y+=(K-r)//2
            res+=make(X, Y, n)
        else:
            ans = n+2
            res = [(X, Y)]
            X-=r//2
            Y+=K-r//2
            res+=make(X, Y, n+1)
if is_changed:
    print(ans)
    for a, b in res[::-1]:
        print(b*sgnY, a*sgnX)
else:
    print(ans)
    for a, b in res[::-1]:
        print(a*sgnX, b*sgnY)

Solution (Python):
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
    from math import ceil
    K=int(input())
    X,Y=map(int,input().split())
    
    def trans(u,v):
        if(abs(X)<abs(Y)): u,v=v,u
        if(X<0): u=-u
        if(Y<0): v=-v
        return u,v

    x,y=max(abs(X),abs(Y)),min(abs(X),abs(Y))
    if(K%2==0 and (X+Y)&1):
        print(-1)
        return

    
    if(x+y<K and (x+y)&1):
        print(3)
        print(*trans(x,-K+x))
        print(*trans(x+(K+x-y)//2,-(K-x-y)//2))
        print(*trans(x,y))
        return
    if(x+y<K and (not (x+y)&1)):
        print(2)
        print(*trans((x+y)//2,-K+(x+y)//2))
        print(*trans(x,y))
        return

    
    if(x+y==K):
        print(1)
        print(*trans(x,y))
        return
    
    
    c=(-x-y)%K
    n=ceil((x+y)/K)+(c&1)
    l=(K*n-x-y)//2
    print(n)
    nx,ny=K-l,-l
    print(*trans(nx,ny))
    while(nx+K<=x):
        nx+=K
        print(*trans(nx,ny))
    nx,ny=x,ny+(K-(x-nx))
    print(*trans(nx,ny))
    while(ny+K<=y):
        ny+=K
        print(*trans(nx,ny))
resolve()

Solution (Python):

import sys

def get(K: int, X: int, Y: int):
    p = None
    if X >= K:
        X -= K
        p = [K, 0]
    elif Y >= K:
        Y -= K
        p = [0, K]
    else:
        if X + Y >= K:
            p = [X, K - X]
            Y -= K - X
            X = 0
        else:
            p = [-K, 0]
            X += K
    return p, X, Y

def jump_two(K: int, X: int, Y: int):
    assert(X + Y < 2 * K)
    if X + Y == 0:
        return []
    elif X + Y == K:
        return [(X, Y)]
    ret = []
    rev = False
    if X > Y:
        X, Y = Y, X
        rev = True
    a = K - (Y - X)//2
    b = K - (Y + X)//2
    assert(0 <= a and a <= K)
    assert(0 <= b and b <= K)
    ret.append([a, K - a])
    ret.append([-b, K - b])
    if rev:
        for i in range(len(ret)):
            ret[i][0], ret[i][1] = ret[i][1], ret[i][0]
    return ret

def calc(K: int, X: int, Y: int):
    ret = []
    if X + Y < K:
        if (X + Y) % 2 != 0:
            
            p, X, Y = get(K, X, Y)
            ret.append(p)
        ret += jump_two(K, X, Y)
        return ret
    while X + Y >= K * 2:
        p, X, Y = get(K, X, Y)
        ret.append(p)
    if X + Y == K:
        
        ret.append([X, Y])
    else:
        
        if (X + Y) % 2 != 0:
            p, X, Y = get(K, X, Y)
            ret.append(p)

        ret += jump_two(K, X, Y)
    return ret


def solve(K: int, X: int, Y: int):
    if K % 2 == 0 and (X + Y) % 2 != 0:
        print(-1)
        return
    negate_X = False
    negate_Y = False
    if X < 0:
        X *= -1
        negate_X = True
    if Y < 0:
        Y *= -1
        negate_Y = True
    v = calc(K, X, Y)
    for i in range(len(v)):
        if negate_X:
            v[i][0] *= -1
        if negate_Y:
            v[i][1] *= -1
    x = 0
    y = 0
    print(len(v))
    for dx, dy in v:
        x += dx
        y += dy
        print(x, y)
    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    K = int(next(tokens))  
    X = int(next(tokens))  
    Y = int(next(tokens))  
    solve(K, X, Y)

if __name__ == '__main__':
    main()

Solution (Python):

import random

def copysign(x, y):
    return abs(x) * (1 if y >= 0 else - 1)

def dist(k, x, y):
    if not (abs(x) + abs(y) < 2 * k):
        return None
    if x == y == 0:
        return 0
    elif abs(x) + abs(y) == k:
        return 1
    elif (abs(x) + abs(y)) % 2 == 0:
        return 2
    else:
        return 3

def iterate_neighbors(k, x, y):
    assert abs(x) + abs(y) < 2 * k
    dz = (2 * k - abs(x) - abs(y)) // 2
    for dx in (dz, k - dz):
        dy = k - dx
        yield (dx, dy)
        yield (dx, - dy)
        yield (- dx, dy)
        yield (- dx, - dy)

def solve(k, x, y):
    while True:
        d = dist(k, x, y)
        if d == 0:
            return
        elif d == 1:
            dx = x
            dy = y
        elif d == 2:
            for dx, dy in iterate_neighbors(k, x, y):
                if dist(k, x - dx, y - dy) == 1:
                    break
            else:
                assert False
        elif d == 3:
            while True:
                dx = random.randint(0, k) * random.choice([-1, 1])
                dy = (k - abs(dx)) * random.choice([-1, 1])
                if dist(k, x - dx, y - dy) == 2:
                    break
        else:
            dx = copysign(min(k, abs(x)), x)
            dy = copysign(k - abs(dx), y)
        yield (dx, dy)
        x -= dx
        y -= dy

def main():
    k = int(input())
    x, y = map(int, input().split())
    if k % 2 == 0 and (abs(x) + abs(y)) % 2 != 0:
        print(-1)
        return
    ops = list(solve(k, x, y))
    print(len(ops))
    x = 0
    y = 0
    for dx, dy in ops:
        x += dx
        y += dy
        print(x, y)

main()

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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

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

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
answer = []
while (x,y) != (X,Y):
    dx,dy = next_vec(X-x,Y-y)
    x += dx; y += dy
    answer.append('{} {}'.format(x,y))

print(len(answer))
print('\n'.join(answer))

Solution (Python):
import sys

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

mx = 1 if X >= 0 else -1
my = 1 if Y >= 0 else -1
X, Y = abs(X), abs(Y)
isSwaped = False
if X < Y:
    X, Y = Y, X
    isSwaped = True

dist = X+Y
if dist%K == 0:
    rx, ry = 0, 0
else:
    if dist%2 == 0:
        if K%2 == 0:
            if dist <= 2*K:
                R = 2*K
            else:
                R = -(-dist // K) * K
        else:
            R = -(-dist // (2*K)) * 2*K
    else:
        if K%2 == 0:
            print(-1)
            sys.exit()
        else:
            if dist <= 3*K:
                R = 3*K
            else:
                R = -(-(dist-K) // (2*K)) * 2*K + K
    r = (R-dist) // 2

    if r < K:
        rx, ry = 0, r
    else:
        rx = r//2
        ry = r-rx

anss = []
x, y = 0, 0
if -ry < y:
    dy = y+ry
    y -= dy
    x += K-dy
    anss.append((x, y))

num = (X+rx-x) // K
for _ in range(num):
    x += K
    anss.append((x, y))

if x < X+rx:
    dx = X+rx-x
    x += dx
    y += K-dx
    anss.append((x, y))

num = (Y-y) // K
for _ in range(num):
    y += K
    anss.append((x, y))

if y < Y:
    dy = Y-y
    y += dy
    x -= K-dy
    anss.append((x, y))

print(len(anss))
if isSwaped:
    print('\n'.join(map(lambda x: str(x[1]*mx)+' '+str(x[0]*my), anss)))
else:
    print('\n'.join(map(lambda x: str(x[0]*mx)+' '+str(x[1]*my), anss)))

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>=2*k:
		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):
K = int(input())
def calc(a, b):
    if abs(X-a)+abs(Y-b) >= 2*K:
        if abs(X-a) >= K:
            r1 = K if X > a else -K
        else:
            r1 = abs(X-a) * (1 if X > a else -1)
        r2 = (K-abs(r1)) * (1 if Y > b else -1)
    elif abs(X-a)+abs(Y-b) == K:
        r1, r2 = X-a, Y-b
    else:
        rr = 2*K - abs(X-a)-abs(Y-b)
        rr = 0 if rr % 2 else rr//2
        if abs(X-a) < abs(Y-b):
            r1 = (abs(X-a)+rr) * (1 if X > a else -1)
            r2 = (K-abs(r1)) * (1 if Y > b else -1)
        else:
            r2 = (abs(Y-b)+rr) * (1 if Y > b else -1)
            r1 = (K-abs(r2)) * (1 if X > a else -1)
    return (a+r1, b+r2)

x, y = 0, 0
X, Y = map(int, input().split())
if K % 2 == 0 and (X+Y) % 2:
    print(-1)
else:
    ANS = []
    while x != X or y != Y:
        x, y = calc(x, y)
        ANS.append((x, y))

    print(len(ANS))
    for ans in ANS:
        print(*ans)