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 math

# X, K, D = 6, 2, 4

# X, K, D = 7, 4, 3

# X, K, D = 10, 1, 2


# X, K, D = 1000000000000000, 1000000000000000, 1000000000000000

# X, K, D = 4, 4, 4


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

def calculate(x, k, d):
    x = abs(x)
    d = abs(d)
    n = x / d
    kk = math.floor(n)

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

    k = k - kk

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


result = calculate(X, K, D)

print(result)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(1000000000)
import math
from math import gcd
def lcm(a, b): return a * b // gcd(a, b)
from itertools import count, permutations, combinations, chain, product
from functools import lru_cache
from collections import deque, defaultdict
from operator import itemgetter
from pprint import pprint
ii = lambda: int(input())
mis = lambda: map(int, input().split())
lmis = lambda: list(mis())
INF = float('inf')
N1097 = 10**9 + 7
DEBUG = sys.argv[-1]!='ONLINE_JUDGE'

def meg(f, ok, ng):
    while abs(ok-ng)>1:
        mid = (ok+ng)//2
        if f(mid):
            ok=mid
        else:
            ng=mid
    return ok

def get_inv(n, modp):
    return pow(n, modp-2, modp)

def factorials_list(n, modp):    # 10**6
    fs = [1]
    for i in range(1, n+1):
        fs.append(fs[-1] * i % modp)
    return fs

def invs_list(n, fs, modp):     # 10**6
    invs = [get_inv(fs[-1], modp)]
    for i in range(n, 1-1, -1):
        invs.append(invs[-1] * i % modp)
    invs.reverse()
    return invs

def comb(n, k, modp):
    num = 1
    for i in range(n, n-k, -1):
        num = num * i % modp
    den = 1
    for i in range(2, k+1):
        den = den * i % modp
    return num * get_inv(den, modp) % modp

def comb_from_list(n, k, modp, fs, invs):
    return fs[n] * invs[n-k] * invs[k] % modp

#

class UnionFindEx:
    def __init__(self, size):
        #正なら根の番号、負ならグループサイズ
        self.roots = [-1] * size
    def getRootID(self, i):
        r = self.roots[i]
        if r < 0:   #負なら根
            return i
        else:
            r = self.getRootID(r)
            self.roots[i] = r
            return r
    def getGroupSize(self, i):
        return -self.roots[self.getRootID(i)]
    def connect(self, i, j):
        r1, r2 = self.getRootID(i), self.getRootID(j)
        if r1 == r2:
            return False
        if self.getGroupSize(r1) < self.getGroupSize(r2):
            r1, r2 = r2, r1
        self.roots[r1] += self.roots[r2]    #サイズ更新
        self.roots[r2] = r1
        return True

Yes = 'Yes'
No = 'No'

def dprint(*args, **kwargs):
    pass
    # if DEBUG:
    #     print(*args, **kwargs)

def main():
    X,K,D=mis()
    X = abs(X)
    ZP = X%D
    if abs(X - ZP)//D > K:
        dprint('too far')
        print(X - D*K)
        return
    ZN = ZP - D
    X, K = ZP, K - abs(X - ZP)//D
    if K&1:
        dprint('odd')
        print(abs(ZN))
    else:
        dprint('even')
        print(abs(ZP))


main()

Submission status: Accepted

Solution code:
def check():
    X, K, D = list(map(int, input().split(' ')))
    
    if K*D <= abs(X):
        if X > 0:
            return X - K*D
        else:
            return X + K*D
            
    N = abs(X) // D
    signX = -1 if X < 0 else 1
    X = X * signX
    
    nearest_point = X - N*D
    
    if (K-N) % 2 == 0:
        return nearest_point
    else:
        return nearest_point - D

print(abs(check()))

Submission status: Accepted

Solution code:
import sys,math
try:sys.stdin,sys.stdout=open('input.txt','r'),open('out.txt','w')
except:pass
from sys import stdin,stdout;mod=int(1e9 + 7);from statistics import mode
from collections import *;from math import ceil,floor,inf,factorial,gcd,log2,sqrt,log
ii1=lambda:int(stdin.readline().strip())
is1=lambda:stdin.readline().strip()
iia=lambda:list(map(int,stdin.readline().strip().split()))
isa=lambda:stdin.readline().strip().split()
# print('{:.3f}'.format(1),round(1.123456789,4))
# sys.setrecursionlimit(500000)
def lcm(a,b): return (a*b)//gcd(a,b)
def setbits(n):return bin(n).count('1')
def resetbits(n):return bin(n).count('0')
def modinv(n,p):return pow(n,p-2,p)
def ncr(n,r):
    num,den=1,1;r=min(n,n-r)
    for i in range(r):num*=(n-i);den*=(i+1)
    return num//den
