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:
x, k, d = map(int, input().split())
tmp = x
limit = abs(x) // d
if limit >= k:
    print(abs(x+k*d) if x < 0 else abs(x-k*d))
else:
    if x > 0:
        plus = abs(x - limit*d)
        minus = abs(x - (limit+1)*d)
        if limit % 2 == 0:
            if k % 2 != 0:
                print(minus)
            else:
                print(plus)
        else:
            if k % 2 != 0:
                print(plus)
            else:
                print(minus)
    else:
        plus = abs(x + (limit+1)*d)
        minus = abs(x + limit*d)
        if limit % 2 == 0:
            if k % 2 != 0:
                print(plus)
            else:
                print(minus)
        else:
            if k % 2 != 0:
                print(minus)
            else:
                print(plus)

Submission status: Accepted

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

if abs(x) > k*d:
    res = abs(x) - k*d
else:
    # res = min(abs(x%d), abs((x%d)-d))
    res = (k*d - abs(x)) % (d*2)
    # res = abs((k*d)%x)
    if abs(x%d) <= abs((x%d)-d):
        k -= x // d
        if k % 2 == 0:
            res = abs(x%d)
        else:
            res = abs((x%d)-d)
    else:
        k -= x // d - 1
        if k % 2 == 0:
            res = abs((x%d)-d)
        else:
            res = abs(x%d)
print(res)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
def main():
    X, K, D = map(int, input().split())

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
XKD = list(map(int, input().split()))

posi = XKD[0]

count = 0
factor = 0
if (XKD[1] > int(abs(posi) / XKD[2])):
    time = XKD[1] - int(abs(posi) / XKD[2])
    factor = int(abs(posi) / XKD[2])
else:
    time = 0
    factor = XKD[1]

if (posi >= 0):
    posi -= XKD[2] * factor
else:
    posi += XKD[2] * factor

if (posi >= 0):
    if (time % 2 == 0):
        print(abs(posi))
    else:
        print(abs(posi - XKD[2]))
else:
    if (time % 2 == 0):
        print(abs(posi))
    else:
        print(abs(posi + XKD[2]))

Submission status: Accepted

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

x=abs(x)
kai=x//d
if kai==k:
    print(x%d)
elif kai>k:
    print(x-k*d)
else:
  right=x%d
  left=abs(x%d-d)

  if (kai+k)%2==0:
      print(right)
  else:
      print(left)

Submission status: Accepted

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

need_jump = abs(X) // D
# print(need_jump)

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

# まだ残っていて、奇数ならもう1回飛んだ値、偶数ならその値が答え
cand1 = abs(X) - D * need_jump
# cand2 = abs(cand1 - D)
# print(min(cand1, cand2))

if (K - need_jump) % 2 == 1:
    ans = abs(cand1 - D)
else:
    ans = cand1
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

X = abs(X)
q, r = divmod(X, D)

if K < q:
  print(X - K * D)
else:
  print(abs(r - ((K - q) % 2) * D))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
IN = list(map(int, input().split()))
X, K, D = IN[0], IN[1], IN[2]

#mv = int(-X / D + 0.5)
mv = round(-X / D, 0)
if (abs(mv)>K): mv = mv * K / abs(mv)
residual_moves = K - abs(mv)

if residual_moves % 2 == 0:
    print (int(abs(X + D * mv)))
else:
    posa = abs(X+D*(mv+1))
    posb = abs(X+D*(mv-1))
    posc = abs(X+D*(mv+2))
    posd = abs(X+D*(mv-2))
    print(int(min(posa, posb, posc, posd)))

Submission status: Accepted

Solution code:
def is_odd(n):
    if n % 2:
        return True
    return False

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

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

print(abs(ans))

Submission status: Accepted

Solution code:
x,k,d=map(int,input().split())
if x>=0:
    a = x % d
    b = x%d-d #負の値
    if a<abs(b):goal=a
    else:goal=b
    if x-k*d>=a:
        res=x-(k*d)
    else:
        rem=k-(x-a)//d
        if rem%2==0:
            res=a
        else:
            res=abs(b)
