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:
#from statistics import median
#import collections
#aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0]
from fractions import gcd
from itertools import combinations,permutations,accumulate, product # (string,3) 3回
#from collections import deque
from collections import deque,defaultdict,Counter
import decimal
import re
#import bisect
#
#    d = m - k[i] - k[j]
#    if kk[bisect.bisect_right(kk,d) - 1] == d:
#
#
#
# pythonで無理なときは、pypyでやると正解するかも！！
#
#
# my_round_int = lambda x:np.round((x*2 + 1)//2)
# 四捨五入g
import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7
#mod = 9982443453
def readInts():
  return list(map(int,input().split()))
def I():
  return int(input())
x,k,d = readInts()
ikeru = abs(x)//d
# 0 の時も忘れないでね～
#print(ikeru)
if ikeru >= k:
    if x > 0:
        print(abs(x - k*d))
    elif x < 0:
        print(abs(x + (k*d)))
else:
    # ikeru <= k
    if ikeru%2 == k%2:
        if x > 0:
            print(x - (ikeru*d))
        else:
            print(abs(x+(ikeru*d)))
    else:
        if x > 0:
            print(abs(x - (ikeru*d) - d))
        else:
            print(abs((abs(x+(ikeru*d)) - d)))

Submission status: Accepted

Solution code:
#import sys
MOD = 10 ** 9 + 7
INFI = 10**10
#input = sys.stdin.readline
import math
from collections import deque
import itertools
import heapq
#import bisect
from fractions import Fraction
import copy
from functools import lru_cache
from collections import defaultdict
import pprint

#oo=list("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
# ko=list("abcdefghijklmnopqrstuvwxyz")

def sosuhante(n):
    for k in range(2, int(math.sqrt(n))+1):
        if n% k ==0:
            return False
    return True
def cmb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result

def kingaku(a,b,n):
    keta=len(str(n))
    return a*n+b*keta

def my_index(l, x, default=False):
    if x in l:
        return l.index(x)
    else:
        return default

#    h,w,a,b = map(int, input().split())
#    c = [[0 for j in range(n)] for i in range(n)]

def ret(a):
    c=[None]*(len(a)-1)
    if len(a)==1:
        return a[0]
    elif len(a)==0:
        return 0
    for i in range(1,len(a)):
        c[i-1]=abs(a[i]-a[i-1])
    return ret(c)

def soinsubunkai(n):
    a = []
    i = 1
    while i*i <= n:
        if n % i == 0 and i!=1:
            a.append(i)
            n=n//i

        if n% i !=0 or i==1:
            i += 1
    nokori=[n]
    return a + nokori