def ncr_p(n, r, p):
    num,den=1,1;r=min(r,n-r)
    for i in range(r):num = (num * (n - i)) % p ;den = (den * (i + 1)) % p
    return (num * modinv(den,p)) % p
def isPrime(num) :
    if num<=1:return False
    if num==2 or n==3:return True
    if (num % 2 == 0 or num % 3 == 0) :return False
    m = int(num**0.5)+1
    for i in range(5,m,6):
        if (num % i == 0 or num % (i + 2) == 0) :return False
    return True
def bin_search(arr, low, high, val):
    while low <= high:
        mid = low + (high - low) // 2;
        if arr[mid] == val:return mid
        elif arr[mid] < val:low = mid + 1
        else:high = mid - 1
    return -1
def sumofdigit(num):
    count=0;
    while num : count+=num % 10;num //= 10;
    return count;
def inputmatrix():
    r,c=iia();mat=[0]*r;
    for i in range(r):mat[i]=iia();
    return r,c,mat;
def prefix_sum(n,arr):
    for i in range(1,n):arr[i]+=arr[i-1]
    return arr;
def binomial(n, k):
    if 0 <= k <= n:
        ntok = 1;ktok = 1
        for t in range(1, min(k, n - k) + 1):ntok *= n;ktok *= t;n -= 1
        return ntok // ktok
    else:return 0
