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:
# Template 1.0
import sys, re
from collections import deque, defaultdict, Counter, OrderedDict
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from heapq import heappush, heappop, heapify, nlargest, nsmallest
def STR(): return list(input())
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def list2d(a, b, c): return [[c] * b for i in range(a)]
def sortListWithIndex(listOfTuples, idx):   return (sorted(listOfTuples, key=lambda x: x[idx]))
def sortDictWithVal(passedDic):
    temp = sorted(passedDic.items(), key=lambda kv: (kv[1], kv[0]))
    toret = {}
    for tup in temp:
        toret[tup[0]] = tup[1]
    return toret
def sortDictWithKey(passedDic):
    return dict(OrderedDict(sorted(passedDic.items())))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7


def findMaximum(low, high):
    # Base Case: Only one element is present in arr[low..high]*/
    if low == high:
        return fun(low)

        # If there are two elements and first is greater then
    # the first element is maximum */
    if high == low + 1 and fun(low) >= fun(high):
        return fun(high);

        # If there are two elements and second is greater then
    # the second element is maximum */
    if high == low + 1 and fun(low) < fun(high):
        return fun(low)

    mid = (low + high) // 2  # low + (high - low)/2;*/

    # If we reach a point where arr[mid] is greater than both of
    # its adjacent elements arr[mid-1] and arr[mid+1], then arr[mid]
    # is the maximum element*/
    if fun(mid) <= fun(mid+1) and fun(mid) <= fun(mid-1):
        return fun(mid)

        # If arr[mid] is greater than the next element and smaller than the previous
    # element then maximum lies on left side of mid */
    if fun(mid) >= fun(mid+1) and fun(mid) <= fun(mid-1):
        return findMaximum(mid+1,high)
    else:  # when arr[mid] is greater than arr[mid-1] and smaller than arr[mid+1]
        return findMaximum(low, mid-1)

def fun(zz):
    return abs(x+d*(2*zz-k))

x,k,d = MAP()

l = 0
r = k

print(findMaximum(l, r))

Submission status: Accepted

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

Submission status: Accepted

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

if x<0:
  x=-x
#print(x,k,d)

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

Submission status: Accepted

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

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

if b < 0:
    s = abs(x - k * d)
else:
    if b % 2 == 0:
        s = abs(x - a * d)
    else:
        s = abs(x - (a+1) * d)

print(s)

Submission status: Accepted

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

X = abs(X)

if K*D < X:
    ret = X - K*D
else:
    j = X//D
    if (K - j) % 2 == 0:
        ret = X - j*D
    else:
        ret = (j+1)*D - X

print(ret)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

from math import floor, ceil
def do(x, k, d):
    t1, t2 = floor(x/d), ceil(x/d)
    ogk = k
    ogx = x

    x -= t1*d
    k -= t1
    ans1 = 10**16
    if k%2 == 0:
        ans1 = abs(x)
    else:
        ans1 = min(abs(x + d), abs(x - d))

    x, k = ogx, ogk
    x -= t2*d
    k -= t2
    ans2 = 10**16
    if k%2 == 0:
        ans2 = abs(x)
    else:
        ans2 = min(abs(x + d), abs(x - d))
    
    print(min(ans1, ans2))


if x > 0:
    if x - k*d < 0:
        q = round(x/d)
        do(x, k, d)
    else:
        print(x - k * d)

elif x < 0:
    if x + k*d > 0:
        do(abs(x), k, d)
    else:
        print(abs(x + k*d))

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

Submission status: Accepted

Solution code:
# Date [ 2020-08-15 21:13:42 ]
# Problem [ c.py ]
# Author Koki_tkg

import sys;      from decimal import Decimal
import math;     from itertools import combinations, product
import bisect;   from collections import Counter, deque, defaultdict

# sys.setrecursionlimit(10 ** 6)
MOD = 10 ** 6 + 7
INF = 10 ** 9
PI = 3.14159265358979323846

def read_str():      return sys.stdin.readline().strip()
def read_int():      return int(sys.stdin.readline().strip())
def read_ints():     return map(int, sys.stdin.readline().strip().split())
def read_str_list(): return list(sys.stdin.readline().strip().split())
def read_int_list(): return list(map(int, sys.stdin.readline().strip().split()))
def lcm(a: int, b: int) -> int: return (a * b) // math.gcd(a, b)

def Main():
    x, k, d = read_ints()
    x = abs(x)
    cnt = x // d
    if cnt < k:
        x -= cnt * d
    else:
        cnt = k
        x -= cnt * d
    if abs(x - d) < x and (k-cnt)%2 == 1:
        x = abs(x - d)
        cnt += 1
    
    if (k - cnt) % 2 == 0:
        print(x)
    elif x == 0:
        print(d)
    else:
        print(min(abs(x - d), x + d)) 