def make_divisors(n):
    lower_divisors , upper_divisors = [], []
    i = 1
    while i*i <= n:
        if n % i == 0:
            lower_divisors.append(i)
            if i != n // i:
                upper_divisors.append(n//i)
        i += 1
    return lower_divisors + upper_divisors[::-1]

def main():
    #l,r,d=map(int,input().split())
    x,k,d = map(int, input().split())
    tempkai=abs(x)//d
    ans=INFI
    if tempkai>=k:
        ans=abs(x)-d*k
    else:
        if (k-tempkai)%2==0:
            ans=abs(x)-d*tempkai
        else:
            temp=abs(x)-d*tempkai
            temp2=abs(temp-d)
            temp3=abs(temp+d)
            ans=min(temp3,temp2)
    print(ans)

if __name__ == "__main__":

    main()

Submission status: Accepted

Solution code:
X, K, D = map(int, input().split())
X = abs(X)
if X // D > K:
    result = X - D*K
else:
    n = X//D
    rem_jump = K - n
    p1 = X % D
    p2 = D - p1

    if rem_jump % 2 == 0:
        result = p1
    else:
        result = p2

print(result)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

import sys

input = iter(sys.stdin.read().splitlines()).__next__

def solve(X, K, D):
   if X < 0:
      X = -X
   if X - K*D >= 0:
      return X - K*D
   k = X // D
   X -= k*D
   assert X >= 0
   K -= k
   assert K > 0

   K %= 2
   if K == 1:
      X -= D
   if X < 0:
      X = -X
   return X

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

res = solve(X, K, D)
print(res)

Submission status: Accepted

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

ab = abs(x)//d
ab_1 = ab +1

koho1 = abs(x) - abs(d*ab)
koho2 = abs(abs(x) -abs(d*ab_1))

tmp = k-ab

koho_s = []

if(tmp > 0):
    if(tmp%2 == 0):
        ans = koho1
    else:
        ans = koho2
elif(tmp == 0):
    ans = koho1
else:
    ans = abs(x) - abs(d*k)

print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import collections as cl
import sys


def II():
    return int(sys.stdin.readline())


def MI():
    return map(int, sys.stdin.readline().split())


def LI():
    return list(map(int, sys.stdin.readline().split()))


def main():
    x, k, d = MI()
    x = abs(x)

    min_idou = x // d
    amari = x % d

    if min_idou >= k:
        print(min(abs(x - k*d), x))
        return

    k -= min_idou
    if k % 2 == 0:
        print(amari)
    else:
        print(-1*(amari - d))


main()

Submission status: Accepted

Solution code:
import sys
import heapq, functools, collections
import math, random
from collections import Counter, defaultdict

# available on Google, not available on Codeforces
# import numpy as np
# import scipy


def solve(x,k,d):  # fix inputs here
    console("----- solving ------")
    console(x,k,d)

    x = abs(x)

    if k*d <= x:
        return x - k*d

    remainder = x%d
    moves = (x - remainder) // d

    leftover_moves = (k - moves) % 2

    console(remainder, moves, leftover_moves)

    return min(abs(remainder + leftover_moves*d), abs(remainder - leftover_moves*d))


def console(*args):  # the judge will not read these print statement
    print('\033[36m', *args, '\033[0m', file=sys.stderr)
    return

# fast read all
# sys.stdin.readlines()

for case_num in [1]:
    # read line as a string
    # strr = input()

    # read line as an integer
    # k = int(input())
    
    # read one line and parse each word as a string
    # lst = input().split()

    # read one line and parse each word as an integer
    x,k,d = list(map(int,input().split()))

    # read matrix and parse as integers (after reading read nrows)
    # lst = list(map(int,input().split()))
    # nrows = lst[0]  # index containing information, please change
    # grid = []
    # for _ in range(nrows):
    #     grid.append(list(map(int,input().split())))

    res = solve(x,k,d)  # please change
    
    # Google - case number required
    # print("Case #{}: {}".format(case_num+1, res))

    # Codeforces - no case number required
    print(res)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
from functools import reduce
from fractions import gcd
import math
import bisect
import itertools
import sys
input = sys.stdin.readline
INF = float("inf")


# 処理内容
def main():
    X, K, D = map(int, input().split())
    
    if abs(X) - K*D >= 0:
        print(abs(X) - K*D)
    else:
        L = K*D - abs(X)
        m = L // D
        if m % 2 == 0:
            print(L - m*D)
        else:
            print((m+1)*D - L)



if __name__ == '__main__':
    main()

Submission status: Accepted

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

def ceil(a, b):
	return -(-a//b)

mint = abs(x)//d
targ1 = min(abs(x-d*mint), abs(x+d*mint))
targ2 = min(abs(targ1-d), abs(targ1+d))

if mint >= k:
	print(min(abs(x-k*d), abs(x+k*d)))
	exit()
else:
	targ3 = k-mint
	if targ3 % 2 == 0:
		print(targ1)
	else:
		print(targ2)

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)-a*d)
  else:
    print(abs(abs(x)-(a+1)*d))

Submission status: Accepted

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

a = (x+ d*k)//(2*d)
b = a+1

if abs(x+d*k-2*d*a) < abs(x+d*k-2*d*b):
    best = a
else:
    best = b

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

Submission status: Accepted

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

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

Submission status: Accepted

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

