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())
while k!=0:
    if x>0:
        if x-(k*d)>0:
            x=x-(k*d)
            break
        if x-d==0:
            break
        if x-d < 0 :
            if abs(x-d)<x:
                x=abs(x-d)
                break
            else:
                break
        else :
            x=x-d
            k=k-1
    else:
        if x+(k*d)<0:
            x=abs(x+(k*d))
            break
        if x+d==0:
            break
        if x+d > 0 :
            if abs(x)>x+d:
                x=(x+d)
                break
            else:
                x=abs(x)
                break
        else :
            x=x+d
            k=k-1

print (abs(x))

Submission status: Wrong Answer

Solution code:
x, k, d = map(int, input().split())
if k%2 == 0 and abs(x)==abs(d):
  print(abs(x))
  exit()

for i in range(k):
  if abs(x - d) <= abs(x + d):
      x = x - d
  else:
      x = x + d
  print(x)
print(abs(x))

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np
import math
import collections
import copy
from collections import deque 
from functools import reduce
from itertools import product
from itertools import combinations

# input = sys.stdin.readline
Xa, K, D = map(int, input().split())
X = abs(Xa)
Xs = X // D
Xm = X % D
if K < Xs:
#     print("A")
    print(abs(X-K*D))
elif Xa == 0:
    if K % 2 == 0:
        print(0)
    else:
        print(D)
elif Xm == 0:
    if K % 2 == 0:
        print(0)
    else:
        print(D)
elif Xm / 2 > K:
#     print("B")
#     print(abs(D-Xm))
    if (Xa >= 0):
        print(abs(D-Xm))
    else:
        print(abs(D-K))
else:
#     print(abs(D-K))
    if (Xa >= 0):
        print(abs(D-K))
    else:
        print(abs(D-Xm))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

x = abs(x)
ans = 0

if x == 0:
    if k % 2 == 0:
        ans = 0
    else:
        ans = d
if k == 1:
    ans = abs(x - d)
elif 2 * x <= d:
    ans = x
elif x <= d:
    if k % 2 == 0:
        ans = x
    else:
        ans = x - d
else:
    t = x // d
    if k >= t + 3:
        if (k - t) % 2 == 0:
            ans = min(abs(x - t * d), abs(x - (t + 2) * d))
        else:
            ans = min(abs(x - (t + 1) * d), abs(x - (t + 3) * d))
    else:
        ans = min(abs(x - k * d), abs(x - (k - 2) * d))

print(ans)

Submission status: Wrong Answer

Solution code:
start, move, distance = list(map(int, input().split()))
if abs(start) > abs(distance) and abs(start) // abs(distance) <= abs(move):
    n = abs(start) // abs(distance)
    print(abs(start) - (abs(distance) * n))
elif abs(start) > abs(distance) and abs(start) // abs(distance) > abs(move):
    n = abs(start) // abs(distance)
    print(abs(start) - (abs(distance) * abs(move)))
else:
    print(abs(start))

Submission status: Wrong Answer

Solution code:
x,k,d = map(int,input().split())
ans_1 = x%d #プラスの最小
ans_2 = abs(ans_1-d) #マイナスの最小

if x//d > k:
    print(x-(d*k))
    exit()
