Problem:


Score : 300 points

Problem Statement

Takahashi, who lives on the number line, is now at coordinate X. He will make exactly K moves of distance D in the positive or negative direction.

More specifically, in one move, he can go from coordinate x to x + D or x - D.

He wants to make K moves so that the absolute value of the coordinate of the destination will be the smallest possible.

Find the minimum possible absolute value of the coordinate of the destination.

Constraints

-10^{15} \leq X \leq 10^{15}

1 \leq K \leq 10^{15}

1 \leq D \leq 10^{15}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X K D

Output

Print the minimum possible absolute value of the coordinate of the destination.

Sample Input 1

6 2 4

Sample Output 1

2

Takahashi is now at coordinate 6. It is optimal to make the following moves:

Move from coordinate 6 to (6 - 4 =) 2.

Move from coordinate 2 to (2 - 4 =) -2.

Here, the absolute value of the coordinate of the destination is 2, and we cannot make it smaller.

Sample Input 2

7 4 3

Sample Output 2

1

Takahashi is now at coordinate 7. It is optimal to make, for example, the following moves:

Move from coordinate 7 to 4.

Move from coordinate 4 to 7.

Move from coordinate 7 to 4.

Move from coordinate 4 to 1.

Here, the absolute value of the coordinate of the destination is 1, and we cannot make it smaller.

Sample Input 3

10 1 2

Sample Output 3

8

Sample Input 4

1000000000000000 1000000000000000 1000000000000000

Sample Output 4

1000000000000000

The answer can be enormous.



Sample input:
6 2 4


Sample output:
2

Submission status: Wrong Answer

Solution code:
x, k, d = map(int, input().split())
ans = x
if x//d>=k:
    ans-= (k*d)