l, r, d1 = -k, k, k + 1
while (l <= r):
    mid = (l + r) // 2
    if (x + mid * d >= 0):
        d1 = mid 
        r = mid - 1
    else:
        l = mid + 1

l, r, d2 = -k, k, k + 1
while (l <= r):
    mid = (l + r) // 2
    if (x - mid * d < 0):
        d2 = mid 
        l = mid + 1
    else:
        r = mid - 1

ans = int(1e30)
if (d1 != k + 1):
    r = (k - abs(d1)) % 2
    if (r == 0):
        ans = min(ans, abs(x + d * (d1 + r)))
    else:
        ans = min(ans, abs(x + d * (d1 - 1)))
        ans = min(ans, abs(x + d * (d1 + 1)))
if (d2 != k + 1):
    r = (k - abs(d2)) % 2
    if (r == 0):
        ans = min(ans, abs(x + d * (d2 + r)))
    else:
        ans = min(ans, abs(x + d * (d2 - 1)))
        ans = min(ans, abs(x + d * (d2 + 1)))


print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
x,k,d = list(map(int, input().strip().split()))
x=abs(x)
y=x//d
if y>=k:
    print(x-k*d)
else:
    z=k-y
    w=x%d
    if w>=d-w:
        if (z-1)%2==0:
            print(d-w)
        else:
            print(w)
    else:
        if z%2==0:
            print(w)
        else:
            print(d-w)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
# C

import sys
from collections import defaultdict, deque
from heapq import heappush, heappop
import math
import bisect
input = sys.stdin.readline

# 再起回数上限変更
# sys.setrecursionlimit(1000000)

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

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

n = x // d
x = x - n * d
k -= n

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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

def main(X, K, D):
    L = X - D * K
    R = X + D * K
    if L >= 0:
        return L
    if R < 0:
        return -R
    t = (-L) // (D + D)
    ans = -L
    for i in range(t - 10, t + 10):
        x = L + (D + D) * i
        if L <= x <= R:
            ans = min(ans, abs(x))
    return ans

print(main(X, K, D))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
import  math
import fractions
from collections import defaultdict
import copy
stdin = sys.stdin
     
ns = lambda: stdin.readline().rstrip()
ni = lambda: int(stdin.readline().rstrip())
nm = lambda: map(int, stdin.readline().split())
nl = lambda: list(map(int, stdin.readline().split()))

X,K,D=nm()
X=abs(X)
if(K*D<=X):
    print(X-K*D)
    sys.exit(0)
else:
    tmp=X%D
    tmp2=K-(X//D)
    if(tmp2%2==0):
        print(tmp)
        sys.exit(0)
    else:
        print(D-tmp)
        sys.exit(0)

Submission status: Accepted

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

if k*d <= x:
    res = x - k*d
else:
    temp = x // d
    temp_x = x - temp*d
    temp_k = k - temp
    
    if temp_k%2 == 0:
        res = temp_x
    else:
        res = abs(temp_x - d)
print(res)

Submission status: Accepted

Solution code:
from sys import stdin
nii=lambda:map(int,stdin.readline().split())
lnii=lambda:list(map(int,stdin.readline().split()))

x,k,d=nii()

dist=abs(x)

num=dist//d

if num>k:
  print(abs(dist-d*k))
  exit()
else:
  k-=num
  dist1=dist-d*num

  if k>=1:
    dist2=abs(dist1-d)
    if dist2<=dist1:
      k-=1
      dist1=dist2

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

Submission status: Accepted

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

if -d*k+x < 0:
    t = d*k-x
    p = t//(2*d)
    l = []
    #print(p)
    for i in range(-1, 2):
        s = p+i
        if 0 <= s <= k:
            #print(p+i, abs(2*d*s-d*k+x))
            l.append(abs(2*d*s-d*k+x))
    if len(l) > 0:
        ans = min(l)
    else:
        ans = abs(2*d*k-d*k+x)

else:
    ans = -d*k+x
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
X, K, D = [int(n) for n in input().split()]
if X < 0:
    X = -X

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