if __name__ ==  '__main__':
    Main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

print(ans)

Submission status: Accepted

Solution code:

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

l=0
if x==0:
    if k%2==0:
        
        print(0)
    else:
        print(d)
elif x<0:
    if x+d*k>=0:
        x=abs(x)
        a=x%d
        b=d-a
        e=x//d
        if e&1==k&1:
            print(a)
        else:
            print(b)
    else:
        print(abs(x+d*k))
        
        
else:
    if x-d*k<=0:
        a=x%d
        b=d-a
        e=x//d
        if e&1==k&1:
            print(a)
        else:
            print(b)
    else:
        print(x-d*k)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#N = int(input())
X,K,D= list(map(int,input().split()))
cnt = 0
if X <= 0:
    X *=-1

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

while X >= 0 :
    X -= D
    cnt += 1

if ((K-cnt) % 2 == 0):
    print(X*-1)
else :
    print(X+D)

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
X, K, D = list(map(int, input().split()))
X = abs(X)
syo, amari = divmod(X, D)
if amari > (D-amari):
    syo = syo +1
if syo >= K:
    print(X - K*D)
else: 
    remain_num = K - syo
    position = abs(X - syo*D)
    if remain_num%2 == 1:
        position = abs(position-D)
    print(position)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
x,k,d = map(int, input().split())
x = abs(x)
y = x//d
z = x%d

if y >= k:
    print(x-k*d)
elif (k-y)%2 == 0:
    print(z)
else:
    print(d-z)

Submission status: Accepted

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

Submission status: Accepted

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

tmp = (x+d*k)/(2*d)
tmp = int(tmp)
ans = []
for i in [0,k,tmp]:
    for j in range(-10,10):
        a = i+j
        if a<0 or k<a:
            continue
        y = x+d*k - 2*d*a
        ans.append(abs(y))
ans = min(ans)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

if X > 0:
    k = X // D
    if k <= K:
        X -= D * k
        K -= k
    else:
        X -= D * K
        K = 0
        print(abs(X))
        exit()

    cur_abs = abs(X)
    while K > 0:
        X -= D
        if abs(X) > cur_abs:
            X += D
            break
        cur_abs = abs(X)
        K -= 1
    K = K % 2
    if K % 2 != 0:
        print(min(abs(X + D), abs(X - D)))
    else:
        print(cur_abs)


elif X < 0:
    k = abs(X) // D
    if k <= K:
        X += D * k
        K -= k
    else:
        X += D * K
        K = 0
        print(abs(X))
        exit()

    cur_abs = abs(X)
    while K > 0:
        X += D
        if abs(X) > cur_abs:
            X -= D
            break
        cur_abs = abs(X)
        K -= 1
    K = K % 2
    if K % 2 != 0:
        print(min(abs(X + D), abs(X - D)))
    else:
        print(cur_abs)


else:   # X == 0
    if K % 2 == 0:
        print(0)
    else:
        print(min(abs(X + D), abs(X - D)))

Submission status: Accepted

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

if abs(X) > K * D:
    if X >= 0:
        print(X - K * D)
    else:
        print(abs(X + K * D))
else:
    loop = abs(X) // D
    nok = K - loop
    if X >= 0:
        tmp = X - (loop * D)
        if nok % 2 == 0:
            print(tmp)
        else:
            print(abs(tmp - D))
    else:
        tmp = X + (loop * D)
        if nok % 2 == 0:
            print(abs(tmp))
        else:
            print(abs(tmp + D))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from math import *

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

A = (K*D + X) / (2 * D)

if K <= A:
    print(abs(X-K*D))
elif A <= 0:
    print(abs(X+K*D))
else:
    print(min(abs(X-2*ceil(A)*D+K*D) ,abs(X-2*floor(A)*D+K*D)) )

Submission status: Accepted

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

Submission status: Accepted

Solution code:
x,k,d=map(int,input().split())
#xを絶対値に変換
x=abs(x)
#場合分けxがdより十分に大きい。つまり近づける価値がある。
if x>=d:
    #ゼロ直前までは何回？
    n=x//d
    #ゼロ直前の値は？
    mina=x%d
    #場合分けゼロ直前まで十分な回数ある
    if k>=n:
        #偶数(k-nがゼロの場合含む)
        if (k-n)%2==0:
            print(mina)
        else:
            print(abs(mina-d))
    #十分な回数ない
    else:
        print(x-d*k)
