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

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

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

Submission status: Accepted

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

X = abs(X)
q = X // D
mod = X % D

if q > K:
    ans = abs(abs(X)-K*D)
elif q==K:
    ans = mod
else:
    ans1 = D-mod
    ans2 = mod

    if ans1<ans2:
        if (K-q-1)%2==1:
            ans = ans2
        else:
            ans = ans1
    else:
        if (K-q)%2==1:
            ans = ans1
        else:
            ans = ans2



print(ans)

Submission status: Accepted

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

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

    if X%D == 0:
        if abs(X // D) <= K:
            if (K - X // D)%2 == 0:
                res = 0
            else:
                res = D
        else:
            if X >= 0:
                res = abs(X - K * D)
            else:
                res = abs(X + K * D)
        print(res)
        sys.exit()

    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):
        if X >= 0:
            res = abs(X-K*D)
        else:
            res = abs(X+K*D)
        print(res)
        sys.exit()

    res = []

    if ablep:
        if X >= bestp:
            resid = K - (X - bestp) // D
        else:
            resid = K - (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 = K - (X - bestm) // D
        else:
            resid = K - (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: Accepted

Solution code:
x,k,d=map(int,input().split())
if x==0:
  if k%2==0:
    print(0)
    exit()
  else:
    print(d)
    exit()
x=abs(x)
if abs(x//d)>=k:
  if k%2==0:
    print(min(abs(x-k*d),abs(x+k*d),x))
    exit()
  print(min(abs(x-k*d),abs(x+k*d)))
  exit()
else:
  a=abs(x//d)
  if x>0:
    kouho=x-a*d
    nokori=k-a
    if nokori%2==0:
      print(kouho)
      exit()
    else:
      print(abs(kouho-d))
      exit()

Submission status: Accepted

Solution code:
s = input().split()
X=int(s[0])
K=int(s[1])
D=int(s[2])
q, mod = divmod(X, D)
if q < 0:
  q = -q
if 2*mod < D:
   if K < q:
      if X > 0:
         m = X - D*K
      else:
         m = X + D*K
   else:
       restmovement = (K-q)%2
       if restmovement==0:
          m = mod
       else:
          m = D - mod
else:
   if X > 0:
      if K < q+1:
         m = X - D*K
      else:
         restmovement = (K - q - 1)%2
         if restmovement==0:
                    m = D - mod
         else:
                    m = mod
   else:
       if K < q-1:
          m=X - D*K
       else:
          restmovement = (K+1-q)%2
          if restmovement==0:
                m = D-mod
          else:
                m = mod
if m < 0:
   print(-m)
else:
   print(m)

Submission status: Accepted

Solution code:
a,b,c=map(int, input().split()) 
k = b*c
l = abs(a)

if l > 0:
    if l > k:
        print(l-k)
    if l == k:
        print(0)
    if l < k:
        d = int(l//c)
        e = b - d
        if e%2== 0:
            print(l-c*d)
        else:
            f = d + 1
            print(f*c -l)
if l == 0:
    if b%2==0:
        print(0)
    else:
        print(c)

Submission status: Accepted

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

if abs(X) >= abs(K*D):
    print(abs(X)-abs(K*D))
else:
    rK = K - int(abs(X)/abs(D))
    if rK % 2 == 0:
        print(abs(abs(X)  - D*(int(abs(X)/abs(D)))))
    else:
        print(abs(abs(X)  - D*(1+int(abs(X)/abs(D)))))

Submission status: Accepted

Solution code:
X,K,D = map(int,input().split( ))
 
X = abs(X)
 
q, mod = divmod(X, D)

if q >= K:
  print(X-D*K)

else:
  cnt = K-q
  if cnt%2 == 0:
    print(mod)
  if cnt%2 == 1:
    print(D-mod)

Submission status: Accepted

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

X=abs(X)
idou=K*D
if(0<X):
    if(K*D<X):
        print(X-K*D)
        exit()

    a=abs(X//D)
    b=abs(X//D)+1
    kyori_a=X-a*D
    kyori_b=X-b*D

    if(K-a==0):
        print(abs(kyori_a))
        exit()
    if((K-a)%2==0):
        print(abs(kyori_a))
        exit()
    else:
        print(abs(kyori_b))
        exit()

elif(X==0):
    if(K%2==0):
        print(0)
        exit()
    else:
        print(D)
        exit()

Submission status: Accepted

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 x//d%2 == k%2:
      print(abs(x%d))
    else:
      print(abs(d-x%d))
else:
  if x+k*d <= 0:
    print(abs(x+k*d))
  else:
    if -x//d%2 == k%2:
      print(abs(-x%d))
    else:
      print(abs(d+x%d))

Submission status: Accepted

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

seki = K * D
amari = X % D

if 0 <= (X-seki) or (X+seki) <= 0 : # 原点を含まない
    result = min(abs(X-seki),abs(X+seki))
else : # 原点を含む
    if X >= 0 :
        if K%2 == 0 :
            if ((abs(X)-amari)/D) % 2 == 0 :
                #print("a")
                result = amari
            else :
                #print("b")
                result = abs(amari-D)
        else :
            if ((abs(X)-amari)/D) % 2 == 1 :
                #print("c")
                result = amari
            else :
                #print("d")
                result = abs(amari-D)
    else :
        amari = abs(X)%D
        if K%2 == 0 :
            if ((abs(X)-amari)/D) % 2 == 0 :
                #print("e")
                result = amari
            else :
                #print("f")
                result = abs(-amari+D)
        else :
            if ((abs(X)-amari)/D) % 2 == 1 :
                #print("g")
                result = amari
            else :
                #print("h")
                result = abs(-amari+D)
    
print(result)

Submission status: Accepted

Solution code:

inp = list(map(int,raw_input().split()))
x = inp[0]
k = inp[1]
d = inp[2]
x = abs(x)
if(x >= k*d):
    print(x-k*d)
else:
    res=10000000000000000000000000000000000000
    tmp = int(x/d)
    for y in range(-100,100):
        if((y+tmp)<=k and y+tmp >=0):
            rem = k-y-tmp
            if(rem%2 == 0):
                res=min(res,abs(x-(y+tmp)*d))
    print(res)

Submission status: Accepted

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

if abs(X) >= K * D:
  if X >= 0:
    Ans = X - (K * D)
  else:
    Ans = abs(X + (K * D))
    
else:
  abX = abs(X)
#  if X >= 0:
  if (K - abX // D) % 2 == 0:
    Ans = abX - ((abX // D) * D)
  else:
    Ans = abs(abX - ((abX // D) * D) - D)
      
#  else:
#    print(abs(X // D))
#    if (K - abs(X // D)) % 2 == 0:
#      Ans = abs(X + (abs((X // D)) * D))
#    else:
#      Ans = abs(X + (abs((X // D)) * D) - D)
    
print(Ans)

Submission status: Accepted

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

sm = abs(x)//d+1

if abs(x)-sm*d>0 :
    ans = abs(x)-sm*d 
elif abs(x)-d*k>0:
    ans = abs(x)-d*k
elif (k-sm)%2==0:
    ans = min(abs(x)-sm*d,abs(abs(x)-sm*d-2*d))
else:
    ans = min(abs(abs(x)-sm*d-d),abs(abs(x)-sm*d+d))

print(abs(ans))

Submission status: Accepted

Solution code:
[_x, _k, _d] = list(map(int, input().split(" ")))
_x = abs(_x)

if _x >= _d:
  _y = _x//_d
  if _k < _y:
    _y = _k
  _x = abs(_x - _d * _y)
  _k -= _y
if _k % 2 == 1:
  _x = abs(_x - _d)

print(_x)

Submission status: Accepted

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

    zero_flag=0
    if abs(X)>=K*D:
        zero_flag=1

    if zero_flag==0:
        d=abs(X)%D
        d_=abs(X)//D
        if (K-d_)%2==1:
            print(abs(D-d))
        else:
            print(abs(X)-abs(D*d_))
    else:
        print(abs(X)-K*D)

if __name__=="__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys,collections,math,random,fractions,bisect,itertools;sys.setrecursionlimit(10**7)
sr = sys.stdin.readline; P = print; P2 = lambda x: print(*x, sep="\n")
def I(i=0): t = [int(x)-i for x in sr().split()];return t
def S(): t = sr().split(); return t if len(t) > 1 else t[0]
L1,L2,L3,L4, = [],[],[],[]
 
x,k,d = I()

if abs(x)/d >= k:
    print(abs(x)-k*d)
else:
    if (k - math.floor(x/d))%2 == 0:
        print(abs(x%d))
    else:
        print(abs(x%d-d))

Submission status: Accepted

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

if K - tmp_k < 0:
    ans = abs(X - (D * K))
else:
    tmp = (K- tmp_k) % 2
    if tmp == 0:
        ans = abs(X - (D * tmp_k))
    else:
        ans = abs(X - D * (tmp_k + 1))
        
print(ans)

Submission status: Accepted

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

if abs(X) >= K*D:
    print(abs(X) - K*D)
else:
    n = abs(X) // D
    A = abs(X) - n * D
    if K % 2 == n % 2:
        print(A)
    else:
        print(abs(D-A))

Submission status: Accepted

Solution code:
x,k,d= map(int,input().split())
import sys
z = abs(x) // d
if z <= k:
    y = abs(x) - d * z
    if (k- z)% 2 != 0:
        y = abs(y-d)
else:
    y = abs(x) - d * k
if x == d:
    if k % 2 == 0:
        y = x
    else:
        y = 0
print(abs(y))

Submission status: Accepted

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

Submission status: Accepted

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

s =min(X//D,K)

X -= s*D
K -=s

if K % 2 ==0:
  print(abs(X))
  
else:
  X -=D
  print(abs(X))

Submission status: Accepted

Solution code:
import sys
X, K, D = map(int,input().split())
cnt = 0
a = X
b = 0
tmp = 0
if abs(X) >= K*D:
  if X > 0:
    print(X - K*D)  
  else:
    print(abs(X + K*D))
  sys.exit()
if K % 2 == 0:
  tmp = int(X/D)
  if tmp% 2 == 0:
    a = X - D*tmp
  else:
    a = X - D*tmp + D
  b = a - D*2
else:
  tmp = int(X/D)
  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: Accepted

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


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

	num= abs(x)//d
	if k<=num:
		if x<0:
			x=x+k*d
		else:
			x=x-k*d
		print(abs(x))
	else:
		if x<0:
			x=x+num*d
		else:
			x=x-num*d

		k=k-num

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

Submission status: Accepted

Solution code:
import math
x,k,d=map(int, input().split())
if k*d <= abs(x):
	print(abs(x)-d*k)
else:
	n = math.floor(abs(x)/d)
	if (k-n)%2==0:
		print(abs(x)-d*n)
	else:
		print(abs(abs(x)-d*(n+1)))

Submission status: Accepted

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

else:
    if(K%2==1):
        print(D)
    else:
        print(0)

Submission status: Accepted

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

if X == 0:
    if K % 2 == 0:
        print(0)
    else:
        print(D)
        
elif X > 0 :
    if (X // D) >= K:
        print(X - K*D)
    else:
        X1 = X - (X // D) * D
        K1 = K - (X // D)
        if K1 % 2 == 0:
            print(X1)
        else:
            print(abs(X1-D))
else:
    if (-X // D) >= K:
        print(abs(X + K*D))
    else:
        X1 = X + (-X // D)*D
        K1 = K - (-X // D)
        if K1 % 2 == 0:
            print(abs(X1))
        else:
            print(abs(X1+D))

Submission status: Accepted

Solution code:
#X:座標
#K：試行回数
#D：移動距離

X,K,D=map(int,input().split())
b=0
Xabs=abs(X)
J=Xabs%D
N=Xabs//D

if N>K:
    b=Xabs-K*D

else:
    if (K-N)%2==0 :
        b=J
   
    #取れる数字はXをKで割った時のあまり(J)or(K-J)
    #残りは試行回数でどうなるか
    
    else:
        b=D-J
        

print(b)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
x,k,d=list(map(int,input().split()))
flag=0
if(x<0):
    flag=1
    x=abs(x)
if(x-k*d<0):
    t=(x//d)
    if(t&1==k&1):
        print(abs(x-t*d))
    else:
        print(abs(x-(t+1)*d))
else:
    print(abs(x-k*d))

Submission status: Accepted

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

if x<0:
  x *= -1

if x>=0 and x-k*d>=0:
  print(x-k*d)
else:
  tmp = x//d
  if tmp%2==k%2:
    print(x%d)
  else:
    print(d-x%d)

Submission status: Accepted

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

abs_x = abs(x)
ans = 0
left = 0

if x == 0:
    if k % 2 == 0:
        ans = 0
    else:
        ans = d

else:
    if abs_x // d > k:
        ans = abs_x - d * k
    elif x // d == k:
        ans = 0
    else:
        left = k - abs_x // d
        if left % 2 == 0:
            ans = abs_x % d
        else:
            ans = abs(abs_x % d - d)

print(ans)

Submission status: Accepted

Solution code:
X, K, D = map(int,input().split())
absX = abs(X)
t = abs(X // D)
remain_K = K - t
l = X % D
if remain_K < 0:
    a = abs(X - (K * D))
    b = abs(X + (K * D))
    if a < b:
        ans = a
    else:
        ans = b
else:
    if remain_K % 2 == 0:
        a = abs(l - (D * 2))
        b = abs(l + (D * 2))
        if a < b and a < l:
            ans = a
        elif a > b and b < l:
            ans = b
        else:
            ans = l
    else:
        a = abs(l - D)
        b = abs(l + D)
        if a < b:
            ans = a
        else:
            ans = b
print(ans)

Submission status: Accepted

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

if x - D*K >= 0 :
    print(x - D*K)
    exit()

k = x//D
if (K - k)%2 == 0:
    print(x - D*k)
    exit()
if x - D*(k-1) < abs(x - D*(k+1)):
    print(x - D*(k-1))
    exit()
else:
    print(abs(x - D*(k+1)))

Submission status: Accepted

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

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

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

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

Submission status: Accepted

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


x = abs(x)
min_pos = x % d
min_neg = min_pos - d

num_to_min_pos = x // d
if k <= num_to_min_pos:
    print(x - d * k)
    
else:
    if (k - num_to_min_pos) % 2 == 0:
        print(min_pos)
    else:
        print(abs(min_neg))

Submission status: Accepted

Solution code:
l=list(map(int,input().split()))
x=l[0]
k=l[1]
d=l[2]
x1=abs(x)

cou=x1//d
cou+=1
x1=x1-d*cou
# while x1>0:
#   x1-=d
#   cou+=1
if k<cou:
  x2=abs(x)-k*d
  print(x2)


else:
  a=cou%2
  b=k%2
  x4=x1*(-1)
  if a==b:
    print(abs(x1))
  if a!=b:
    print(x1+d)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
x,k,d=map(int,input().split())
if x<=0:
  x=x*(-1)
if d>=x:
  if k%2==0:
    print(x)
    exit()
  else:
    print(d-x)
    exit()
else:
  if x//d>=k and x>d:
    print(x-k*d)
    exit()
  elif x<=d:
    if k%2==0:
      print(x)
      exit()
    else:
      print(d-x)
    exit()
  else:
    if (x//d)%2==k%2:
      print(x%d)
      exit()
    else:
      print(d-x%d)
      exit()

Submission status: Accepted

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

aX = abs(X)
if aX // D >= K:
    ans = aX - K * D
else:
    if (K - (aX // D )) % 2 == 0:
        ans = aX % D
    else:
        ans = (aX % D) - D
    
print(abs(ans))

Submission status: Accepted

Solution code:

X, K, D = map(int, input().split())
K2 = K
#X=１に近いものにおきかえる
if X >= 0:
  a= 1
else:
  a = -1
#何回引けるか
h = min(K, int(a*X/D))
K = K-h
X = X - a*D*h

#print(X, K, D)
      


flg = 0
minx = abs(X)
Xnow = X
Xprepre = 0
for i in range(K):
  if i != 0:
    Xprepre = Xpre
  Xpre = Xnow #前回
  X1 = Xnow -D
  X2 = Xnow +D
  if abs(X1) < abs(X2):
    Xnow = X1
  else:
    Xnow = X2
  #繰り返したらおわり
  if Xprepre == Xnow:
    flg = 1
    if (K-i+1)%2 == 0:
      print(abs(Xnow))
    else:
      print(abs(Xpre))
    break
if flg == 0:
  print(abs(Xnow))

Submission status: Accepted

Solution code:
import math

if __name__ == '__main__':
    s = input().split(' ')
    X, K, D = abs(int(s[0])), int(s[1]), int(s[2])

    a = X // D

    if a >= K:
        print(X - K*D)

    else:
        if (K -a)  % 2 == 0:
            print(X % D)
        else:
            print(D - X % D)

Submission status: Accepted

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

r, s = divmod(x, d)
if r < 0:
    r = abs(r)
    if k < r:
        print(abs(x + k * d))
    elif r%2 == k%2:
        print(s)
    elif r%2 != k%2:
        print(abs(s-d))
else:
    if k <= r:
        print(abs(x - k * d))
    elif r%2 == k%2:
        print(s)
    elif r%2 != k%2:
        print(abs(s-d))

Submission status: Accepted

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

#偶数、奇数
isEven = (k%2 == 0)

#一番いい回数
div = abs(x//d)


#0,a,b
ans = 0
if k < div:
    if x > 0:
        # print("0")#消す
        print(abs(x-d*k))
    else:
        # print("1")#消す
        print(abs(x+d*k))
else:
    if x > 0:
        if (k-div) % 2 == 0:
            # print("A")#消す
            print(abs(x-d*div))
        else:
            # print("B")#消す
            hoge = x-d*div
            if hoge+d < abs(hoge-d):
                print(abs(hoge+d))
            else:
                print(abs(hoge-d))
    else:
        if (k-div) % 2 == 0:
            # print("C")#消す
            print(abs(x+d*div))
        else:
            # print("D")#消す
            hoge = x+d*div
            if hoge+d < abs(hoge-d):
                # print("E")#消す
                print(abs(hoge+d))
            else:
                # print("F")
                print(abs(hoge-d))

Submission status: Accepted

Solution code:
x,k,d=list(map(int, input("").split()))
x=abs(x)
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=x-k*d
print(abs(x))

Submission status: Accepted

Solution code:
import sys

X, K, D = [int(x) for x in input().strip().split(" ")]

X = abs(X)
k, x = divmod(X, D)

if K <= k:
    print(X - D * K)
    sys.exit(0)

X = x
K -= k

if K % 2 == 0:
    print(abs(X))
    sys.exit(0)

print(abs(X - D))

Submission status: Accepted

Solution code:
data = input().split(" ")
x0 = int(data[0])
k  = int(data[1])
d  = int(data[2])

const = x0 - k*d
if const >= 0:
  print(const)
else:
  ii = int(abs(const)/(2*d))
  if ii >= k:
    print( abs(const+2*d*(k)) )
  else:
    x_1 = abs(const+2*d*(ii-1))
    x_2 = abs(const+2*d*(ii))
    x_3 = abs(const+2*d*(ii+1))
    x = [x_1,x_2,x_3]
    x.sort()
    print(x[0])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
x,k,d=[int(x) for x in input().split()]
if k<(abs(x)/d):
  print(abs(abs(x)-d*k))
else:
  if x%d==0:
    if (k-(abs(x)/d))%2==0:
      print(0)
    elif (k-(abs(x)/d))%2==1:
      print(d)
  else:
    n=int(abs(x)/d)+1
    if (k-n)%2==0:
      print(abs(abs(x)-d*n))
    elif (k-n)%2==1:
      print(abs(abs(x)-d*(n-1)))

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!

input_line = input().rstrip().split(" ")
Answer = 0

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

# 場合1 : 0に届かない場合

if X > 0 and X - K * D >= 0:
    Answer = X - K * D
elif X < 0 and X + K * D <= 0:
    Answer = X + K * D

# 場合2 : 0をまたいで移動する場合

elif X >= 0:
    nearpin = X - abs(int(X / D)) * D
    nokori = K - abs(int(X / D))
    if nokori % 2 == 0:
        Answer = nearpin
    else:
        Answer = nearpin - D
elif X < 0:
    nearpin = X + abs(int(X / D)) * D
    nokori = K - abs(int(X / D))
    if nokori % 2 == 0:
        Answer = nearpin
    else:
        Answer = nearpin + D

print(abs(Answer))

Submission status: Accepted

Solution code:
import sys
import math
def get_array(): return list(map(int, sys.stdin.readline().strip().split()))
def get_ints(): return map(int, sys.stdin.readline().strip().split())
def input(): return sys.stdin.readline().strip()
x,k,d=get_ints()
x=abs(x)
a=x-k*d
ans=0
if a>=0:
    ans=a
else:
    c=k-x//d
    if c%2==0:
        ans=abs(x-(x//d)*d)
    else:
        ans=min(abs(x-(x//d)*d+d),abs(x-(x//d)*d-d))
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
x, k , d = map(int, input().split())
y = abs(x)
count = y // d
e = y % d
f = abs(e-d)
if e > f:
    min1 = f
    min2 = e
    count += 1
else:
    min1 = e
    min2 = f
if count > k:
    print(y - d * k)
else:
    if (k - count)%2 == 0:
        print(min1)
    else:
        print(min2)

Submission status: Accepted

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

if abs(X)/D >= K:
    X = abs(X)-D*K
    print(X)
else:
    X = min(abs(X-D*S), abs(X+D*S))
    K = K - S
    for k in range(K):
        if min(abs(X+D), abs(X-D)) < abs(X):
            X = min(abs(X+D), abs(X-D))
            K -= 1
        else:
            if K%2 == 0:
                print(X)
                break
            else:
                X = min(abs(X+D), abs(X-D))
                print(X)
                break

    if K == 0:
        print(X)

Submission status: Accepted

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()

def main():
    x, k, d = map(int, input().split())
    x = abs(x)
    cnt = x // d
    point = x % d
    if x == 0:
        if k % 2 == 0:
            point = 0
        else:
            point = d
    else:
        # 最小値まで行く回数がKより多い場合
        if cnt > k:
            point = x - k * d
        # Kより少ないときは
        else:
            # もう一回移動して最小値を確かめる
            if point == 0:
                if (k - cnt) % 2 == 0:
                    point = 0
                else:
                    point = d
                print(point)
                exit()
            else:
                if abs(point - d) < abs(point):
                    point = point - d
                    cnt += 1
            # k-cntが偶数の時はそのまま、奇数の時は一回逆方向にdだけ進む
            if (k - cnt) % 2 == 0:
                pass
            else:
                point = min(abs(point - d), abs(point + d))
    print(abs(point))

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

X = abs(X)

n = X // D
m = K - n

A1 = abs(X - n*D)
A2 = abs(X - (n+1)*D)

Ans = A2

if m > 1:
    if A1 <= A2:
        if m % 2 == 0:
            Ans = A1
    else:
        if m % 2 == 0:
            Ans = A1
else:
    Ans = abs(X - K*D)

print(Ans)

Submission status: Accepted

Solution code:
def resolve():
    X, K, D = map(int, input().split())
    X = abs(X)
    q, mod = divmod(X, D)
    # K>qのとき一番小さくなる。
    # D/2より小さい時は引いて絶対値が最小
    if q>=K:
        # 届かないとき。
        answer = X - K*D
    else:
        # 届くとき
        positive_abs_min = X-q*D
        negative_abs_min = D - positive_abs_min
        # positive_abs_minのあとに偶数回残っていればpositive_abs_min
        rest_k = K - q
        if rest_k%2 == 0:
            answer = positive_abs_min
        else:
            answer = negative_abs_min
    print(answer)
resolve()

Submission status: Accepted

Solution code:
def function(X, K, D):
    kai = int(abs(X)/D)
    if kai >= K:
        if X > 0:
            ans = X-K*D
        else:
            ans = X+K*D
    else:
        K = K-kai
        if X > 0:
            X = X-kai*D
        else:
            X = X+kai*D
        if K % 2 == 1:
            if X > 0:
                ans = X-D
            else:
                ans = X+D
        else:
            ans = X
    return abs(ans)


if __name__ == '__main__':
    X, K, D = input().split()
    X = int(X)
    K = int(K)
    D = int(D)
    print(function(X, K, D))

Submission status: Accepted

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

@author: Paras
"""

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

moves = abs(x)//d
if moves>=k:
    print(abs(x)-(k*d))
    
else:
    closest1 = abs(x)-moves*d
    closest2 = closest1-d
    if closest1==abs(closest2):
        if closest1==0:
            leftmoves = k-moves
            if leftmoves%2==0:
                print("0")
            else:
                print(d)
        else:
            print(closest1)
    else:
        if (k-moves)%2==0:
            print(abs(closest1))
        else:
            print(abs(closest2))

Submission status: Accepted

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

Submission status: Accepted

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

if(X > 0):
    if(X-K*D >= 0):
        print(X-K*D)
    else:
        l = X//D
        X = X-D*(l+1)
        K = K-l-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:
        l = -X//D
        X = X + D*(l+1)
        K = K-l-1
        if(K % 2 == 0):
            print(abs(X))
        else:
            print(abs(X-D))
else:
    if(K % 2 == 0):
        print(0)
    else:
        print(abs(D))