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 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()
    
    
    
    
    

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:
            if abs(nx-X)+abs(ny-Y)==K:
                ans.append((X,Y))
                break
            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):
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()
    
    
    
    
    

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:
            if abs(nx-X)+abs(ny-Y)==K:
                ans.append((X,Y))
                break
            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 solve(k, x, y):
    if k % 2 == 0 and (x + y) % 2 == 1:
        print(-1)
        return

    ans = []
    abx, aby = abs(x), abs(y)
    mh = abx + aby
    xx, yy = 0, 0

    if mh % k == 0:
        d = (mh - 1) // k + 1
        for _ in range(d - 1):
            dx, dy = abs(abx - xx), abs(aby - yy)
            if dx > 0:
                if dx >= k:
                    xx += k
                else:
                    xx += dx
                    yy += k - dx
            else:
                yy += k
            ans.append((xx, yy))
        ans.append((abx, aby))
        print_ans(ans, x, y)
        return

    d = (mh - 1) // k + 1

    if k % 2 == 1 and mh % 2 != max(2, d) % 2:
        if abx > aby:
            xx += min(k, abx)
            yy += k - min(k, abx)
        else:
            yy += min(k, aby)
            xx += k - min(k, aby)
        ans.append((xx, yy))

    for _ in range(d - 2):
        dx, dy = abs(abx - xx), abs(aby - yy)
        if dx > dy:
            if abx > xx:
                xx += k
            else:
                xx -= k
        else:
            if aby > yy:
                yy += k
            else:
                yy -= k
        ans.append((xx, yy))

    dx, dy = abs(abx - xx), abs(aby - yy)
    mhh = (dx + dy) // 2
    if dx > dy:
        if abx > xx:
            xx += mhh
        else:
            xx -= mhh
        if aby > yy:
            yy -= k - mhh
        else:
            yy += k - mhh
    else:
        if aby > yy:
            yy += mhh
        else:
            yy -= mhh
        if abx > xx:
            xx -= k - mhh
        else:
            xx += k - mhh
    ans.append((xx, yy))
    ans.append((abx, aby))

    print_ans(ans, x, y)


def print_ans(ans, x, y):
    print(len(ans))
    for dx, dy in ans:
        if x < 0:
            dx = -dx
        if y < 0:
            dy = -dy
        print(dx, dy)


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

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):
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)

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

nowx, nowy = 0, 0
ans = []

def ok():
    global ans, x, y
    ans.append((x, y))
    print(len(ans))
    for (x, y) in ans:
        print(x,y)
    exit(0)
        


while(abs(nowx - x) + abs(nowy - y) >= 2*K):
    if x - nowx >= K:
        nowx += K
    elif nowx - x >= K:
        nowx -= K
    elif y - nowy >= K:
        nowy += K
    else:
        nowy -= K
    ans.append((nowx, nowy))

if (abs(nowx - x) + abs(nowy - y)) % 2 == 1 and K % 2 == 0:
    print(-1)
    exit(0)

    
if (abs(nowx - x) + abs(nowy - y)) == K:ok()
    
if (abs(nowx - x) + abs(nowy - y)) % 2 == 1:
    if abs(nowx+K - x) + abs(nowy - y) < 2*K:nowx += K
    elif abs(nowx-K - x) + abs(nowy - y) < 2*K:nowx -= K
    elif abs(nowx - x) + abs(nowy+K - y) < 2*K:nowy += K
    elif abs(nowx - x) + abs(nowy-K - y) < 2*K:nowy -= K
    ans.append((nowx, nowy))
    
if (abs(nowx - x) + abs(nowy - y)) == K:ok()

rest = K*2 - (abs(nowx-x) + abs(nowy-y))
rest //= 2
xgo, ygo = 1, 1

if nowx != x:
    xgo = (x - nowx) // abs(nowx - x)
if nowy != y:
    ygo = (y - nowy) // abs(nowy - y)

if abs(nowx-x) >= abs(nowy-y):
    nowx += xgo * (K - rest)
    nowy -= ygo * rest
    ans.append((nowx, nowy))
    
else:
    nowy += ygo * (K - rest)
    nowx -= xgo * rest
    ans.append((nowx, nowy))

ok()