else:
    if k%2==0:
        print(x)
    else:
        print(abs(x-d))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

# プラス側だけで考える
x = abs(x)

# xがDより小さくなるまで0に近づける
move_near_0 = min(x // d, k)
x -= d * move_near_0
k -= move_near_0

# kが偶数ならそこで往復、kが奇数なら-D
if k % 2 == 0:
    print(x)
else:
    print(abs(x - d))

Submission status: Accepted

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

if (x == 0):
    if (k % 2 == 0):
        ans = 0
    else:
        ans = d
else:
    if ((k * d) > abs(x)):
        y = abs(x) // d
        ans = abs(abs(x) - y * d)
        if ((k - y) % 2 == 1):
            ans = abs(d - ans)
    else:
        ans = abs(x) - k * d


print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
init_pos, move_max, move_dist = [int(x) for x in input().split(' ')]
if init_pos < 0:
    init_pos *= -1

move_cnt = min(init_pos // move_dist, move_max)

moved_pos = init_pos - move_cnt * move_dist
min_abs = moved_pos

if move_max - move_cnt > 0:
    dests = [moved_pos, move_dist - moved_pos]
    min_abs = dests[(move_max - move_cnt) % 2]

print(min_abs)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import deque
import sys, copy,itertools
input = sys.stdin.readline

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

x = abs(x)

q = x // d
mod = x % d

if q >= k:
  ans = x - k*d
else:
  if (k-q) % 2 ==0: 
    ans = mod
  else:
    ans = abs(mod-d)

print(abs(ans))

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
X, K, D = map(int, input().split())

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

Submission status: Accepted

Solution code:
x,k,d = map(int,input().split()) #x 最初の座標 k 移動回数　D移動距離
dum2 = 10000000
if x < 0:
    x = -x
div,mod = divmod(x,d)
if div >= k:
    print(x-(k*d))
else:
    dum1 = mod
    dum2 = abs(mod-d)
    if (k-div)%2 == 1:
        print(dum2)
    else:
        print(dum1)

Submission status: Accepted

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

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

Submission status: Accepted

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

a = (K * D - X) / (2*D)
a1, a2 = math.floor(a), math.ceil(a)

left = 0
right = K
def f(a):
    return X - K * D + 2 * a * D

mid = None
flag = 0
while left+1 < right:
    mid = (left + right) // 2

    if f(mid) == 0:
        flag = 1
        break
    elif f(mid) < 0:
        left = mid
    else:
        right = mid

if flag:
    print(abs(X - K * D + 2 * mid * D))
else:
    res1 = abs(X - K * D + 2 * left * D)
    res2 = abs(X - K * D + 2 * right * D)
    print(min(res1, res2))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

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

    s = int(X / D)
    if X - D*s >= X - D*(s+1):
        v0 = abs(X - D*s)
        v1 = abs(X - D*(s+1))
    else:
        s = s+1
        v1 = abs(X - D*s)
        v0 = abs(X - D*(s+1))

    ans = 0
    if K < s:
        ans = X - (K * D)
    else:
        if (K - s) % 2 == 0:
            ans = v0
        else:
            ans = v1
    print(ans)

if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:
X, K, D = map(int, input().split())
if X < 0:
    X = -X
steps, res = divmod(X, D)
if steps >= K:
    print(X - D * K)
else:
    K -= steps
    if K % 2 == 0:
        print(res)
    else:
        print(min(res + D, abs(res - D)))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
from sys import stdin
X, K, D = [int(i) for i in stdin.readline().split()]
pos = X
if abs(X) >= K * D:
    pos = abs(X) - (K * D)
else:
    res = abs(X) % D
    res_2 = abs(D) - res
    per = abs(X) // D
    if (K - per) % 2 == 0:
        pos = res
    else:
        pos = res_2
print(pos)

Submission status: Accepted

Solution code:


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

def sol(x, k, d):

    x = abs(x)

    temp = x // d

    if temp >= k:
        return x - d * k

    x -= temp * d
    k -= temp

    if k % 2 == 0:
        return x
    
    return abs(x - d)

print (sol(x, k, d))

Submission status: Accepted

Solution code:

def resolve():
    X, K, D = list(map(int, input().split()))
    X = -X if X < 0 else X
    m = X//D
    if m < K:
        if X%D > D-X%D:
            nmove = m+1
            print(D-X%D if (K-nmove)%2==0 else X%D)
        else:
            nmove = m
            print(X%D if (K-nmove)%2==0 else D-X%D)
    else:
        nmove = m
        print(X-K*+D)


if '__main__' == __name__:
    resolve()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

need = abs(x)//d

if x>0:
    if k>=need:
        x -= need*d
        k -= need
    else:
        x -= k*d
        k = 0
else:
    if k>=need:
        x += need*d
        k -= need
    else:
        x += k*d
        k = 0
    

while x>0 and k>0:
    x-=d
    k-=1

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

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
x, k, d = map(int,input().split())
x = x if x < 0 else -x

def solve():
    if (0 - x) >= k * d:
        return abs(x + k * d)
    s = (0 - x) // (d)
    if (k - s) % 2 == 0:
        return abs(x + d * s)
    return abs(x + d * (s + 1))

print(solve())

Submission status: Accepted

Solution code:
# /usr/bin/python3
# -*- coding: utf-8 -*-
from queue import Queue
from queue import LifoQueue as Stack
from math import sqrt, floor, ceil, log2, log10, pi
from fractions import gcd
from itertools import permutations, combinations
from operator import itemgetter
from functools import cmp_to_key
from bisect import bisect_left


INF=1001001001
__MOD__=(10**9)+7
yn = 'YNeos'
judge = False
cnt = 0
ans = None


def lcm(a, b):
    return (a*b) // gcd(a,b)

def intinput():
    return int(input())

def mulinputs():
    return map(int,input().split())

def lineinputs(func=intinput):
    datas = []
    while True:
        try:
            datas.append(func())
        except EOFError:
            break
    return datas

def bindex(a,x):
    i = bisect_left(a,x)
    if i != len(a) and a[i] == x:
        return i
    return None

class ModInt():
    def __init__(self, x):
        self.__x = (x % __MOD__)
    def __add__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((self.__x + other.__x)%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((self.__x + other.__x)%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")
    def __radd__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((other.__x + self.__x)%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((other.__x + self.__x)%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")

    def __sub__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((self.__x - other.__x)%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((self.__x - other.__x)%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")
    def __rsub__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((other.__x - self.__x)%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((other.__x - self.__x)%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")

    def __mul__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((self.__x * other.__x)%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((self.__x * other.__x)%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")
    def __rmul__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((other.__x * self.__x)%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((other.__x * self.__x)%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")

    def __truediv__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((self.__x * other.__modinv())%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((self.__x * other.__modinv())%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")
    def __rtruediv__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((other.__x * self.__modinv())%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((other.__x * self.__modinv())%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")

    def __pow__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__(pow(self.__x, other.__x, __MOD__)))
        elif type(other) == ModInt:
            return self.__class__(pow(self.__x, other.__x, __MOD__))
        else:
            raise Exception("Not Int or Not ModInt")
    def __rpow__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__(pow(other.__x, self.__x, __MOD__)))
        elif type(other) == ModInt:
            return self.__class__(pow(other.__x, self.__x, __MOD__))
        else:
            raise Exception("Not Int or Not ModInt")

    def __modinv(self, m=__MOD__):
        a = self.__x
        if a == 0:
            raise ZeroDivisionError()
        if gcd(a, m) != 1:
            raise Exception("%sの逆数は求まりません。" % a)
        b, u, v = m, 1, 0
        while b != 0:
            t = a//b;
            a -= t*b
            a, b = b, a
            u -= t * v
            u, v = v, u
        u %= m
        if u < 0:
            u += m
        return u
    def __int__(self):
        return self.__x
    def __str__(self):
        return str(self.__x)



if __name__ == '__main__':
    X, K, D = mulinputs()
    X = abs(X)
    p = int(X//D)
    q = K - p
    r = X - (D*p)
    if q == 0:
        print(abs(r))
    elif q < 0:
        print(abs(X-(D*K)))
    elif q%2==0:
        print(abs(r))
    else:
        print(min(abs(r+D), abs(r-D)))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
X, K, D = map(int, input().split())
x = abs(X)
if X == 0:
    cnt = K
    if cnt % 2 == 0:
        ans = 0
    else:
        ans = D
elif x >= D:
    cnt = K - x//D
    if x- K*D >= 0:
        ans = x - K*D
    elif x - K*D < 0 and cnt % 2 == 0:
        ans = abs(x%D)
    else:
        ans = abs(D - x%D)
elif x < D:
    cnt = K
    if cnt % 2 == 0:
        ans = abs(x)
    else:
        ans = abs(x-D)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

    if x > 0:

        if x >= d:
            steps = min(k, x // d)
            k -= steps
            x -= steps * d

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

            return
    
    if x < 0:
        if  x <= -d:
            steps = min(k, -x // d)
            k -= steps
            x += steps * d

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

            return

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


    print(abs(x))

solve()