def divisors(n):
    res = [];
    for i in range(1,ceil(sqrt(n))+1):
        if n%i == 0:
            if i==n//i:res.append(i)
            else:res.append(i);res.append(n//i)
    return res;
# code start from here-
# for i in range(10):
x,k,d=iia()

xx=abs(x)

if k*d<=xx:
    ans1=abs(xx-k*d )
    print(ans1)
else:
    ans1,ans2=0,0
    step=xx//d
    rs=k-step

    f=abs(xx-step*d)
    s=abs(xx-step*d-d)
    if rs%2==0:
        a=f
    else:
        a=s
    print(a)

Submission status: Accepted

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

Submission status: Accepted

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

x = abs(x)

inf = float('INF')

if x - k*d >= 0:
    ans = x - k * d

else:
    temp = x//d
    if (k-temp) % 2 == 0:
        ans = x - temp * d
    else:
        ans = x - (temp+1) * d

ans = abs(ans)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

        print(abs(X%D))
    else:
        print(abs(X%D-D))

Submission status: Accepted

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

if least>=K:
    ans = X - K*D
elif (K-least)%2==0:
    ans = X - least*D
elif (K-least)%2==1:
    ans = -1*(X-(least+1)*D)

print(ans)

Submission status: Accepted

Solution code:
def main(X, K, D):
    x = abs(X)
    k = K

    shou, amari = x // D, x % D
    if shou >= k:   # 0付近までたどり着かないとき
        x -= k * D
        k = 0
    else:
        k -= (shou + 1)
        x -= (shou + 1) * D

    if k % 2:    # 奇数のとき
        print(abs(x + D))
    else:   # 偶数の時
        print(abs(x))

if __name__ == '__main__':
    X, K, D = list(map(int, input().split()))
    main(X, K, D)

Submission status: Accepted

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

a = input().split()
x = abs(int(a[0]))
count = int(a[1])
d = int(a[2])

num = x // d
if abs(x - d * num) <= abs(x - d * (num + 1)):
  num += 1
else:
  num = num
  
if count == num:
  mini = abs(x - d * num)
elif count > num:
  count -= num
  if count % 2 == 0:
    mini = abs(x - d * num)
  else:
    a = abs(abs(x - d * num) - d)
    b = abs(abs(x - d * num) + d)
    if a >= b:
      mini = b
    else:
      mini = a
else:
  mini = abs(x - d * count)
print(mini)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, gcd
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from heapq import heappush, heappop
from functools import reduce, lru_cache
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def TUPLE(): return tuple(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7 
#mod = 998244353
from decimal import *
#import numpy as np
#decimal.getcontext().prec = 10

X, K, D = MAP()

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

flg = X//abs(X)
n = abs(X)//D

if abs(X-n*D*flg) > abs(X-(n+1)*D*flg):
	n += 1

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

X -= flg*n*D
if X == 0:
	flg = 1
else:
	flg = X//abs(X)
K -= n

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
x, k, d = map(int, input().split())
x = abs(x)
mod = x % d
max_move = x - k * d

if mod < max_move:
    res = max_move
else:
    rest = k - x // d
    if rest % 2 == 0:
        res = mod
    else:
        res = abs(mod - d)

print(res)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
x,k,d=map(int,input().split())
x=abs(x)
if x<d:
    if k&1==0:
        print(x)
    else:
        print(abs(x-d))
    sys.exit()

val = x//d

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

x-=(val*d)
k-=val
if k&1==0:
    print(x)

else:
    ans = min(x+d,abs(x-d))
    print(ans)

Submission status: Accepted

Solution code:
import sys
import math
import itertools
import bisect
from copy import copy,deepcopy
from collections import deque,Counter
from decimal import Decimal
def s(): return input()
def i(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
INF = 10**9
mod = 10**9+7

X,K,D = I()
if X >= 0:
    if K*D <= X:
        print(abs(X-K*D))
        exit()
    num1 = X-(X//D)*D
    num2 = num1-D
    if (X//D)%2 == 0:
        f = 0
    else:
        f = 1
    if (X//D)%2 != (K-X//D)%2:
        if f == 0:
            f = 1
        else:
            f = 0
    if f == 0:
        print(abs(num1))
    else:
        print(abs(num2))
else:
    X *= -1
    if K*D <= X:
        print(abs(X-K*D))
        exit()
    num1 = X-(X//D)*D
    num2 = num1-D
    if (X//D)%2 == 0:
        f = 0
    else:
        f = 1
    if (X//D)%2 != (K-X//D)%2:
        if f == 0:
            f = 1
        else:
            f = 0
    if f == 0:
        print(abs(num1))
    else:
        print(abs(num2))

Submission status: Accepted

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]


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


    
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys 
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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

Y = abs(X)
n = Y // D 
s = Y % D

N = K-n
if N < 0:
    print(Y-K*D)
    sys.exit()
    
if N % 2 == 0:
    print(s)
else:
    print(abs(D)-s)

Submission status: Accepted

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

x = abs(X)
k = K

xd = min(x // D + 1, K)
x -= D * xd
k -= xd

if k % 2 == 1:
    x += D

print(abs(x))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#ceilevenoddformula
import math
from queue import PriorityQueue
import bisect
import copy
import time
from collections import Counter
#map(int,input().split())
#int(x) for x in input().split()
x,k,d=map(int,input().split())
x=abs(x)
if x%d==0:
	c=x//d
	if c==k:
		print(0)
	elif c<k:
		k=k-c
		if k%2==0:
			print(0)
		else:
			print(d)
	else:
		print(x-k*d)
else:
	c1=x-(x//d)*d
	c2=abs(c1-d)
	cc1=x//d
	cc2=cc1+1
	if k==cc1:
		print(c1)
	elif k==cc2:
		print(c2)
	elif cc1<k:
		k=k-cc1
		if k%2==0:
			print(c1)
		else:
			print(c2)
	else:
		print(x-k*d)

Submission status: Accepted

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

def dbg(s):
    # print(s)
    pass


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

k_steps = abs(X)//D
x = absX - K*D
mod = abs(X) % D
if k_steps >= K:
    print(x)
    exit()

remain_steps = K - k_steps

if remain_steps % 2 == 0:
    x = mod
else:
    x = D - mod
print(x)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
x, m, d = map(int, input().split())
x = abs(x)
mn = float('inf')
if x // d > m:
    mn = x-m*d
else:
    m -= x//d
    if m % 2 == 0:
        mn = x %d
    else:
        mn = abs(x%d -d)
print(mn)

Submission status: Accepted

Solution code:

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

Submission status: Accepted

Solution code:
X,K,D = list(map(int, input().split()))
#XからD歩移動をK回繰り返す
X = max(X, -X)

if X-(K*D)>0:
  print(X-(K*D))
else:
  #原点に最も接近するステップ数
  step, place, nokori = int(X/D), X-(int(X/D)*D), K-int(X/D)
  #print(step, place, nokori)
  if nokori%2 ==0:
    print(place)
  else:
    print(-(place-D))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
# 標準入力を取得
X, K, D = list(map(int, input().split()))

# 求解処理
ans = int()
C = int(abs(X) / D)
if C >= K:
    if X < 0:
        ans = X + K * D
    else:
        ans = X - K * D
else:
    if X < 0:
        ans = -1 * (abs(X) % D) + ((K - C) % 2 == 1) * D
    else:
        ans = (abs(X) % D) - ((K - C) % 2 == 1) * D
ans = abs(ans)

# 結果出力
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

xdivd=x//d

if abs(xdivd)+1>k:
    # print('a')
    print(abs(abs(x)-d*k))
elif (k-abs(xdivd))%2==0:
    # print('b')
    print(abs(x-d*xdivd))
else:
    # print('c',xdivd)
    print(abs(x-(xdivd+1)*d))

Submission status: Accepted

Solution code:
T = input()
T = T.split(" ")
x = int(T[0])
k = int(T[1])
d = int(T[2])
val = (x+k*d)//(2*d)
if val>=0 and val<=k:
    print(min(abs(-2*d*val+x+k*d), abs(-2*d*(val+1)+x+k*d)))
elif val<0:
    print(abs(x+k*d))
else:
    print(abs(x-k*d))

Submission status: Accepted

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

    if X>0:
        n=K-b
        #print(n)
        if n>=0:
            if n%2==1:
                print(aa)
            else:
                print(a)
        else:
            print(X-D*K)
    if X<0:
        n=K+b        
        if n>=0:
            if n%2==1:
                print(aa)
            else:
                print(a)
        else:
            print(abs(X+D*K))

Submission status: Accepted

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

a = int(X/D)

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

Submission status: Accepted

Solution code:
import math
import collections
import bisect
from collections import deque
from copy import copy, deepcopy
 
 
def main():
    X,K,D = map(int,input().split())
    N = abs(X)//D
    if N>=K:
        ans = abs(X)-K*D
    else:
        if (K-N)%2==0:
            ans = abs(X)-N*D
        else:
            ans = (N+1)*D-abs(X)
    print(ans)
        
 
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import numpy as np

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

xx=x
kk=k

if (x >=0):
  k_int=int(x/d)
  if (k_int >= k):
    xmin=abs(x-k*d)
  else:
    xx1=x-k_int*d
    xx2=xx1-d
    if ((k-k_int) % 2 == 0): 
      xmin=abs(xx1)
    else: xmin=abs(xx2)

if (x <0):
  k_int=int(abs(x)/d)
  if (k_int >= k):
    xmin=abs(x+k*d)
  else:
    xx1=x+k_int*d
    xx2=xx1+d
    if ((k-k_int) % 2 == 0): 
      xmin=abs(xx1)
    else: xmin=abs(xx2)
      
      
print (xmin)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
X, K, D = (int(x) for x in input().split())
X = abs(X)
count = X // D
if K < count:
    count = K
X = abs(X) - D*count
rest = K - count
if rest%2 == 1:
        X -= D
print(abs(X))

Submission status: Accepted

Solution code:
import sys
import math
X, K, D = map(int, input().split())
if X == 0 and K % 2 == 0:
    print(X)
    sys.exit()
elif X == 0 and K % 2 == 1:
    print(D)
    sys.exit()

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

Submission status: Accepted

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

x = abs(X)

ans = 0
if x >= K*D:
  ans = x - K*D
else:
  k = K - x//D
  if k%2==0:
    ans = x % D
  else:
    ans = D - (x % D)
print(ans)

Submission status: Accepted

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

ans = 0
if (x - d * k < 0):
        k -= (x + d - 1) // d
        now = x - (x + d - 1) // d * d
        if (k % 2 == 0):
            ans = abs(now)
        else:
            ans = now + d
else:
    ans = x - d * k
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

    
if __name__ == '__main__':
    main()

Submission status: Accepted

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

if x > 0:
    kaisu = x//d
    if x//d <= k:
        k -= kaisu
        x -= kaisu*d
    else:
        x -= k*d
        k = 0
else:
    kaisu = -(x//d) 
    if kaisu <= k:
        k -= kaisu
        x += kaisu*d
    else:
        x += k*d
        k = 0

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

Submission status: Accepted

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

r = x%d
l = r-d

lc = abs(x-l)//d
rc = abs(x-r)//d

ans = min(abs(x-d*k), abs(x+d*k))
if(k>=lc):
    if((k-lc)%2): ans = min(ans, min(abs(l+d), abs(l-d)))
    else: ans = min(ans, abs(l))
if(k>=rc):
    if((k-rc)%2): ans = min(ans, min(abs(r+d), abs(r-d)))
    else: ans = min(ans, abs(r))
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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


if x<0:
	d*=-1

if k%2==1:
	x-=d

d*=2
k/=2


v1=x-k*d
n=x/d
v2=x-n*d if (n<=k) else v1
v3=x-(n+1)*d if(n+1<=k) else v1

print min(abs(v1),abs(v2),abs(v3))

Submission status: Accepted

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

syou = abs(X) // D


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

Submission status: Accepted

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

Submission status: Accepted

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

X = abs(X)
k = X//D


if K - k <=0:
    print(X - D*K)
elif (K - k) % 2 != 0:
    print(D - X + D*k)
else:
    print(X - D*k)

Submission status: Accepted

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

else:
	if X<0:
		temp=X+d*D
		if (K-d)%2==0:
			ans=temp
		else:
			ans=temp+D
	else:
		temp=X-d*D
		if (K-d)%2==0:
			ans=temp
		else:
			ans=temp-D

print(abs(ans))

Submission status: Accepted

Solution code:
import sys
import math
from collections import defaultdict, deque
from copy import deepcopy
    
input = sys.stdin.readline
def RD(): return input().rstrip()
def F(): return float(input().rstrip())
def I(): return int(input().rstrip())
def MI(): return map(int, input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float,input().split()))
def Init(H, W, num): return [[num for i in range(W)] for j in range(H)]
def end_of_loop():raise StopIteration
    
    
def main():
    X, K, D = MI()
    #計算処理簡単のため
    X = abs(X)
    if K*D <= X:
        print(X-K*D)
        sys.exit()
    syou = X // D
    amari = X % D
    if (K - syou) % 2 == 0:
        print(amari)
    else:
        print(abs(amari-D))

if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

Submission status: Accepted

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

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

a = math.ceil(abs(X)/D)

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

Submission status: Accepted

Solution code:
x,k,d=map(int,input().split())
if x==0:
    if d%2==0:
        print(0)
    else:
        print(d)
else:
    cou=0
    ok=True
    if x>0:
        xx=x
        while x>0:
            if xx-k*d>0:
                ans=x-k*d
                ok=False
                break
            cou+=1
            x-=d
            
        xx=x+d
        if ok:
            if cou%2==0 and k%2==0:
                ans=x
            elif cou%2==0 and k%2==1:
                ans=xx
            elif cou%2==1 and k%2==0:
                ans=xx
            else:
                ans=x
    
    if x<0:
        xx=x
        while x<0:
            if xx+k*d<0:
                ans=xx+k*d
                ok=False
                break
            cou+=1
            x+=d
            
        xx=x-d
        if ok:
            if cou%2==0 and k%2==0:
                ans=x
            elif cou%2==0 and k%2==1:
                ans=xx
            elif cou%2==1 and k%2==0:
                ans=xx
            else:
                ans=x
    print(abs(ans))

Submission status: Accepted

Solution code:
temparr = input()
temparr = temparr.split()
a = abs(int(temparr[0]))
b = int(temparr[1])
c = int(temparr[2])
rem = a // c 

if rem > b :
    print(a - b * c)

else:
    a -=  rem * c 
    b -= rem 
    v1 = a - c 
    v2 = v1 + c 
    if b & 1 :
        print(abs(v1) )
    else:
        print(abs(v2))

Submission status: Accepted

Solution code:
#!python3

import sys
iim = lambda: map(int, sys.stdin.readline().rstrip().split())

def resolve():
    X, K, D = iim()

    r = 1
    if X < 0:
        r = -1
        X = -X
    a, b = divmod(X, D)

    diff = K - a
    if diff <= 0:
        print(abs(r*(X - K*D)))
        return

    if diff & 1:
        b -= D

    print(abs(r*b))



if __name__ == "__main__":
    resolve()

Submission status: Accepted

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

    now = x
    i = 0
    if (now == 0):
        if k%2 == 0:
            print(0)
        else:
            print(d)
    elif (now > 0):
        if (now - d * k >=0):
            print(now - d * k)
            return
        i = int(now/d)
        now = now%d
        if (k-i)%2 == 0:
            print(now)
        else:
            print(d-now)
    elif (now < 0):
        if (now + d * k <=0):
            print(-(now + d * k))
            return
        i = int((-now)/d)
        now = -((-now)%d)
        if (k-i)%2 == 0:
            print(-now)
        else:
            print(now+d)
    
if __name__=='__main__':
    main2()

Submission status: Accepted

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

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

Submission status: Accepted

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

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