else:
    ans-=(x//d)*d
    k-=x//d
    #print(k, ans, (x//d)*d)
    if k%2==1:
        ans=min(ans+d, abs(ans-d))
print(abs(ans))

Submission status: Wrong Answer

Solution code:
x,k,d = list(map(int, input().split(' ')))

if k*d<=x:
    print(x-k*d)
elif -k*d>=x:
    print(x+k*d)
else:
    rem = x%(d)
    div = x//d
    if (k-div)%2==0:
        print(rem)
    else:
        print(d-rem)

Submission status: Wrong Answer

Solution code:
c,K,M=list(map(int,input().split()))
d=1000000000000000000000000000000000
for i in range(K):
  c=abs(c-M)
  if c<d:
    d=c
    if d<M and (K+1-i)%2==0:
      print(d)
      exit()
    elif d<M and (K+1-i)%2==1:
      print(min(abs(d+M),abs(d-M)))
      exit()
print(d)

Submission status: Wrong Answer

Solution code:
# import numpy as np
# import math
# import copy
# from collections import deque
import sys
input = sys.stdin.readline
# sys.setrecursionlimit(10000)


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

    bestp = X % D
    bestm = bestp - D

    ablep = False

    if abs(X-bestp)//D <= K:
        ablep = True

    ablem = False

    if abs(X-bestm)//D <= K:
        ablem = True

    if (not ablep) & (not ablem):
        res = min(abs(X+K*D),abs(X-K*D))
        print(res)
        sys.exit()

    res = []

    if ablep:
        if X >= bestp:
            resid = (X - bestp) // D
        else:
            resid = (bestp - X) // D
        if resid%2 == 0:
            res.append(abs(bestp))
        else:
            res.append(abs(bestp-D))
            res.append(abs(bestp+D))

    if ablem:
        if X >= bestm:
            resid = (X - bestm) // D
        else:
            resid = (bestm - X) // D
        if resid%2 == 0:
            res.append(abs(bestm))
        else:
            res.append(abs(bestm-D))
            res.append(abs(bestm+D))

    res = min(res)
    print(res)



main()

Submission status: Wrong Answer

Solution code:
X, K, D = map(int, input().split())

if(X > 0):
    if(X-K*D > 0):
        print(X-K*D)
    else:
        while(X > 0):
            X = X-D
            K -= 1
        if(K % 2 == 0):
            print(abs(X))
        else:
            print(abs(X+D))
elif(X < 0):
    if((X+K*D) < 0):
        print(abs(X+K*D))
    else:
        while(X < 0):
            X = X+D
            K -= 1
        if(K % 2 == 0):
            print(abs(X))
        else:
            print(abs(X-D))

Submission status: Wrong Answer

Solution code:
x, k, d = map(int, input().split())
x = abs(x)
if x <= d:
    if k % 2:
        print(min(abs(x - d), abs(x - ((k - 1) * d) + d)))
    else:
        print(abs(x))
else:
    a = x // d
    b = x % d
    if a <= k:
        print(min(b, abs(x - ((k - 1) * d) + d)))
    else:
        print(x - (d * k))

Submission status: Wrong Answer

Solution code:
x, k, d=map(int, input().split(' '))
for i in range(k):
  if x > 0:
    x -= d
  elif (k-i) % 2 == 0:
    x = -x
    break
  else:
    x += d
    break

print(x)

Submission status: Wrong Answer

Solution code:

n = input()

n_list = n.split()

x = int(n_list[0])
k = int(n_list[1])
d = int(n_list[2])

def count(x, k, d):
	x_positive = 1
	if x > 0:
		x_positive = -1
	
	# まずベスポジエリアまでいけるかを確認
	if int(x / d) >= k:
		# いけない
		return abs(x) - d * k
	
	# いける
	
	# ベスポジエリアまで何回掛かるか？
	cnt = int(x / d)
	
	c = k - cnt # 残りの移動回数
#	print('残り{}回'.format(c))
	
	x = abs(x) - d * cnt
#	print('xは{}'.format(x))
	
	# 残りの移動回数が偶数なら現時点がベストになる
	if c % 2 == 0:
		return x
	
	# 奇数の場合（つまりまだ後1回移動できるとき）
	return min(abs(x + d), abs(x - d))
	

print('{}'.format(count(x,k,d)))

Submission status: Wrong Answer

Solution code:
x,k,d = map(int,input().split())
anl = []
for i in range(k):
	anl.append(abs(d*k + x - 2*d*(i+1)))

print(min(anl))

Submission status: Wrong Answer

Solution code:
x,k,d=map(int,input().split())

def solve(x, k, d):
    if k == 0:
        return x
    else:
        for i in range(k):
            x = min(map(abs, ((x - d), x, (x + d))))

        return x
print(solve(x,k,d))

Submission status: Wrong Answer

Solution code:
inp = input()
XKD = inp.split()
X = int(XKD[0])
K = int(XKD[1])
D = int(XKD[2])
kai = (K*D-X)/(2*D)
kai_r = round(kai)
l = []
l.append(abs(X+2*kai_r*D-K*D))
l.append(abs(X+2*(kai_r-1)*D-K*D))
l.append(abs(X+2*(kai_r+1)*D-K*D))
print(min(l))

Submission status: Wrong Answer

Solution code:
x, k, d = map(int, input().split())

# print(x,k,d)

x = abs(x)
if x<d:
    print(min(x, d-x))
    exit()

c = min(x//d, k)
x = abs(x-d*c)
k = k-c
# print(k,c,x)
if x == 0:
    if k%2==0: # even
        print(0)
    else: # odd
        print(d)

else: # +
    print(x)

Submission status: Wrong Answer

Solution code:
x, k, d = map(int, input().split())
x = abs(x)
# x > 0のとき
left = abs(x - d * (x // d + 1))
right = abs(x - d * (x // d))

if k < x // d:
    print(x - d * k)
    exit()

if (k - (x // d)) % 2 == 0:
    if (x // d) % 2 == 0:
        print(right)
    else:
        print(left)
else:
    if (x // d) % 2 == 1:
        print(right)
    else:
        print(left)

Submission status: Wrong Answer

Solution code:
import sys
x, k, d = list(map(int, input().split()))

if (x == d):
    if ((k % 2) == 0):
        print(d)
    else:
        print(0)
    sys.exit()

s = x // d
a = x % d
ans = 0

if (s > k):
    ans = x - (d * k) + a
else:
    ans = a

print(ans)

Submission status: Wrong Answer

Solution code:
import sys,bisect,string,math,time,functools,random,fractions
from heapq import heappush,heappop,heapify
from collections import deque,defaultdict,Counter
from itertools import permutations,combinations,groupby
rep=range;R=range
def Golf():n,*t=map(int,open(0).read().split())
def I():return int(input())
def S_():return input()
def IS():return input().split()
def LS():return [i for i in input().split()]
def MI():return map(int,input().split())
def LI():return [int(i) for i in input().split()]
def LI_():return [int(i)-1 for i in input().split()]
def NI(n):return [int(input()) for i in range(n)]
def NI_(n):return [int(input())-1 for i in range(n)]
def StoLI():return [ord(i)-97 for i in input()]
def ItoS(n):return chr(n+97)
def LtoS(ls):return ''.join([chr(i+97) for i in ls])
def RA():return map(int,open(0).read().split())
def RLI(n=8,a=1,b=10):return [random.randint(a,b)for i in range(n)]
def RI(a=1,b=10):return random.randint(a,b)
def Rtest(T):
    case,err=0,0
    for i in range(T):
        inp=INP()
        a1,ls=naive(*inp)
        a2=solve(*inp)
        if a1!=a2:
            print((a1,a2),inp)
            err+=1
        case+=1
    print('Tested',case,'case with',err,'errors')
def GI(V,E,ls=None,Directed=False,index=1):
    org_inp=[];g=[[] for i in range(V)]
    FromStdin=True if ls==None else False
    for i in range(E):
        if FromStdin:
            inp=LI()
            org_inp.append(inp)
        else:
            inp=ls[i]
        if len(inp)==2:
            a,b=inp;c=1
        else:
            a,b,c=inp
        if index==1:a-=1;b-=1
        aa=(a,c);bb=(b,c);g[a].append(bb)
        if not Directed:g[b].append(aa)
    return g,org_inp
def GGI(h,w,search=None,replacement_of_found='.',mp_def={'#':1,'.':0},boundary=1):
    #h,w,g,sg=GGI(h,w,search=['S','G'],replacement_of_found='.',mp_def={'#':1,'.':0},boundary=1) # sample usage
    mp=[boundary]*(w+2);found={}
    for i in R(h):
        s=input()
        for char in search:
            if char in s:
                found[char]=((i+1)*(w+2)+s.index(char)+1)
                mp_def[char]=mp_def[replacement_of_found]
        mp+=[boundary]+[mp_def[j] for j in s]+[boundary]
    mp+=[boundary]*(w+2)
    return h+2,w+2,mp,found
def TI(n):return GI(n,n-1)
def accum(ls):
    rt=[0]
    for i in ls:rt+=[rt[-1]+i]
    return rt
def bit_combination(n,base=2):
    rt=[]
    for tb in R(base**n):s=[tb//(base**bt)%base for bt in R(n)];rt+=[s]
    return rt
def gcd(x,y):
    if y==0:return x
    if x%y==0:return y
    while x%y!=0:x,y=y,x%y
    return y
def YN(x):print(['NO','YES'][x])
def Yn(x):print(['No','Yes'][x])
def show(*inp,end='\n'):
    if show_flg:print(*inp,end=end)

mo=10**9+7
inf=float('inf')
FourNb=[(-1,0),(1,0),(0,1),(0,-1)];EightNb=[(-1,0),(1,0),(0,1),(0,-1),(1,1),(-1,-1),(1,-1),(-1,1)];compas=dict(zip('WENS',FourNb));cursol=dict(zip('LRUD',FourNb))
l_alp=string.ascii_lowercase
#sys.setrecursionlimit(10**9)
read=sys.stdin.buffer.read;readline=sys.stdin.buffer.readline;input=lambda:sys.stdin.readline().rstrip()


show_flg=False
show_flg=True

ans=0


x,k,d=LI()
s=int((k*d-x)/(2*d))

ans=inf
for t in range(max(0,s-2),min(s+2,k)+1):
    ans=min(ans,abs(x+(2*t-k)*d))

print(ans)

Submission status: Wrong Answer

Solution code:
import math
x, k,d = map(int, input().split())
mn=1
if x<0:
    mn=(-1)
    x=abs(x)
xkdm= (x-k*d)
if xkdm>0:
    print(xkdm)
    exit()

ld = math.floor(-xkdm/2/d) 

if ((-1)*(xkdm+2*ld*d) <  (xkdm+2*(ld+1)*d)):
    print(mn*(-1)*(xkdm+2*ld*d))
else:
    print(mn*(xkdm+2*(ld+1)*d))

Submission status: Wrong Answer

Solution code:
x,k,d = map( int, input().split())

if( x == 0 ):
    if( k % 2 == 0 ):
        print("{}".format(0))
    else:
        print("{}".format(abs(d)))

if( x > 0 ) :
    if( x - k * d >= 0 ) :
        ret = x - k * d
        print("{}".format(ret))
    rem = x % d
    cost = x // d
    rem2 = abs(rem - d)
    remc = k - cost
    if( remc % 2 == 0 ) : 
        ret = rem
        print("{}".format(ret))
    else:
        ret = rem2
        print("{}".format(ret))
elif( x < 0 ) :
    if( x + k * d <= 0 ) :
        ret = abs( x + k * d )
        print("{}".format(ret))
    rem = (-x) % d
    cost = (-x) / d
    rem *= -1
    rem2 = rem + d
    remc = k - cost
    if( remc % 2 == 0 ):
        ret = abs(rem)
        print("{}".format(ret))
    else :
        ret = rem2
        print("{}".format(ret))

Submission status: Wrong Answer

Solution code:
import sys
X, K, D = map(int,input().split())
cnt = 0
a = X
b = 999999999
tmp = 0
if X == 0:
  print(0)
  sys.exit()
if X >= K*D:
  if K % 2 == 0:
    print(X - K*D)
  sys.exit()
if K % 2 == 0:
  tmp = int(X/D)
  #print(tmp)
  if tmp% 2 == 0:
    a = X - D*tmp
  else:
    a = X - D*tmp + D
  b = a - D*2
else:
  tmp = int(X/D)
  #print(tmp)
  if tmp% 2 == 1:
    a = X - D*tmp
  else:
    a = X - D*tmp + D
  b = a - D*2
print(min(abs(a),abs(b)))

Submission status: Wrong Answer

Solution code:
# cook your dish here
# your code goes here
a, b, c = input().split()
x=int(a)
k=int(b)
d=int(c)
val=(k*d-x)/(2*d)
fs=int(val)
# print(fs)
ans1=1000000000000000000
ans2=1000000000000000000
ans3=1000000000000000000
if fs<0:
	ans=abs(x-k*d)
elif fs==0:
	ans1=abs(x-k*d)
	ans2=abs(x-k*d+2*d)
	ans=min(ans1,ans2)
else:
    if fs-1>=0:
        ans1=abs(x-k*d+2*(fs-1)*d)
    if fs<=k:
        ans2=abs(x-k*d+2*fs*d)
    if fs+1<=k:
        ans3=abs(x-k*d+2*(fs+1)*d)
    ans=min(ans1,ans2,ans3)
print(ans)

Submission status: Wrong Answer

Solution code:
X, K, D = map(int, input().split())
ans = 0

if abs(X) < (K * D):
    q = abs(X) // D
    r = abs(X) % D
    if (K - q) % 2 == 0:
        ans = r
    else:
        ans = min(abs(r + D), abs(r - D))
elif X > 0:
    ans = X - (K * D)
elif X < 0:
    ans = -(abs(X) - (K * D))

print(ans)

Submission status: Wrong Answer

Solution code:
x,k,d= map(int, input().split())

if abs(x) >= k*d:
    print(abs(x)-k*d)
else:
    if(abs(x)%d==0 and (int(abs(x)/d)+10000000000000000 - k)%2 == 1):
        print(d)
    elif(abs(x)%d == 0):
        print(0)
    else:
        print((abs(x)-k*d+10000000000000000000*d)%d)

Submission status: Wrong Answer

Solution code:
x,k,d = map(int, input().split())
ans=0
p=x//d
if abs(x)>=k*d:
    ans=abs(x)-k*d
elif abs(x)<=d:
    if k%2==0:
        ans=abs(x)
    else:
        if x<=0:
            ans=x+d
        else:
            ans=d-x
else:
    if (k-p)%2==0:
        ans=abs(x)-p*d
    else:
        ans=abs(abs(x)-p*d-d)        
print(ans)

Submission status: Wrong Answer

Solution code:
X, K, D = map(int, input().split())
X=abs(X)
if X==0 and K%2==0:
    ans=0
elif X==0 and K%2==1:
    ans=D
elif D>=X and K%2==0:
    ans=X
elif D>=X and K%2==1:
    ans=D-X
elif X>0 and D<X :
    if K*D<X:
        ans=X-K*D
    else:
        ans=X
        for i in range(0,K):
            ans=ans-D
            if ans<0:
                if (K-i)%2==0:
                    ans=ans+D
                    ans=-ans
                    break
                else:
                    ans=-ans
                    break
print(ans)

Submission status: Wrong Answer

Solution code:
X, K, D = list(map(int, input().split()))
X = abs(X)
if K * D >= X:
    cgoal = X % D
    fgoal = abs(abs(cgoal) - D)
    times2cgoal = (X - cgoal) // D
    if times2cgoal % 2 == K % 2:
        print(min(cgoal, fgoal))
    else:
        if X >= 0:
            print(fgoal)
else:
    print(X - K * D)

Submission status: Wrong Answer

Solution code:
import sys
input=lambda: sys.stdin.readline().rstrip()
x,k,d=map(int,input().split())
if k%2==1:
  if x>0:
    x-=d
  else:
    x+=d
  k-=1

a=x%(2*d)
b=a-2*d
ans=abs(x-k*d)
if abs(x-a)//d<=k:
  ans=min(ans,abs(a))
if abs(x-b)//d<=d:
  ans=min(ans,abs(b))
print(ans)

Submission status: Wrong Answer

Solution code:
X,K,D = map(int, input().split())

Kp = [2*i-K for i in range(K)]
ABS = 10**15

for i in range(len(Kp)):
  A = X + Kp[i] * D
  if abs(A) <= ABS:
    ABS = abs(A)

print(ABS)

Submission status: Wrong Answer

Solution code:
import numpy as np


X, K, D = list(map(int,input().split()))

ans = 0
if abs(K * D) < abs(X):
    ans = (abs(X) - abs(K * D)) * (1 if X > 0 else -1)
else:
    num = X//D # 距離がD未満になるまでに必要な最小の移動回数
    ans = (abs(X) - abs(num * D)) * (1 if X > 0 else -1)
    # 残った回数が奇数の場合
    if (K - num) % 2 == 1:  
        ans = min([abs(ans - D), abs(ans + D)])
        
print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    X, K, D = map(int, input().split())

    val = [0] * 100000000
    val[0] = X
    #val = X
    #val_new = 0
    for i in range(K) :
        if abs(val[i] - D) < abs(val[i] + D) :
            val[i+1] = val[i] - D
        else :
            val[i+1] = val[i] + D
        #val[i+1] = min(abs(val[i] - D), abs(val[i] + D))
        #val_new = min(abs(val - D), abs(val + D))
        #val = val_new
        if (val[i+1] == val[i-1] or val[i+1] == val[i-2]) and i > 2:
            break

    print(abs(val[i+1]))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
x, k, d = map(int, input().split())
min = 1000000000
cnt = 0
if x > 0:
    for i in range(k):
        if abs(x-d) < min:
            min = abs(x-d)
            x-=d
            cnt += 1
        else:
            if abs(x-d)>abs(x+d):
                x = abs(x+d)
            else:
                x = abs(x-d)
            break
    k -= cnt
    if k%2 == 0:
        print(min)

    else:
        print(x)
        
else:
    for i in range(k):
        if abs(x+d) < min:
            min = abs(x+d)
            x+=d
            cnt += 1
        else:
            if abs(x-d)>abs(x+d):
                x = abs(x+d)
            else:
                x = abs(x-d)
            break
    k -= cnt
    if k%2 == 0:
        print(min)

    else:
        print(x)

Submission status: Wrong Answer

Solution code:
x, k, d = map(int, input().split())

while ((x > 0) and (k > 0)):
  x -= d
  k -= 1
if k % 2 == 1:
  print(x + d)
else:
  print(abs(x))

Submission status: Wrong Answer

Solution code:
x, k, d = (int(x) for x in input().split())

if x < k*d:
    print(x % d)
else:
    print(x - k * d)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
from bisect import bisect_left
from bisect import bisect_right
from collections import defaultdict
from heapq import heappop, heappush
import itertools
import random
from collections import deque
from decimal import *
import queue
from functools import reduce

input = sys.stdin.readline

def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))
def inputStr(): return input()[:-1]

inf = float('inf')
mod = 1000000007

#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-

def main():
	X,K,D = inputMap()

	ima = abs(X-0)
	if ima == 0:
		if K % 2 == 0:
			print(0)
			sys.exit()
		else:
			print(abs(D))
			sys.exit()

	if X > 0:
		pass
	else:
		X = abs(2*X)

	koko = abs(X-D)
	if koko == 0:
		if K % 2 == 0:
			print(abs(D))
			sys.exit()
		else:
			print(0)
			sys.exit()

	imaima = abs(X-(K*D))
	if imaima == 0:
		print(0)
		sys.exit()

	if X - K*D > 0:
		ans = X - K*D
		print(ans)
		sys.exit()
	else:
		tmp = X // D
		nokori = K - tmp
		if nokori % 2 == 0:
			ans = X % D
			print(ans)
			sys.exit()
		else:
			o000 = X % D
			assd = abs(D - abs(D-o000))
			print(assd)
			sys.exit()





#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
if __name__ == "__main__":
	main()

Submission status: Wrong Answer

Solution code:
x, k, d = map(int, input().split())

if abs(x)//d>=k:
    print(abs(x)-k*d)
    exit()

s = x

while k>0:
    s = s-d
    print(s,x)
    if abs(s)>=abs(x):
        break
    x = s
    k -= 1

if k == 0:
    print(abs(s))
    exit()

if d%2 == 0:
    print(abs(s))
else:
    print(abs(x))

Submission status: Wrong Answer

Solution code:
x, k, d = map(int,input().split())
now = x
if x and k and d > 100 and x < k * d:
  k = k - (k - 100)
for _ in range(k):
  if abs(now - d) <= abs(now + d):
    now = now - d
  elif abs(now + d) < abs(now - d):
    now = now + d
print(abs(now))

Submission status: Wrong Answer

Solution code:
from decimal import *
x, k, d = map(Decimal, input().split())

if x >= 0:
  if x-k*d >= 0:
    print(abs(x-k*d))
  else:
    if d%2 == k%2:
      print(abs(k-x%d))
    else:
      print(abs(x%d))
else:
  if -x+k*d <= 0:
    print(abs(-x+k*d))
  else:
    if d%2 == k%2:
      print(abs(k+x%d))
    else:
      print(abs(-x%d))

Submission status: Wrong Answer

Solution code:
X,K,D=map(int,input().split())
if X >= K*D :
    print(X-K*D)
else: #通りすぎる
    C=X-D*(X//D) #最寄りの正かつ１回は残っている
    A=K-(X//D) #残りの移動回数
    if A%2==0:
        print(D-C)
    else:
        print(C)

Submission status: Wrong Answer

Solution code:
import itertools

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

if K % 2 == 0:
  q, mod = divmod(abs(X), D*2)
  if mod <= abs(mod - D*2):
    if q*2 <= K:
      print(mod)
    else:
      print(abs(X-(K*D)))
  else:
    if q*2 <= K:
      print(abs(mod-D*2))
    else:
      print(abs(X-(K*D)))   
else:
  q, mod = divmod(abs(X), D)
  if mod <= abs(mod - D):
    if q <= K:
      print(mod)
    else:
      print(abs(X-(K*D)))
  else:
    if q <= K:
      print(abs(mod-D))
    else:
      print(abs(X-(K*D)))

Submission status: Wrong Answer

Solution code:
from math import gcd
x,k,d=list(map(int,input().split()))
print(max(abs(x-d),gcd(x,d)))

Submission status: Wrong Answer

Solution code:

playerinput = input()
lst = playerinput.split(" ")

X = int(lst[0])
K = int(lst[1])
D = int(lst[2])

lowest_value = X

lst_of_values = ["empty"]

if X == K == D:
    K = 0
    lowest_value = X


while K != 0:
    if abs(lowest_value + D) < lowest_value:
        lowest_value = lowest_value + D
        K -= 1

    if abs(lowest_value - D) < lowest_value:
        lowest_value = lowest_value - D
        K -= 1

    else:
        break

Submission status: Wrong Answer

Solution code:

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

ans=X

a=abs(X//D)

"""
for i in range(a):
    ans=min(abs(ans-D),abs(ans+D))
    
"""

if (K>a>0 or -K<a<0) and (K-a)%2==0:
    ans=min(abs(X-a*D),abs(X+a*D))
elif (K>a>0 or -K<a<0) and (K-a)%2==1:
    ans=min(abs(X-(a+1)*D),abs(X+(a+1)*D))


elif a>=K:
    ans=X-K*D
else:
    ans=X+K*D
    


print(ans)

Submission status: Wrong Answer

Solution code:
a,b,c=map(int,input().split())

if abs(a)>=abs(b*c):
    print(abs(abs(a)-abs(b*c)))
    exit()

n=abs(a)
m=n//c
if abs(a)-abs(c*m)>abs(a)-abs(c*(m+1)):
    m=m+1
print(m)
if (b-m)%2==0:
    print(abs(abs(a)-abs(c*m)))
else:
    print(min(abs(abs(a)-abs(c*(m+1))),abs(abs(a)-abs(c*(m-1)))))

Submission status: Wrong Answer

Solution code:
# [] = list(map(int, input().split()))
import numpy as np

[x, k, d] = list(map(np.uint64, input().split()))
x = abs(x)
howMany = x // d
remainder = x % d
stepLeft = k - howMany
if k < howMany:
    print((howMany - k) * d)
elif stepLeft % 2 == 0:
    print(remainder)
elif stepLeft % 2 == 1:
    print(d - remainder)

Submission status: Wrong Answer

Solution code:
x, k, d = map(int, input().split())

if abs(x) > abs(k*d):
    print(abs(x - k*d))
else:
    a = x // d
    z1 = x - a*d
    z2 = x - (a+1)*d
    if z1 == 0:
        print(abs(z2))
    else:
        b = min(abs(z1), abs(z2))
        print(abs(b))

Submission status: Wrong Answer

Solution code:
x,k,d=[int(i) for i in input().split()]

x=abs(x)

t=x

if k%2==1:
    t-=d
    k-=1

min=abs(t)

if x-k*d>0:
    print(x-k*d)
    exit(0)

if x-k*d<0:
    k-=(x/(d*2))*2


while(-x<t and k>0):
    t-=d*2
    k-=2
    if min < abs(t):
        break
    else:
        min=abs(t)

print(min)

Submission status: Wrong Answer

Solution code:
x, k, d = map(int, input().split())
ischange = bool()
ischanging = bool()
bef_x = 0

if x <= 0:
    ischange = False
    ischanging = False
else:
    ischange = True
    ischanging = True

for i in range(k):
    if ischange == ischanging:
        if x <= 0:
            x = x + d
            ischanging = False
        else:   
            x = x - d
            ischanging = True
    else:
        if x <= 0:
            bef_x = x + d
        else:   
            bef_x = x - d
        break
if bef_x == 0:
    bef_x = x

if k % 2 == 0:
    print(abs(x))
else:
    print(abs(bef_x))

Submission status: Wrong Answer

Solution code:
x, k, d = map(int, input().split())

if abs(x) - (d * k) > d:
    print(abs(x) - (d * k))
    exit()

k -= abs(x) // d
a = x % d
b = min(abs(a + d), abs(a - d))

if k % 2 == 0:
    print(a)
else:
    print(b)

Submission status: Wrong Answer

Solution code:
x,k,d= map(int, input().split())

if abs(x) > k*d:
    print(abs(x)-k*d)
else:
    if(abs(x)%d==0 and (int(abs(x)/d)+10000000000000000 - k)%2 == 1):
        print(d)
    elif(abs(x)%d == 0):
        print(0)
    else:
        print((abs(x)-k*d+10000000000000000000*d)%d)

Submission status: Wrong Answer

Solution code:
import math

start, kai, step = map(int, input().split(" "))

if start >= 0:
    if 0 <= start - (kai * step):
        print(abs(start) - (kai * step))
        exit()
else:
    if 0 > start - (kai * step):
        print(abs(start) - (kai * step))
        exit()

ans1 = start % step
ans2 = abs(ans1 - step)
if (kai - abs(start // step)) % 2 == 0:
    print(abs(ans1))
else:
    print(abs(ans2))

Submission status: Wrong Answer

Solution code:
X, K, D = map(int,input().split())

a = abs(X // D)
if a >= K:
  ans = min(abs(X - D*K), abs(X + D*K ))
  print(ans)
else:
  if abs(X - D*a) <= abs(X - D*(a+1)):
    if (K - a)%2 == 0:
      print(abs(X-D*a))
    else:
      print(abs(X - D*(a+1)))
  else:
    if (K-a)%2 == 0:
      print(abs(X - D*(a+1)))
    else:
      print(abs(X-D*a))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Aug 15 21:49:56 2020

@author: naoki
"""

X,K,D = map(int, input().split()) 
Xmin = abs(X) # x座標の絶対値の最小値(後に更新する)
X2 =  0 
# K 回後のXの座標の値は、 X_last = X + D(i - (K -i))
for i in range(K):
        X2 = X + D*(2*i - K)
        if abs(X2) <= Xmin: 
            Xmin = abs(X2)
print(Xmin)

Submission status: Wrong Answer

Solution code:
x, k, d = map(int, input().split())  # x現在地,k 移動回数,D移動できる距離
a = x // d  # 移動できる回数
b = x % d  # 移動した特のあまり
if x <= 0:
    flag = False
else:
    flag = True
if k <= a:  # 移動する回数がkより大きいとき
    x = x - (k*d)
else:
    k = k - a  # ここで最短
    if k % 2 == 0:
        if flag:
            x = x - (a * d)
        else:
            x = x + (a * d)
    else:
        if flag:
            x = x - (a * d)
            x = x - abs(d)
        else:
            x = x + (a * d)
            x = x + abs(d)

print(abs(x))

Submission status: Wrong Answer

Solution code:
X,K,D = map(int,input().split())
b = 0

b = abs(X)//D
if b <= K:
    K -= b
else:
    b = K
    K = 0
    
    
if X > 0:
    X -= D*b
elif X < 0:
    X += D*b
    
if K == 0:
    print(X)
else:
    if X == 0:
        if K % 2 == 0:
            print(X)
        else:
            print(D)
    elif X > 0:
        if abs(X-D) < abs(X):
            X -= D
            K -= 1
        if K % 2 == 0:
            print(abs(X))
        else:
            print(min(abs(X-D),abs(X+D)))
    elif X < 0:
        if abs(X+D) < abs(X):
            X += D
            K -= 1
        if K % 2 == 0:
            print(abs(X))
        else:
            print(min(abs(X-D),abs(X+D)))

Submission status: Wrong Answer

Solution code:
X, K, D = [int(x) for x in input().split()]
if K % 2:
    K -= 1
    if X > 0:
        X -= D
    elif X < 0:
        X += D
D *= 2
K //= 2
if X // D < K:
    print(min(X%D, abs(X%D - D)))
else:
    if X < 0:
        print(abs(X + K*D))
    else:
        print(abs(X - K*D))

Submission status: Wrong Answer

Solution code:
X, K, D = list(map(int, input().split()))
X = abs(X)
if K * D >= X:
    cgoal = X % D
    fgoal = abs(abs(cgoal) - D)
    times2cgoal = (X - cgoal) // D
    print(cgoal, fgoal, times2cgoal)
    if times2cgoal % 2 == K % 2:
        print(min(cgoal, fgoal))
    else:
        if X >= 0:
            print(fgoal)
else:
    print(X - K * D)

Submission status: Wrong Answer

Solution code:
X, K, D = map(int, input().rstrip().split())
# print(X,K,D)
before = X + 1
for i in range(K):
  if X == before:
    if (K-i)%2 == 1:
      X = D
    else:
      X = before
    break
  if X<0:
    X += D
  else:
    X -= D
  before = X
print(abs(X))

Submission status: Wrong Answer

Solution code:
x, k, d = map(int,input().split())

# 移動制限がないときの最小値
m = min(x % d, abs(x - x%d))

# mを得るために必要な最小の移動回数
n = x // d

if k <= n:
    print(x - k * d)
    exit()

k -= n

x = x % d

if x > abs(x - d):
    x = x - d
    k -= 1

if k % 2 == 0:
    print(abs(x))
else:
    print(min(abs(x - d), abs(x + d)))

Submission status: Wrong Answer

Solution code:
X,K,D = map(int,input().split())
tmp = X -K*D
if X == 0:
    if K%2 == 0:
        ans = 0
    else:
        ans = abs(D)
elif tmp >= 0:
    ans = tmp
else:
    if X < 0:
        X *= (-1)
    dif  = X//D
    buf = X - D*dif
    if (K-dif) % 2  == 0:
        ans = abs(buf)
    else:
        ans = abs(buf - D)
print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
input_line = input().split(' ')
X = int(input_line[0])
K = int(input_line[1])
D = int(input_line[2])

num = X // D

if num >= K:
    if X > 0:
        X -= K * D
    else:
        X += K * D
else:
    K -= num
    if X > 0:
        X -= num * D
        if K % 2 == 1:
                X -= D
    else:
        X += num * D
        if K % 2 == 1:
                X += D

if X < 0:
    X *= -1
print(X)

Submission status: Wrong Answer

Solution code:
X, K, D= list(map(int, input().split()))
if X >= 0:
    a, b = X // D, X % D
    ans = X
    if a > K:
        ans = X - K * D
    elif K % 2 == a % 2:
        ans = b
    else:
        ans = abs(b - D)
    print(ans)
else:
    a, b = abs(X) // D, X % D
    ans = abs(X)
    if a >= K:
        ans = X + K * D
    elif K % 2 == (a + 1) % 2:
        ans = b
    else:
        ans = b - D
    print(abs(ans))

Submission status: Wrong Answer

Solution code:
X,K,D = map(int,input().split())

times_in = abs(X)//D
times_out = (abs(X)//D) + 1

if X>0:
    if X - K * D < 0:
        if times_in%2==0:
            if K%2 == 0:
                ans = min(abs(X - times_in * D),abs(X - (times_in + 2) * D))
            else:
                ans = min(abs(X - times_out * D), abs(X - (times_out - 2) * D))
        else:
            if K%2 == 0:
                ans = min(abs(X - times_out * D),abs(X - (times_out - 2) * D))
            else:
                ans = min(abs(X - times_in * D), abs(X - (times_in + 2) * D))
    else:
        ans = X - K * D

elif X<0:
    if X + K * D > 0:
        if times_in%2==0:
            if K%2 == 0:
                ans = min(abs(X + times_in * D),abs(X + (times_in + 2) * D))
            else:
                ans = min(abs(X + times_out * D), abs(X + (times_out - 2) * D))
        else:
            if K%2 == 0:
                ans = min(abs(X + times_out * D),abs(X + (times_out - 2) * D))
            else:
                ans = min(abs(X + times_in * D), abs(X + (times_in + 2) * D))
    else:
        ans = X + K * D
else:
    if K%2==0:
        ans = 0
    else:
        ans = abs(D)

print(ans)

Submission status: Wrong Answer

Solution code:
import math
x, k, d = map(int, input().split())
x = abs(x)

tmp1 = x - d * (math.ceil(x / d))
tmp2 = tmp1 + d

if x >= k * d:
    print(x - k * d)
elif x == d and k % 2 == 0:
    print(x)
elif k % 2 == 0:
    print(min(abs(tmp1), abs(tmp2)))
else:
    print(max(abs(tmp1), abs(tmp2)))

Submission status: Wrong Answer

Solution code:
x,k,d=list(map(int, input("").split()))
c=x//d
m=x%d
if c<k:
    m=abs(m)
    if (k-c)%2==0:
        x=m
    else:
        if abs(m+d) <= abs(m-d):
            x=m+d
        else:
            x=m-d
else:
    x=m

print(abs(x))