else:
    a=-((-x)%d)
    b=a+d
    if abs(a)>b:goal=b
    else:goal=a
    if abs(x)-k*d>=-a:
        res=abs(x+(k*d))
    else:
        rem=k-(abs(x-a))//d
        if rem%2==0:
            res=abs(a)
        else:
            res=b
print(res)

Submission status: Accepted

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

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

    exit()

if X > 0:
    if X % D == 0:
        a = X // D
    else:
        a = X // D + 1

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

if X < 0:
    if X % D == 0:
        a = -1 * X // D
    else:
        a = -1 * X // D + 1

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

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
X,K,D=map(int,input().split())
a=round(abs(X/D))
if K<a:
  print(abs(abs(X)-K*abs(D)))
elif (K-a)%2==0:
  l=[]
  l.append(abs(abs(X)-a*abs(D)))
  l.append(abs(abs(X)-(a+2)*abs(D)))
  l.append(abs(abs(X)-(a-2)*abs(D)))
  print(min(l))
elif (K-a)%2!=0:
  l=[]
  l.append(abs(abs(X)-(a+1)*abs(D)))
  l.append(abs(abs(X)-(a-1)*abs(D)))  
  print(min(l))

Submission status: Accepted

Solution code:
import os
import sys
from io import BytesIO, IOBase

BUFSIZE = 8192
class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
INF = 10 ** 18
MOD = 10**9+7

Ri = lambda : [int(x) for x in sys.stdin.readline().split()]
ri = lambda : sys.stdin.readline().strip()


s,k,d = Ri()
flag = False
if s < 0:
    flag = True

totk=  k
s = abs(s)
times = s//d

minn = s-k*d
if minn >= 0:
    print(s-k*d)
else:
    pos1 = s-times*d
    pos2 = INF
    k-=times
    if pos1 == 0:
        if k %2 == 1:
            print(d)
        else:
            print(0)
        exit()
    pos2 = abs(pos1 - d)
    if k == 1:
        print(pos2)
    elif k % 2 == 0:
        print(pos1)
    else:
        print(pos2)

Submission status: Accepted

Solution code:
def hoge(X,K,D):
  abs_X=abs(X)
  if K*D<abs_X:
    return abs_X-K*D
  K-=X//D
  X=X%D
  if K%2==0:
    return X
  else:
    return abs(X-D)

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python
import os
import re
import sys
from bisect import bisect, bisect_left, insort, insort_left
from collections import Counter, defaultdict, deque
from copy import deepcopy
from decimal import Decimal
from fractions import gcd
from io import BytesIO, IOBase
from itertools import (
    accumulate, combinations, combinations_with_replacement, groupby,
    permutations, product)
from math import (
    acos, asin, atan, ceil, cos, degrees, factorial, hypot, log2, pi, radians,
    sin, sqrt, tan)
from operator import itemgetter, mul
from string import ascii_lowercase, ascii_uppercase, digits


def inp():
    return(int(input()))


def inlist():
    return(list(map(int, input().split())))


def instr():
    s = input()
    return(list(s[:len(s)]))


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

# For getting input from input.txt file
#sys.stdin = open('input.txt', 'r')

# Printing the Output to output.txt file
#sys.stdout = open('output.txt', 'w')


