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:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil,pi,factorial
from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def SI(): return input().rstrip()
def printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
inf = 10**17
mod = 10**9 + 7

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

Submission status: Accepted

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

#とりあえず少しでも0に近づける試みをする。
tmp = abs(X)//D
if tmp >= K:
    print(X-D*K)
    exit()
K -= tmp
X -= D*tmp
if K%2 == 0:
    print(X)
else:
    print(abs(X-D))

Submission status: Accepted

Solution code:
nums = [int(e) for e in input().split()]
current = nums[0]
num = nums[1]
move = nums[2]


current = abs(current)

res = current // move

if res >= num:
    print(current - move*num)
else:
    current = current - move * res
    num -= res
    if num%2 == 0:
        print(current)
    else:
        print(abs(current-move))

Submission status: Accepted

Solution code:
x, k, d = map(int, input().split())
abs_x = abs(x)
kd = k * d 
ans = 0
if abs_x > kd:
  ans = abs_x - kd
else:
  div, mod = divmod(abs_x, d)
  ans = abs(mod - (k - div) % 2 * d)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
X,K,D = map(int,input().split())
X = abs(X)
#0またがない
if X > K*D:
    ans = X - K*D
#0またぐ
else:
    time = X//D + 1
    K = K - time
    X = X - time*D
    if K % 2 == 0:
        ans = abs(X)
    if K % 2 != 0:
        ans = X+D

print(ans)

Submission status: Accepted

Solution code:
import sys


def read_input_line():
    return sys.stdin.readline().strip()


def task(X, K, D):
    if_steps = abs(X) / D
    if K <= if_steps:
        return abs(X) - K * D
    over_steps_delta = (K - if_steps) % 2
    over_steps = round(if_steps + over_steps_delta)
    below_steps = over_steps - 2
    dist_a = abs(abs(X) - over_steps * D)
    dist = dist_a
    if below_steps >= 0:
        dist_b = abs(abs(X) - below_steps * D)
        if dist_b < dist:
            dist = dist_b
    return dist


def main():
    [X, K, D] = [int(s) for s in read_input_line().split(' ')]
    result = task(X, K, D)
    print(result)


main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from sys import stdin


def f(x, k, d):
    x = abs(x)
    if k == 0:
        return abs(x)

    tmp = x // d

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


if __name__ == '__main__':
    X, K, D = [int(x) for x in stdin.readline().rstrip().split()]
    print(f(X, K, D))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#
# abc175 c
#