if ans_1 > ans_2:
    if (k-(x//d)-1)%2 == 0:
        print(min(ans_1,ans_2))
    else:
        print(max(ans_1,ans_2))
        
        
elif ans_1 < ans_2:
    if (k-(x//d))%2 == 0:
        print(int(ans_1))
    else:
        print(int(ans_2))

elif ans_1 == ans_2:
    print(ans_1)

Submission status: Wrong Answer

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

xi = abs(x)
if abs(x) > d*k:
  m = min(abs(x+d*k),abs(x-d*k))
else:
  n = ceil(xi // d)
  if (n+k) % 2 == 0:
    m = abs(xi-d*n)
  else:
    m = abs(xi-d*(n-1))
print(m)

Submission status: Wrong Answer

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

a=min(K,abs(X//D))
print(abs(X-a*D))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
X,K,D = [int(a) for a in input().split()]
X0=X
u=max(abs(X),D)
l=-u
now=0
for i in range(K):
    if i==0:
        if X>0 and X-K*D>0:
            print(X-K*D)
            break
        if X<0 and X+K+D<0:
            print(X+K*D)
            break
    if X>0:X-=D
    elif X<=0:X+=D
    if X>0 and X<X0:
        u=X
        now=1
    if X<0 and X>-X0:
        l=X
        now=-1
    if X==abs(X0):
        u=X
        if i!=K-1:l=0
        now=1
    if X==-abs(X0):
        l=X
        if i!=K-1:u=0
        now=-1
    #print(X,u,l,i,now)
    if X==0 and i==K-1:
        print("0")
        break
    if (u>0 and u<X0 and l<0 and l>-X0) or (u==0 or l==0):
        if (K-1-i)%2==0:
            if now==1:
                print(u)
            else:
                print(abs(l))
        else:
            if now==1:
                print(abs(l))
            else:
                print(u)
        break
    if i==K-1:
        print(abs(X))

Submission status: Wrong Answer

Solution code:
from math import ceil, floor
X, K, D = map(int, input().split())
ans = abs(X)
fapp = (K*D - X)/(2*D)

if fapp < 0:
    list = [0]
elif fapp > K:
    list = [K]

else:
    list = [ceil(fapp), floor(fapp), int(fapp)]
for f in list:
    abso = abs(X + f*D - (K-f)*D)
    if abso < ans:
        ans = abso

print(ans)

Submission status: Wrong Answer

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

result=X
for i in range(K):
  if abs(result-D)>abs(result+D):
    result=result+D
  elif abs(result+D)>abs(result-D):
    result=result-D
  elif abs(result)==abs(result+D) or abs(result)==abs(result-D):
    break
    
  if result==0 and i%2==0 and K%2==0:
    result=X
    break

print(str(abs(result)))

Submission status: Wrong Answer

Solution code:
x,k,d=map(int,input().split())
c=0
for i in range(k):
  while x>0 and i<k:
    x-=d
    i+=1
  while x<0 and i<k:
    x+=d
    i+=1
print(abs(x))

Submission status: Wrong Answer

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

a = abs(X) // D
if a <= K:
  b = abs(X) - (D * a)
  c = abs(b - D)
else:
  b = abs(abs(X) - (D * K))
  c = 100000000000000000

if ((K - a)%2 == 0) and b <= c:
  print(b)
elif ((K - a)%2 == 1) and b <= c:
  print(abs(b - D))
elif ((K - a)%2 == 0) and c < b:
  print(c)
elif ((K - a)%2 == 1) and c < b:
  print(abs(c - D))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

print(abs(x))

Submission status: Wrong Answer

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

x = abs(x)
ans = 0

if x == 0:
    if k % 2 == 0:
        ans = 0
    else:
        ans = d
if k == 1:
    ans = min(x, abs(x - d))
elif 2 * x <= d:
    ans = x
elif x <= d:
    if k % 2 == 0:
        ans = x
    else:
        ans = x - d
else:
    t = x // d
    if k >= t + 3:
        if (k - t) % 2 == 0:
            ans = min(abs(x - t * d), abs(x - (t + 2) * d))
        else:
            ans = min(abs(x - (t + 1) * d), abs(x - (t + 3) * d))
    else:
        ans = min(abs(x - k * d), abs(x - (k - 2) * d))

print(ans)

Submission status: Wrong Answer

Solution code:
m,K,D = input().split()
m = int(m)
K = int(K)
D = int(D)
min = 0;
if m < 0:
  m * -1
a = (m - m % D) / D
if a > K:
  min = m - D*K
else:
  K -= (m - m % D) / D
  if K % 2 == 0:
    min = m % D
  else:
    min = (m % D - D) * -1
print(min)

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

    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 = (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:
import math
X, K, D = map(int, input().split())
a = X / D
A = math.floor(a)
if A < K:
  if A % 2 == 0 and K % 2 == 0:
    if A + 2 < K:
  	  print(min(abs(X - D * A), abs(X - D * (A + 2))))
    else:
      print(abs(X - D * A))
  if A % 2 == 1 and K % 2 == 0:
    if A + 1 < K:
      print(min(abs(X - D * (A - 1)), abs(X - D * (A + 1))))
    else:
      print(abs(X - D * (A - 1)))
  if A % 2 == 0 and K % 2 == 1:
    if A + 1 < K:
      print(min(abs(X - D * (A - 1)), abs(X - D * (A + 1))))
    else:
      print(abs(X - D * (A - 1)))
  if A % 2 == 1 and K % 2 == 1:
    if A + 2 < K:
  	  print(min(abs(X - D * A), abs(X - D * (A + 2))))
    else:
      print(abs(X - D * A))
else:
  print(abs(X - D * K))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
x, k, d = map(int, input().split())
t = abs(x) // d
a = []
a.append(t - 3)
a.append(t - 2)
a.append(t - 1)
a.append(t)
a.append(t + 1)
a.append(t + 2)
a.append(t + 3)
b = []

for i in a:
    b.append(-i)
for i in b:
    a.append(i)

a.append(k)
a.append(k - 1)
a.append(-k)
a.append(-k + 1)

res = abs(x)
for i in a:
    if i % 2 == k % 2 and abs(i) <= k:
        res = min(res, abs(x - i * d))
print(res)

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:
    if K // 2 != 0:
        K = 0
        lowest_value = X
    else:
        K = 0
        lowest_value = 0

while K != 0:

    if abs(lowest_value + D) < lowest_value:
        lowest_value = lowest_value + D
        K -= 1
        lst_of_values.append(lowest_value)

    if abs(lowest_value - D) < lowest_value:
        lowest_value = lowest_value - D
        K -= 1
        lst_of_values.append(lowest_value)
    else:
        value1 = abs(lowest_value + D)
        value2 = abs(lowest_value - D)
        if value1 < value2:
            lowest_value = value1
            K -= 1
        else:
            lowest_value = value2
            K -= 1


print(lowest_value)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
a,b,c=map(int,input().split())
t=a//c
if a%c==0:
    if t==b:
        print(0)
    elif t<b and (b-t)%2==0:
        print(0)
    elif t<b and (b-t)%2!=0:
        print(c)
    else:
        print(a-b*c)
else:
    if t==b:
        print(a%c)
    elif t<b and (b-t)%2==0:
        print(a%c)
    elif t<b and (b-t)%2!=0:
        print(c-a%c)
    else:
        print(a-b*c)

Submission status: Wrong Answer

Solution code:
import numpy

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

now = X
temp = None
for i in range(0, K):
    if min(abs(now + D), abs(now - D)) == abs(now + D):
        temp = now
        now += D
    else:
        temp = now
        now -= D
    if numpy.sign(temp) != numpy.sign(now):
        break

result = min(abs(now + D), abs(now - D))

print(result)

Submission status: Wrong Answer

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

print(x)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

a=min(K,abs(X//D))
print(X-a*D)

Submission status: Wrong Answer

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

if X > 0 :
  q, mod = divmod(X, D)
  if q >= K:
    print(X-D*q)
  else:
    cnt = K-q
    if cnt%2 == 0:
      print(mod)
    if cnt%2 == 1:
      print(abs(D-mod))
      
elif X < 0 :
  Y = abs(X)
  q, mod = divmod(Y, D)
  if q >= K:
    print(X+D*q)
  else:
    cnt = K-q
    if cnt%2 == 0:
      print(abs(mod))
    if cnt%2 == 1:
      print(abs(D-mod))

Submission status: Wrong Answer

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

if K * D >= abs(X):      #到達する時
    cgoal = abs(X) % D
    if X < 0:
        cgoal = -cgoal
    if X >= 0:
        fgoal = min(cgoal - D, cgoal + D)
        times2cgoal = (X - cgoal) // D
    else:
        fgoal = max(cgoal - D, cgoal + D)
        times2cgoal = abs((X - cgoal) // D)

    #print(cgoal, fgoal, times2cgoal)

    if times2cgoal % 2 == K % 2:
        print(min(abs(cgoal), abs(fgoal)))
    else:
        print(abs(fgoal))
else:               #到達しない時
    print(abs(X) - K * D)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# FileName: 	C_fix
# CreatedDate:  2020-08-15 21:52:53 +0900
# LastModified: 2020-08-15 22:15:52 +0900
#


import os
import sys
# import numpy as np
# import pandas as pd


def main():
    x, k, d = map(int, input().split())
    tmp = 1e31
    s_index = 0
    flag = 1
    if x > 0:
        for s in range(k+1):
            if tmp >= abs(x-s*d):
                tmp = abs(x-s*d)
                s_index = s
            else:
                flag = 0
                break
    elif x < 0:
        for s in range(k+1):
            if tmp >= abs(x+s*d):
                tmp = abs(x+s*d)
                s_index = s
            else:
                flag = 0
                break
    else:
        s_index = 0
    if flag == 1:
        print(tmp)
    else:
        if (k-s_index) % 2 == 0:
            print(tmp)
        elif x > 0:
            print(abs(tmp-d))
        else:
            print(tmp+d)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

#TLE
"""
for i in range(k):
    if(abs(x-d) < abs(x+d)):
        x -= d
    else:
        x += d
print(x)
"""
c = 0

while(x>=0):
    if(c == k):
        print(x)
        sys.exit()
    x -= d
    c += 1


p_x = x + d
m_x = x

xx = 0

if(abs(p_x) < abs(m_x)):
    xx = p_x
else:
    xx = m_x


#捜査回数の奇遇  xが0になるまでの回数によって変わる
if((k-c) % 2 != 0):
    print(abs(xx))
 
else:
    if(abs(xx - d) < abs(xx + d)):
        print(xx)
        print(abs(xx - d))
    else:
        print(abs(xx +d ))

Submission status: Wrong Answer

Solution code:
x,k,d=map(int,input().split())
if x==0:
  if k%2==0:
    print(0)
  else:
    print(d)

if abs(x//d)>k:
  print(min(abs(x-k*d),abs(x+k*d)))
else:
  a=abs(x//d)
  if x>0:
    kouho=x-a*d
    nokori=k-a
    if nokori%2==0:
      print(kouho)
    else:
      print(abs(kouho-d))
  else:
    kouho=x+a*d
    nokori=k-a
    if nokori%2==0:
      print(abs(kouho))
    else:
      print(abs(kouho+d))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

X = abs(X)

quotient = X // D
rest = X % D

if quotient <= K:
  if quotient == 1 and rest ==0:
    tmp = X
  else:
    tmp = X - quotient * D
else:
  tmp = X - K * D 

print(abs(tmp))

Submission status: Wrong Answer

Solution code:

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

ans = x
ans_is_Plass = False

if 0 < x :

    # 理想の数字を追求
    if abs(x - (x // d * d)) < abs(x - (((x // d) + 1) * d)) :
        # print('1採用')
        i_ans = abs(x - (x // d * d))
        s_ans = abs(x - (((x // d) + 1) * d))
        ans_is_Plass == True
    else :
        # print('2採用')
        i_ans = abs(x - (((x // d) + 1) * d))
        s_ans = abs(x - (x // d * d))

    if k <= (x // d) :
        ans = x - (k * d)
    else : 
        if k % 2 == 0 :
            ans = i_ans
        else :
            ans = s_ans
else :
    if abs(x + (x // d * d)) < abs(x + (((x // d) + 1) * d)) :
        # print('1採用')
        i_ans = abs(x + (x // d * d))
        s_ans = abs(x + (((x // d) + 1) * d))
    else :
        # print('2採用')
        i_ans = abs(x + (((x // d) + 1) * d))
        s_ans = abs(x + (x // d * d))
        ans_is_Plass == True

    if k <= (x // d) :
        ans = x + (k * d)
    else : 
        if k % 2 == 0 :
            ans = i_ans
        else :
            ans = s_ans

print(ans)

Submission status: Wrong Answer

Solution code:
import math
X, K, D = map(int, input().split())
ans = X
minc_floor = math.floor(abs(X)/D)
minc_ceil = math.ceil(abs(X)/D)
min_kaisuu = K
# print(minc_ceil, minc_floor)
if minc_ceil <= K:
    for k in range(minc_floor, minc_ceil+1):
        for flug in [-1, 1]:
            if abs(X + flug*k*D) < abs(ans):
                ans = abs(X + flug*k*D)

                # print("ans" + str(ans))
                min_kaisuu = k
                # print(min_kaisuu)
    if (K-min_kaisuu) % 2 != 0:
        ans = min(abs(ans+D), abs(ans-D))
else:
    ans = min(abs(ans+K*D), abs(ans-K*D))
# print(min_kaisuu)
print(ans)

Submission status: Wrong Answer

Solution code:
X,K,D=map(int,input().split())
kk=(X//D)
#print(kk)
l=abs(K-kk)
#print(l)
if kk >= K:
  print(abs(X-K*D))
elif(l%2==0):
  print(abs(X-kk*D))
else:
  print(abs(X-(kk+1)*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)
if kai >= 0:
  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))
else:
  kai_r = round(kai)
  l = []
  l.append(abs(X-K*D))
  l.append(abs(X+2*D-K*D))
  print(min(l))

Submission status: Wrong Answer

Solution code:
x,k,d = map(int,input().split())
ans_1 = x%d #プラスの最小
ans_2 = abs(ans_1-d) #マイナスの最小

if x//d > k:
    print(x-(d*k))
    exit()
if ans_1 > ans_2:
    if (k-(x//d)-1)%2 == 0:
        print(min(ans_1,ans_2))
    else:
        print(max(ans_1,ans_2))
        
        
elif ans_1 < ans_2:
    if (k-(x//d))%2 == 0:
        print(int(ans_1))
    else:
        print(int(ans_2))

Submission status: Wrong Answer

Solution code:
x,k,d=map(int,input().split())
while k!=0:
    if x>0:
        if x-(k*d)>0:
            x=x-(k*d)
            break
        if x-d==0:
            print (x)
            break
        if x-d < 0 :
            if abs(x-d)<x:
                x=abs(x-d)
                break
            else:
                break
        else :
            x=x-d
            k=k-1
    else:
        if x+(k*d)<0:
            x=abs(x+(k*d))
            break
        if x+d==0:
            print (abs(x))
            break
        if x+d > 0 :
            if abs(x)>x+d:
                x=(x+d)
                break
            else:
                x=abs(x)
                break
        else :
            x=x+d
            k=k-1

print (x)

Submission status: Wrong Answer

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 == ((X-amari)/D)%2 :
            result = min(abs(amari),abs(amari-2*D))
        else :
            result = min(abs(amari+D),abs(amari-D))
    else :
        if K%2 == ((abs(X)-X%D)/D)%2 :
            result = min(abs(-X%D),abs(-X%D+2*D))
        else :
            result = min(abs(-X%D+D),abs(-X%D-D))

print(result)

Submission status: Wrong Answer

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

count=X//D
if K>count:
    if X>0:
        y=X-count*D
        if abs(X)>=(D/2):
            count=count+1
            y=X-count*D
        else:
            pass
        if (K-count)%2==0:
            pass
        else:
            y=y+D
    else:
        y=X+count*D
        if abs(X)>=(D/2):
            count=count+1
            y=X+count*D
        else:
            pass
        if (K-count)%2==0:
            pass
        else:
            y=y-D 
else:
    if X>0:
        y=X-K*D
    else:
        y=X+K*D

print(abs(y))

Submission status: Wrong Answer

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

if abs(x)<d :
    if k%2==0 :
        ans=x
    else :
        x1,x2=x-d,x+d

else :
    if x>0 :
        X=x%d
        K=k-(x//d)
        if K%2==0 and K>0 :
            ans=X
        elif K%2==1 and K>0 :
            x3,x4=X-d,X+d
            ans=x4
            if abs(x3)<x4 :
                ans=x3
        else :
            ans=x-k*d

    else :
        Y=abs(x)%d
        K=k-(abs(x)//d)
        if K%2==0 and K>0 :
            ans=-Y
        elif K%2==1 and K>0 :
            y1,y2=-Y+d,-Y-d
            ans=y1
            if abs(y2)<y1 :
                ans=y2
        else :
            ans=x+k*d

print(ans)

Submission status: Wrong Answer

Solution code:
x,k,d=map(int,input().split())
if x==0:
  if k%2==0:
    print(0)
  else:
    print(d)

if abs(x//d)>=k:
  print(min(abs(x-k*d),abs(x+k*d)))
else:
  a=abs(x//d)
  if x>0:
    kouho=x-a*d
    nokori=k-a
    if nokori%2==0:
      print(kouho)
    else:
      print(abs(kouho-d))
  else:
    kouho=x+a*d
    nokori=k-a
    if nokori%2==0:
      print(abs(kouho))
    else:
      print(abs(kouho+d))

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
    if K % 2 == 0 :
        limit = 10
    else :
        limit = 11
    for i in range(100000000) :
        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]) and i > limit :
            break

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


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
xkd = input('').split()
x = int(xkd[0])
k = int(xkd[1])
d = int(xkd[2])

if x > k*d:
    print(x-(k*d))
elif x < k*d and x > (k-1)*d:
    # ぎりぎり０をまたげるパターン
    x1 = x - k*d
    x2 = x - (k-2)*d
    # またぐか、2つ前か、の2択
    if abs(x1) > abs(x2):
        print(abs(x2))
    else:
        print(abs(x1))
else:
    # それ以降
    # 目的地を求める
    step1 = int(x%d) # 目的地までのステップ数
    a1 = x - step1*d # 目的地
    # 0を突き抜けたほうが目的地のケース
    if abs(a1) > abs(a1-d):
        a1 = a1-d
        step1 = step1 +1
    if (k-step1)%2 == 0:
        # 残ステップが偶数ならぴったり
        a2 = a1
    else:
        # 残ステップが奇数なら1個余分に移動
        if a1 > 0:
            a2 = a1-d
        else:
            a2 = a1+d
    print(abs(a2))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
X,K,D=map(int,input().split())
 
x=min(K,abs(X)//D)
 
if X>0:
    X_=X-(D*x)
else:
    X_=X+(D*x)
 
k=K-x
if k%2!=0:
    ans=min(abs(X_-D),abs(X_+D))
else:
    ans=X_
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
#C - Walking Takahashi
X,K,D= map(int,input().split())

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