def main():
    x, k, d = invr()
    x = abs(x)
    d_move = min(k, x // d)
    curr = x - d_move*d
    baki = k - d_move
    if baki % 2 != 0:
        print(d - curr)
    else:
        print(curr)


# ab
BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
def input(): return sys.stdin.readline().rstrip("\r\n")

# endregion


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import math

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


if abs(x) >= d * k:
    print(abs(abs(x)-d*k))
else:
    temp = math.ceil(abs(x)/d)
    if temp % 2 == 1:
        if k % 2 == 1:
            print(abs(abs(x)-temp*d))
        else:
            print(abs(abs(x)-temp*d+d))
    elif temp % 2 == 0:
        if k % 2 == 0:
            print(abs(abs(x)-temp*d))
        else:
            print(abs(abs(x)-temp*d+d))

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

def counta(x,k,d):
    #xcnt=0
    x=abs(x)
    if d<2*x:
        ans=x//d
        #amari=x%d
        if ans>k:
            print(abs(x-k*d))
            return
        k=k-ans
        x=x-ans*d
        if k%2==0:
            print(x)
            return
        else:
            print(abs(x-d))
            return
    else:
        if k%2==0:
            print(x)
            return
        else:
            print(abs(x-d))
            return
counta(x,k,d)

Submission status: Accepted

Solution code:
def solve(X, D, lb, ub):
    # min abs(X + t * D) (lb <= t <= ub)
    t = round(-X / D)
    ans = abs(X)
    for i in [-2, -1, 0, +1, +2]:
        tt = t + i
        tt = min(tt, ub)
        tt = max(tt, lb)
        y = X + tt * D
        ans = min(ans, abs(y))
    return ans

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

y1 = X - K * D
y2 = X + K * D
if (y1 > 0 and y2 > 0) or (y1 < 0 and y2 < 0): # same sign
    print(min(abs(y1), abs(y2)))
else:
    if K % 2 == 0:
        ans = solve(X, D * 2, -(K // 2), +(K // 2))
    else:
        ans1 = solve(X + D, D * 2, -((K + 1) // 2), +((K - 1)) // 2)
        ans2 = solve(X - D, D * 2, -((K - 1) // 2), +((K + 1)) // 2)
        ans = min(ans1, ans2)
    print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

pm = 1 if x >= 0 else -1
a = abs(x)//d
b = a if abs(x-pm*a*d) < abs(x-pm*(a+1)*d) else a+1
c = min(b, k)
e = x-pm*c*d
if k < b or (k-b) % 2 == 0:
    print(abs(e))
else:
    print(min(abs(e+d), abs(e-d)))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
#import string
#from collections import defaultdict, deque, Counter
#import bisect
#import heapq
#import math
#from itertools import accumulate
#from itertools import permutations as perm
#from itertools import combinations as comb
#from itertools import combinations_with_replacement as combr
#from fractions import gcd
#import numpy as np

stdin = sys.stdin
sys.setrecursionlimit(10 ** 7)
MIN = -10 ** 9
MOD = 10 ** 9 + 7
INF = float("inf")
IINF = 10 ** 18

def solve():
    #n = int(stdin.readline().rstrip())
    x,k,d = map(int, stdin.readline().rstrip().split())
    #L = list(map(int, stdin.readline().rstrip().split()))
    #numbers = [[int(c) for c in l.strip().split()] for l in sys.stdin]
    #word = [stdin.readline().rstrip() for _ in range(n)]
    #number = [[int(c) for c in stdin.readline().rstrip()] for _ in range(n)]
    #zeros = [[0] * w for i in range(h)]
    if abs(x) >= k*d:
        print(abs(x) - k*d)
        exit()
    else:
        a = abs(x) // d
        if abs(abs(x) - d*a) >= abs(abs(x) - d*(a+1)) and (k-a-1)%2 == 0:
            print(abs(abs(x) - d*(a+1)))
            exit()
        elif abs(abs(x) - d*a) >= abs(abs(x) - d*(a+1)) and (k-a-1)%2 == 1:
            print(abs(abs(x) - d*a))
            exit()
        elif abs(abs(x) - d*a) <= abs(abs(x) - d*(a+1)) and (k-a)%2 == 0:
            print(abs(abs(x) - d*a))
            exit()
        elif abs(abs(x) - d*a) <= abs(abs(x) - d*(a+1)) and (k-a)%2 == 1:
            print(abs(abs(x) - d*(a+1)))
            exit()

if __name__ == '__main__':
    solve()

Submission status: Accepted

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

if x>0:
    if x>k*d:
        ans=x-k*d
    else:
        X = x-round(x//d)*d
        if (k-round(x//d))%2==0:
            ans=abs(X)
        else:
            ans=abs(X-d)
else:
    if abs(x)>k*d:
        ans=abs(x+k*d)
    else:
        X=x+round(abs(x)//d)*d
        if (k-round(abs(x)//d))%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 K < X // D:
    print(X - (K * D))
    exit()
else:
    # 残りが偶数か奇数か
    if (K - X // D) % 2 == 0:
        print(X % D)
        exit()
    else:
        print(min(abs(X % D - D), abs(X % D + D)))
        exit()

Submission status: Accepted

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

if x < 0:
  x *= -1

r = x % d
q = x // d

if q <= k:
  if (k - q) % 2 == 0:
    print(r)
  else:
    print(d - r)
  
else:
  print(r + (q - k) * d)

Submission status: Accepted

Solution code:

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
start, num_move, distance = map(int, input().split())

# move toward
desired_move = abs(start) // distance

another_move = num_move - desired_move
if another_move <= 0:
    ans_abs = abs(start) - num_move * distance
    print(ans_abs)

else:
    if another_move % 2 == 0:
        ans_abs = abs(start) - desired_move * distance
        print(ans_abs)
    else:
        ans_abs = abs(abs(start) - (desired_move+1) * distance)
        print(ans_abs)

Submission status: Accepted

Solution code:
import math

def a():
    s = input()
    if s == 'RRR':
        print(3)
    elif 'RR' in s:
        print(2)
    elif 'R' in s:
        print(1)
    else:
        print(0)


def b():
    n = int(input())
    l = list(map(int, input().split()))
    l.sort()
    count = 0
    for i, li in enumerate(l):
        for j, lj in enumerate(l[i + 1:],i + 1):
            for lk in l[j + 1:]:
                if lk < li + lj and li != lj and lj != lk:
                    # print(li,lj,lk)
                    count += 1
    print(count)




def c():
    x,k,d = map(int,input().split())
    min = 0
    x = abs(x)
    # k_max = x // d
    # x -= k_max * d
    # k回移動しても0を超えない
    if x >= k * d:
        min = x - k * d
    else:
        # 0を超える直前まで移動（その分kを減算）
        k -= (x // d)
        x -= (x // d) * d
        min = abs(x - (k % 2) * d)
    print(min)



def d():
    pass


# a()
# b()
c()
# d()

Submission status: Accepted

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

def d(h):
    return X + (h - (K-h))*D

if d(s) >= 0:
    ans = d(s)
elif d(g) <= 0:
    ans = abs(d(g))
else:
    half = (s + g)//2
    while s < half:
        if d(half) < 0:
            s = half
        else:
            g = half
        half = (s + g)//2

    ans = min(abs(d(s)) , abs(d(g)) )
print(ans)

Submission status: Accepted

Solution code:
import math
from collections import defaultdict,deque
from itertools import permutations
ml=lambda:map(int,input().split())
ll=lambda:list(map(int,input().split()))
ii=lambda:int(input())
ip=lambda:list(input())
ips=lambda:input().split()

"""========main code==============="""

t=1
#t=ii()
for _ in range(t):
    a,b,c=ml()
    a=abs(a)
    rem=a//c
    rem=abs(rem)
    
    if(b>=rem and b%2==rem%2):
        ans=abs(abs(a)-c*rem)
        print(ans)
    elif(b>=rem+1 and b%2!=rem%2):    
        ans=abs(abs(a)-c*(rem+1))
        print(ans)
    else:
        print(abs(abs(a)-c*b))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def resolve():
    X, K, D = list(map(int, input().split()))
    k = min(abs(X) // D, K)
    x = min(abs(X - D*k),abs(X + D*k))
    l = K-k
    if l % 2 == 1:
        x = min(abs(x-D), abs(x+D))
    print(x)
    
resolve()

Submission status: Accepted

Solution code:
from sys import stdin
def ip(): return [int(i) for i in stdin.readline().split()]
def sp(): return [str(i) for i in stdin.readline().split()]


def solve(x,k,d):
    di = int(x//d)
    if di >= k: 
        return abs(x) - (k*d)
    x = x - (di*d)
    k -= di
    if k % 2 == 0:
        return abs(x)
    else:
        return abs(x - d)

x,k,d = ip()
print(solve(abs(x),k,d))

Submission status: Accepted

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

Submission status: Accepted

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

ax = abs(x)
t = k * d

if t == ax:
    print(0)
elif t < ax:
    print(ax - t)
else:
    a = ax % d
    b = d - a
    f = ax // d

    if k % 2 == f % 2:
        print(a)
    else:
        print(b)

Submission status: Accepted

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

if k < s:
    print(x-d*k)
else:
    if ans < -ans+d:
        if (k-s)%2 == 0:
            print(ans)
        else:
            print(-ans+d)
    else:
        if (k-s)%2 == 0:
            print(ans)
        else:
            print(-ans+d)

Submission status: Accepted

Solution code:
import sys
import itertools
# import numpy as np
import time
import math
import heapq
from collections import defaultdict
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

# map(int, input().split())

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

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



# left = -1
# right = 10 ** 15 + 1
# while right - left > 1:
#     mid = (left + right) // 2
#     if check(mid):
#         right = mid
#     else:
#         left = mid

Submission status: Accepted

Solution code:
from sys import stdin,stdout
for _ in range(1):#int(stdin.readline())):
    # n=int(stdin.readline())
    x,k,d=list(map(int,stdin.readline().split()))
    '''if x==k==d:
        print(0 if k&1 else x)
        continue'''
    x=abs(x)
    des=x%d
    op=x//d
    # print(des,op)
    a=[des,des-d]
    if k<op:#check for eq
        print(x-k*d)
    else:
        print(abs(a[(k-op)%2]))

Submission status: Accepted

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

n = abs(x)//d
sign_x = 1 if x>0 else -1

if n>=k:  
  x -= sign_x*k*d
  print(abs(x))
else:
  cand = [x-sign_x*n*d, x-sign_x*(n+1)*d]
  rest_n = k-n
  if rest_n%2:
    print(abs(cand[1]))
  else:
    print(abs(cand[0]))

Submission status: Accepted

Solution code:
""" C
    https://atcoder.jp/contests/abc175/tasks/abc175_c
"""
import sys
import math
from functools import reduce
from bisect import bisect_left

def readString():
    return sys.stdin.readline()

def readInteger():
    return int(readString())

def readStringSet(n):
    return sys.stdin.readline().split(" ")[:n]

def readIntegerSet(n):
    return list(map(int, readStringSet(n)))

def readIntegerMatrix(n, m):
    return reduce(lambda acc, _: acc + [readIntegerSet(m)], range(0, n), [])

def _pattern(N):
    if N > 1:
        pa = _pattern(N-1)
        return [[0] + p for p in pa] + [[1] + p for p in pa]
    else:
        return [[0], [1]]

def main(X, K, D):
    if abs(X) > abs(D) * K:
        return abs(X) - abs(D) * K
    else:
        k = abs(X) // abs(D)
        if (K - k) % 2 == 0:
            return abs(X) - k * abs(D)
        else:
            return min(abs(abs(X) - (k+1) * abs(D)), abs(abs(X) - (k-1) * abs(D)))

if __name__ == "__main__":
    _X, _K, _D = readIntegerSet(3)

    print(main(_X, _K, _D))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
X,K,D = map(int,input().split())
if(X>0):
    posneg = 0
else:
    posneg = 1

if(posneg == 0):
    Ki = math.ceil(X/D)
    D = D*(-1)
else:
    Ki = abs(math.floor(X/D))

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
import glob

class InputGenerator(object):
    def __init__(self, input_path=None):
        if input_path != None:
            self.g = self.input_generator(input_path)
        else:
            self.g = None

    def input_generator(self, input_path):
        with open(input_path) as f:
            for line in f:
                yield line

    def get_input(self):
        if self.g == None:
            return input()
        else:
            return next(self.g)

def solve(gen):
    len_list = gen.get_input().rstrip().split()
    [x, k, d] = list(map(int, len_list))
    # print(x, k, d)

    r = abs(x) % d
    l = d - r
    q = abs(x) // d

    if abs(x) > k * d:
        print(abs(x) - k * d)
    elif (k - q) % 2 == 0:
        print(r)
    else:
        print(l)

if __name__ == '__main__':
    test_flag = len(sys.argv) > 1

    if test_flag == True:
        file_path_list = sorted(glob.glob('test/*'))
    else:
        file_path_list = [None]

    for file_path in file_path_list:
        gen = InputGenerator(file_path)
        solve(gen)

Submission status: Accepted

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

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

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)