import sys
from io import StringIO
import unittest


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def test_入力例_1(self):
        input = """6 2 4"""
        output = """2"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """7 4 3"""
        output = """1"""
        self.assertIO(input, output)

    def test_入力例_3(self):
        input = """10 1 2"""
        output = """8"""
        self.assertIO(input, output)

    def test_入力例_4(self):
        input = """1000000000000000 1000000000000000 1000000000000000"""
        output = """1000000000000000"""
        self.assertIO(input, output)


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


if __name__ == "__main__":
    # unittest.main()
    resolve()

Submission status: Accepted

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

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

if K % 2 == 1:
	X = D - X

print(X)

Submission status: Accepted

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

Submission status: Accepted

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

if mx<=K:
    x = X-(mx-1)*D
    count -= mx-1
else:
    x = X-(K-1)*D
    count -= K-1

ans = abs(x)
while x>0 and count>0:
    x -= D
    count -= 1
    if abs(x)<ans and count%2==0:
        ans = abs(x)

print(ans)

Submission status: Accepted

Solution code:
from sys import stdin


def f(x, k, d):
    if k == 0:
        return x

    absX = abs(x)
    tmp = absX // d

    if tmp > k:
        return absX - d * k
    else:
        absX = absX - d * tmp
        k = k - tmp
        if k % 2 == 0:
            return absX
        else:
            return abs(absX - d)


if __name__ == '__main__':
    X, K, D = [int(x) for x in stdin.readline().rstrip().split()]
    print(f(X, K, D))

Submission status: Accepted

Solution code:
X,K,D = map(int,input().split())
# 原点に近い位置までの移動回数を求める
count = abs(X) // D
ans = 0
# Kの方が小さければ可能な限り原点に近い位置を求める
if count>K:
    ans = abs(X) - D * K
else:
	# 残りの移動回数が偶数
    if (K-count)%2==0:
        ans = abs(X) % D
    # 残りの移動回数が奇数
    else:
        ans = abs(abs(X) % D - D)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

if X == 0:
    if K % 2 == 0:
        print(0)
    else:
        print(abs(D))
    exit(0)
X = abs(X)
if X > 0:
    k = X - K * D
    if k >= 0:
        print(k)
    else:
        kouho1 = X % D
        kouho2 = abs(kouho1 - D)
        sho = X // D
        if (K - sho) % 2 == 0:
            print(kouho1)
        else:
            print(kouho2)
    exit(0)
if X < 0:
    k = X + K * D
    if k <= 0:
        print(k)
    else:
        kouho1 = (-X) % D
        kouho2 = abs(kouho1 - D)
        sho = (-X) // D
        if (K - sho) % 2 == 0:
            print(kouho1)
        else:
            print(kouho2)
    exit(0)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from sys import exit, stdin

# import copy
# from collections import deque,Counter
# import numpy as np
input = stdin.readline

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

if abs(X) >= K * D:
    print((abs(X) - K * D))
else:
    A1 = abs(X) % D
    A2 = abs(A1 - D)
    n = int((abs(X) - A1)/D)
    if (K - n) % 2 == 0:
        print(A1)
    else:
        print(A2)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
i = list(map(int, input().split()))
x=(abs(i[0]))

if x>=i[1]*i[2]:
    print(x-i[1]*i[2])
else:
    if (i[1]-x//i[2])%2 == 0:
        print(x%i[2])
    else:
        print(i[2]-x%i[2])

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
# AtCoder Beginner Contest 175
# C - Walking Takahashi

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

X=abs(X)

A=X//D
M=X%D

if K<=A:
    print(X-K*D)
    exit()

else :
    if A%2==K%2:
        print(M)
    else :
        print(min(abs(M+D),abs(M-D)))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#import sys
#import numpy as np
import math
#from fractions import Fraction
import itertools
from collections import deque
from collections import Counter
import heapq
from fractions  import gcd
#input=sys.stdin.readline
#import bisect
x,k,d=map(int,input().split())
if x<0 and x+d*k<=0:
    print(abs(x+d*k))
elif x>0 and x-d*k>=0:
    print(x-d*k)
elif x<0:
    i=math.ceil((-1*x)/d)-1
    if (k-(i+1))%2==0:
        print(x+d*(i+1))
    else:
        print(abs(x+d*i))
elif x>0:
    i=math.ceil(x/d)-1
    if (k-(i+1))%2==0:
        print(abs(x-d*(i+1)))
    else:
        print(x-d*i)
elif x==0:
    if k%2==0:
        print(0)
    else:
        print(d)

Submission status: Accepted

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

if(x < 0):
  i = int(abs(x)/d)
  if(k > i):
    k = k - i
    x = x + d*i
  else:
    x = x + d*k
    k = 0
  if(abs(x) > abs(x - d)):
    k += 1
    x = x - d
elif(x > 0):
  i = int(abs(x)/d)
  if(k > i):
    k = k - i
    x = x - d*i
  else:
    x = x - d*k
    k = 0
  if(abs(x) > abs(x + d)):
    k += 1
    x = x + d

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

Submission status: Accepted

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

print(ans)

Submission status: Accepted

Solution code:
import numpy as np

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

X = int(X)
if X < 0:
    X = -X
K = int(K)
D = int(D)

# KD, (K - 2)D, (K - 4)D, ...

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
[X,K,D]=[int(_) for _ in input().split()]
X=abs(X)
Y=X//D
if Y <= K:
  X-=Y*D
  if (K-Y)%2 != 0:
    X = abs(X-D) if abs(X-D) < abs(X+D) else abs(X+D)
else:
  X-=K*D
print(X)

Submission status: Accepted

Solution code:
X,K,D = map(int,input().split())
# 原点に近い位置までの移動回数を求める
count = abs(X) // D
ans = 0
if count>K:
    ans = abs(X) - D * K
else:
    if (K-count)%2==0:
        ans = abs(X) % D
    else:
        ans = abs(abs(X) % D - D)
print(ans)

Submission status: Accepted

Solution code:
a,b,c=map(int,input().split())
a1=(abs(a)-0)%c
if(a==0):
    if(b%2==0):
        print(0)
    else:
        print(c)
if(a>0):
    a2=a1-c
    ma1=int(a/c)
    ma2=ma1+1
    a3=abs(a2)
    if(b>=ma2 and b>ma1):
        if((b-ma1)%2==0):
            print(a1)
        else:
            print(abs(a2))
    if(b==ma1):
        print(a1)
    if(b<ma1):
        print(a-(b*c))
if(a<0):
    a2=abs(a1-c)
    ma1=((a1-(2*a1))-(a))
    ma1=int(ma1/c)
    ma2=ma1+1
    if(b>=ma2 and b>ma1):
        if((b-ma1)%2==0):
            print(a1)
        else:
            print(abs(a2))
    if(b==ma1):
        print(a1)
    if(b<ma1):
        print(abs(a+(b*c)))

Submission status: Accepted

Solution code:
from collections import defaultdict,deque
import math
import copy
def main():
    x,k,d=map(int, input().split())
    res = float('inf')
    x = abs(x)

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




if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
# ####################################################################
# import io
# import sys

# _INPUT = """\
# 100 12 9
# """
# sys.stdin = io.StringIO(_INPUT)
####################################################################
####################################################################
import sys
def _p(*_a):
  #return
  _s=" ".join(map(str,_a))
  #print(_s)
  sys.stderr.write(_s+"\n")
####################################################################
def _pa(_aa):
  #return
  for _a in _aa: _p(_a)
####################################################################
X,K,D = map(int, input().split())

X = abs(X)

# K回移動しても、0に届かない場合
if K * D < X:
  ans = X - (K*D)
  print(ans)
  exit()


# 0に届く場合
a = X // D
k = K - a
x = X - (a * D)
# _p(a,k,x)
if k % 2 == 0:
  ans = x
else:
  ans = x - D

print(abs(ans))

Submission status: Accepted

Solution code:
import sys
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int,sys.stdin.readline().rstrip().split())

X,K,D = MI()
X = abs(X)

q = X//D
r = X % D

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

print(r if (q-K) % 2 == 0 else D-r)

Submission status: Accepted

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

x = abs(x)

ans = 0

t = x // d
s = x % d

if abs(x) > k * d:
    print(abs(x) - k * d)
elif t % 2 == k % 2:
    print(s)
else:
    print(d - s)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys
X, K, D = map(int, input().split())
X = abs(X)
if K*D <= X:
    print(X-K*D)
else:
    num1 = X%D
    num2 = D - num1
    if ((X-num1)//D)%2 == K%2:
        print(num1)
    else:
        print(num2)

Submission status: Accepted

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

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



if __name__ == "__main__":
    solve()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

t = abs(X) // D
s = abs(X) % D

if abs(X) > K*D:
    print(abs(X) - K*D)
elif t%2 == K%2:
    print(s)
else:
    print(D-s)

Submission status: Accepted

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

if X == 0:
    if K % 2 == 0:
        print(0)
    else:
        print(abs(D))
    exit(0)
X = abs(X)
if X > 0:
    k = X - K * D
    if k >= 0:
        print(k)
    else:
        kouho1 = X % D
        kouho2 = abs(kouho1 - D)
        sho = X // D
        if (K - sho) % 2 == 0:
            print(kouho1)
        else:
            print(kouho2)
    exit(0)

Submission status: Accepted

Solution code:
X,K,D = [int(x) for x in input().split()]

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

Submission status: Accepted

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

x = abs(x)

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

Submission status: Accepted

Solution code:
from math import floor
import decimal
x,k,d=map(int,input().split())
if x==0:
	if k%2==0:
		print(0)
	else:
		print(d)
	exit(0)
t=decimal.Decimal(abs(x))//decimal.Decimal(d)
if (k<t):
	if (x>0):
		print(abs(x-k*d))
	else:
		print(abs(x+k*d))
else:
	if (x>0):
		x=x-t*d
		k-=t
		if k%2==0:
			print(abs(x))
		else:
			print(abs(x-d))
	else:
		x=x+t*d
		k-=t
		if k%2==0:
			print(abs(x))
		else:
			print(abs(x+d))

Submission status: Accepted

Solution code:
# AtCoder Beginner Contest 175
# C - Walking Takahashi

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

KOE=K%2

A=abs(X)//D
M=abs(X)%D

# print(A)

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

else :
    if A%2==KOE:
        print(M)
    else :
        print(min(abs(M+D),abs(M-D)))

Submission status: Accepted

Solution code:
def f(v, x, d, k):
  return x + d * (2 * v - k)


def swap(x, y):
  r = x
  x = y
  y = r


lt = input().split(' ')
x = int(lt[0])
k = int(lt[1])
d = int(lt[2])

lf = 1
r = k

# for i in range(k + 1):
#   print(f(i, x, d, k))

ans = abs(f(0, x, d, k))
# ans = min(ans, abs(x))

while (lf <= r):
  mid = (lf + r) // 2
  nt = f(mid, x, d, k)
  cur = f(mid - 1, x, d, k)
  # print('mid = ', mid)
  # cur = abs(cur)
  # nt = abs(nt)
  # swap(nt, cur)
  # print('cur = ', cur)
  # print('nt = ', nt)
  # print('')
  nt = abs(nt)
  cur = abs(cur)
  if (nt > cur):
    ans = min(ans, abs(cur))
    r = mid - 1
  else:
    lf = mid + 1
    ans = min(ans, abs(nt))

print(abs(ans))

Submission status: Accepted

Solution code:
X,K,D = list(map(int, input().split()))
co,ans = divmod(abs(X),D)
if (K - co) > 0:
    if (K - co) % 2 == 0:
        pass
    else:
        ans = abs(ans) - D
else:
    ans = abs(X) - K*D

print(abs(ans))

Submission status: Accepted

Solution code:
x, k, d = map(int,input().split())
if x < 0: x = -x
mv = x // d
if mv >= k:
    # なるべく0に近づけば良い
    print(abs(x)-d*k)
else:
    nearest = x % d
    if (k - mv) % 2 == 1:
        print(abs(nearest - d))
    else:
        print(nearest)

Submission status: Accepted

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

ans = abs(x) % d
cal = abs(x) // d
if ans > abs(ans - d):
    cal += 1
    ans = abs(ans - d)

if cal > k:
    ans = abs(x) - k*d
else:
    if (k - cal) % 2 == 1:
        ans = min(abs(ans-d),abs(ans+d))

print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
x,k,d=map(int,input().split())
m=abs(x)
n=m//d
j=k-n
if j>0:
  if x<0:
    if j%2==0:
      x+=d*n
    else:
      x+=d*(n+1)
  else:
    if j%2==0:
      x-=d*n
    else:
      x-=d*(n+1)
else:
  if x<0:
    x+=d*k
  else:
    x-=d*k
print(abs(x))

Submission status: Accepted

Solution code:
from sys import stdin
input = stdin.readline

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

X = abs(X)

m = X % D
l = X // D

if l > K:
    print(abs(X) - D * K)
else:
    if (K - l) % 2 == 0:
        print(m)
    else:
        print(D - m)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
a,b,c=map(int,input().split())
a=abs(a)
temp=a//c
if(temp>b):
    temp=temp-b
    if(a%c==0):
        if(temp>b):
            print(a-(b*c))
        else:
            print(temp*c)
    else:
        print(a-(b*c))
else:
    if(temp==b):
        if(a%c==0):
            print(0)
        else:
            print(a%c)
    else:
        if(a%c==0):
            b=b-temp;
            if(b%2==0):
                print(0)
            else:
                print(c)
        else:
            b=b-temp;
            if(b%2==0):
                print(a%c)
            else:
                t1=(a%c)-c
                t2=(a%c)+c
                if(abs(t1)>abs(t2)):
                    print(abs(t2))
                else:
                    print(abs(t1))

Submission status: Accepted

Solution code:
#!python3.8
# -*- coding: utf-8 -*-
# abc175/abc175_c
import sys
import math

def s2nn(s): return [int(c) for c in s.split(' ')]
def ss2nn(ss): return [int(s) for s in list(ss)]
def ss2nnn(ss): return [s2nn(s) for s in list(ss)]
def i2s(): return sys.stdin.readline().rstrip()
def i2n(): return int(i2s())
def i2nn(): return s2nn(i2s())
def ii2ss(n): return [i2s() for _ in range(n)]
def ii2nn(n): return ss2nn(ii2ss(n))
def ii2nnn(n): return ss2nnn(ii2ss(n))


def main():
    X, K, D = i2nn()
    absX = abs(X)
    if absX >= K * D:
        print(abs(absX - K * D))
        return
    a, c = divmod(absX, D)
    b = K - a
    if b % 2:
        print(abs(c - D))
    else:
        print(abs(c))
    return


main()

Submission status: Accepted

Solution code:

import sys
input = sys.stdin.readline
# sys.setrecursionlimit(100000)


def main():
    x, k, d = [int(i) for i in input().strip().split()]
    div = x // d
    L = x - d * k
    R = x + d * k
    if L >= 0:
        print(L)
        return
    if R < 0:
        print(-R)
        return
    if k <= div:
        ans = x - (d * k)
    else:
        k = k - div
        even = x % d
        odd = abs(d - even)
        if k % 2 == 1:
            ans = odd
        else:
            ans = even
    print(ans)
    return


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
X, K, D = [int(i) for i in input().split()]
# K = int(input())
X = abs(X)
rem = X % D
count = (X - rem) // D
if count <= K:
  if (K - count) % 2 == 0:
    print(rem)
  else:
    print(abs(rem-D))
else:
  print(X - D * K)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
#!python3.8
# -*- coding: utf-8 -*-
# abc175/abc175_c
import sys
import math

def s2nn(s): return [int(c) for c in s.split(' ')]
def ss2nn(ss): return [int(s) for s in list(ss)]
def ss2nnn(ss): return [s2nn(s) for s in list(ss)]
def i2s(): return sys.stdin.readline().rstrip()
def i2n(): return int(i2s())
def i2nn(): return s2nn(i2s())
def ii2ss(n): return [i2s() for _ in range(n)]
def ii2nn(n): return ss2nn(ii2ss(n))
def ii2nnn(n): return ss2nnn(ii2ss(n))


def main():
    X, K, D = i2nn()
    absX = abs(X)
    if absX >= K * D:
        print(abs(absX - K * D))
        return
    try:
        a, c = divmod(absX, D)
    except:
        print(X)
        return
    b = K - a
    if b % 2:
        print(abs(c - D))
    else:
        print(abs(c))
    return


main()

Submission status: Accepted

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

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

a = x//d
xd = x%d
gk = [0, 0]
if a%2 == 1:
    gk[1] = xd
    gk[0] = abs(xd-d)
else:
    gk[1] = abs(xd-d)
    gk[0] = xd

if k%2 == 0:
    print(gk[0])
else:
    print(gk[1])

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
X,K,D=map(int,input().split())

X=abs(X)
ii=int(X/D)
if K<=ii:
    X-=K*D
else:
    X-=ii*D
    if (K-ii)%2==1:
        X-=D

print(abs(X))

Submission status: Accepted

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

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

else:
    cent = x % d
    num = x // d

if d / 2 > cent and cent != 0:
    cent = d - cent
    num += 1

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

rem = k - num

if rem % 2 == 0:
    print(cent)
    exit(0)
else:
    print(d - cent)

Submission status: Accepted

Solution code:
import sys




x,k,d=map(int,input().split())
x=abs(x)
if(abs(x%d)<abs(x%d-d)):
  min_num=x%d
  min_num2=x%d-d
else:
  min_num=x%d-d
  min_num2=x%d

kaisuu=(x-min_num)/d
if(kaisuu>k):
  print(abs(x-d*k))
  sys.exit()
if((k%2==0 and kaisuu%2==0)or(k%2==1 and kaisuu%2==1)):
  print(abs(min_num))
else:
  print(abs(min_num2))

Submission status: Accepted

Solution code:
import sys
def IL(): return map(int,sys.stdin.readline().rstrip().split())

def solve():
    global x,k,d
    i = min(k,abs(x//d))
    if abs(x-d*i)<abs(x+d*i):
        x -= d*i
    else:
        x += d*i
    k -= i
    if k%2==1:
        if abs(x-d)<abs(x+d):
            print(abs(x-d))
        else:
            print(abs(x+d))
    else:
        print(abs(x))
    return

if __name__=='__main__':
    x,k,d = IL()
    solve()

Submission status: Accepted

Solution code:
import numpy as np

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

num = int(abs(x)/d)
ans = 0

if move <= num:
    if np.sign(x) == 1:
        ans = x-d*move
    else:
        ans = x+d*move

elif (move-num)%2==0:
    if np.sign(x) == 1:
        ans = x-d*num
    else:
        ans = x+d*num

elif (move-num)%2!=0:
    if np.sign(x) == 1:
        ans = x-d*(num+1)
    else:
        ans = x+d*(num+1)


print(abs(ans))

Submission status: Accepted

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

Submission status: Accepted

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

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