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: Wrong Answer

Solution code:
[x, k, d] = list(map(int, input().split()))
x = abs(x)
howMany = x // d
remainder = x % d
stepLeft = k - howMany
if k < howMany:
    print(abs((howMany - k) * d))
elif stepLeft % 2 == 0:
    print(abs(remainder))
elif stepLeft % 2 == 1:
    print(abs(d - remainder))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

init = 10**15
count =0
while True:
    if count == k:
        break
    next1 = x+d
    next2 = x-d
    next = min(abs(next1),abs(next2))
    if x <= next:
        break
    else:
        x=next
        count += 1
print(x)

Submission status: Wrong Answer

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

else:
    y = abs(x) - d * k

print(y)

Submission status: Wrong Answer

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

def valid(value1, value2, diff):
    if diff%2==0:
        return value1
    else:
        return value2

tmp = [X]
min_score = abs(X)
for i in range(K):
    if abs(X-D)>abs(X+D):
        X = X+D
    else:
        X = X-D
    if min_score > abs(X):
        min_score=abs(X)

    if X in tmp:
        a = min([abs(X-D), abs(X+D)])
        min_score = valid(a, min_score, K-i)
        break
    tmp.append(X)
print(min_score)

Submission status: Wrong Answer

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

X=A[0]
K=A[1]
D=A[2]

answer=X
for i in range(K):
  if(abs(X-D) < answer):
    answer = abs(X-D)
    X = X-D
    i += 1
  elif(abs(X+D) < answer):
    answer = abs(X-D)
    X = X-D
    i += 1

print(answer)

Submission status: Wrong Answer

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

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

elif X < 0: 
  if K*D <= X:
    ans = X-K*D
  else:
    if (1+X//D)%2 == 1 and (K-X//D)%2 ==1:
      ans = (X%D)
    elif (1+X//D)%2 == 1 and (K-X//D)%2 ==0:
      ans = (X%D)-X
    elif (1+X//D)%2 == 0 and (K-X//D)%2 ==0:
      ans = (X%D)
    elif (1+X//D)%2 == 0 and (K-X//D)%2 ==1:
      ans = (X%D)-X

elif X == 0:
  if K%2 == 1:
    ans = D
  else:
    ans = 0

print(abs(ans))

Submission status: Wrong Answer

Solution code:

playerinput = input()
lst = playerinput.split(" ")

X = int(lst[0])
K = int(lst[1])
D = int(lst[2])

lowest_value = X

lst_of_values = ["empty"]

if X == K == D:
    K = 0
    lowest_value = X


while K != 0:
    if abs(lowest_value + D) < lowest_value:
        lowest_value = lowest_value + D
        K -= 1

    if abs(lowest_value - D) < lowest_value:
        lowest_value = lowest_value - D
        K -= 1

    else:
        break
if K == 1:
    value1 = abs(lowest_value + D)
    value2 = abs(lowest_value - D)
    if value1 < value2:
        lowest_value = value1
    else:
        lowest_value = value2
print(lowest_value)

Submission status: Wrong Answer

Solution code:
import math
n, m, x = map(int, input().split())
p = round(n /x)
p = min(p, m)
n -= x * p
m -= p
if m % 2 == 0:
    print(abs(n))
else:
    a = abs(n-x)
    b = abs(n+x)
    print(min(a,b))

Submission status: Wrong Answer

Solution code:
def function(X, K, D):
    kai = int(abs(X)/D)
    if (abs(X) % D == 0) and (abs(X)/D <= K):
        if abs(X) > D:
            ans = D
        else:
            ans = X
    elif kai >= K:
        if X > 0:
            ans = X-K*D
        else:
            ans = X+K*D
    else:
        K = K-kai
        if X > 0:
            X = X-kai*D
        else:
            X = X+kai*D
        if X > 0:
            if abs(X-D) < X:
                ans = X-D
            else:
                ans = X
        else:
            if abs(X+D) < abs(X):
                ans = X+D
            else:
                ans = X
    return ans


if __name__ == '__main__':
    X, K, D = input().split()
    X = int(X)
    K = int(K)
    D = int(D)
    print(function(X, K, D))

Submission status: Wrong Answer

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

q = X // D
mod = X % D

if q > K:
    ans = abs(abs(X)-K*D)
elif q==K:
    ans = mod
else:
    amod = D - mod
    if abs(mod) <= abs(amod):
        if (K-q)%2 == 1:
            a1 = mod - D
            a2 = mod + D
            ans = min(abs(a1),abs(a2))
        else:
            ans = mod
    elif abs(mod) > abs(amod):
        if (K-q-1)%2 == 1:
            a1 = amod - D
            a2 = amod + D
            ans = min(abs(a1),abs(a2))
        else:
            ans = abs(amod)


print(ans)

Submission status: Wrong Answer

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

if x == k and k == d:
    print(x)
elif x < k*d:
    print(x % d)
else:
    print(x - k * d)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

x=min(K,abs(X)//D)

if X>0:
    X_=X-(D*x)
else:
    X_=X+(D*x)

k=K-x
if k==0:
    ans=X_
elif k>=1 and k%2==0:
    ans=min(X_,abs(X_-D),abs(X_+D))
elif k>=1 and k%2!=0:
    ans=min(abs(X_-D),abs(X_+D))
    if (10**15)-(abs(X_)+D)>=D:
        if X_>0:
            X_=X_+D
        else:
            X_=X_-D
        ans=min(ans,X_,abs(X_-D),abs(X_+D))

print(ans)

Submission status: Wrong Answer

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


if d>=abs(x):
    if k%2==0:
        p=abs(x)
    else:
        if x>=0:
            p=abs(x-d)
        else:
            p=abs(x+d)
    print(p)
    exit(0)
else:
    cnt=abs(x)//d

    if cnt>=k:
        cnt=k

    if x>=0:
        p=x-d*cnt
    else:
        p=x+d*cnt

    k-=cnt

    if k==0:
        print(p)
        exit(0)

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

        print(p)

Submission status: Wrong Answer

Solution code:
list_in = list(map(int,input().split()))
xin = list_in[0]
kin = list_in[1]
din = list_in[2]

now_place = xin
zflag = 0

for i in range(kin):
    p_place = now_place + din
    m_place = now_place - din
    if m_place < 1:
        zflag = 1
    if (abs(p_place) < abs(m_place)):
        now_place = p_place
    else:
        now_place = m_place
    if (zflag == 1) and (0 == ((kin + 1 - i)%2)):
        break

print(abs(now_place))

Submission status: Wrong Answer

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

min_pos = x % d
min_neg = min_pos - d

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

Submission status: Wrong Answer

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

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

s = x

while k>0:
    s = s-d
    if abs(s)>=abs(x):
        break
    x = s
    k -= 1

if k == 0:
    print(abs(s))
    exit()

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

Submission status: Wrong Answer

Solution code:
def main():
    x, k, d = map(int, input().split())
    if x >= 0:
        pos = x % d
        neg = (x % d) - d
        just_pos_num = x // d
        just_neg_num = x // d + 1
        if k < just_pos_num:
            ans = x - (k * d)
        else:
            m = k - just_pos_num
            if m % 2 != 0:
                ans = abs(neg)
            else:
                ans = abs(pos)
    else:
        pos = (x % d) + d
        neg = x % d
        just_neg_num = x // d
        just_pos_num = x // d + 1
        if k < just_neg_num:
            ans = x + (k * d)
        else:
            m = k - just_neg_num
            if m % 2 != 0:
                ans = abs(pos)
            else:
                ans = abs(neg)
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
#k = int(input())
#s = input()
#a, b = map(int, input().split())
#s, t = map(str, input().split())
#l = list(map(int, input().split()))
#l = [list(map(int,input().split())) for i in range(n)]
#a = [input() for _ in range(n)]


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

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

if (x % 2 != 0 and k % 2 != 0):
    tmp += 1
elif (x % 2==0 and k % 2 == 0):
    tmp += 1

#print(tmp)
if (x>=0):
    print(abs(x-tmp*d))
else:
    print(abs(x+tmp*d))

Submission status: Wrong Answer

Solution code:
x, k, d = map(int, input().split())
ans = min(abs(x + k * d), abs(x - k * d))
t = x // d
for i in range(t - 10, t + 10):
    if abs(i) <= k and abs(i) % 2 == k % 2:
        ans = min(ans,abs(x + i * d))
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
x,k,d = map(int,input().split())
if 0 < x:
    if 0 <  x - d * k:
        print(abs(x - d * k))
    elif x - d * k == 0:
        print(0)
    else:
        judge = x //d
        j1 = x - judge * d
        count = k - judge
        if count % 2 == 0:
            print(abs(j1))
        else:
            print(abs(j1-d))
elif x < 0:
    if x + d * k < 0:
        print(abs(x - d * k))
    elif x - d * k == 0:
        print(0)
    else:
        judge = -x//d
        j2 = x + judge * d
        count = k - judge
        if count % 2 == 0:
            print(abs(j2))
        else:
            print(abs(j2+d))
else:
    # (x == 0)
    if k%2 == 0:
        print(0)
    else:
        print(d)

Submission status: Wrong Answer

Solution code:
import math
X, K, D = map(int, input().split())
ans = X
minc_floor = math.floor(X/D)
minc_ceil = math.ceil(X/D)
min_kaisuu = K
# print(minc_ceil, minc_floor)
if minc_ceil <= K:
    for k in range(minc_floor, minc_ceil+1):
        for flug in [-1, 1]:
            if abs(X + flug*k*D) < ans:
                min_kaisuu = k
                ans = abs(X + flug*k*D)
    if (K-min_kaisuu) % 2 != 0:
        ans = min(abs(ans+D), abs(ans-D))
else:
    ans = min(abs(ans+K*D), abs(ans-K*D))
# print(min_kaisuu)
print(ans)

Submission status: Wrong Answer

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


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

Submission status: Wrong Answer

Solution code:

inp = list(map(int,raw_input().split()))
x = inp[0]
k = inp[1]
d = inp[2]
x = abs(x)
if(d==0 or k==0):
    print(x)
elif(x >= k*d):
    print(x-k*d)
else:
    res=x
    tmp = int(x/d)
    for y in range(-2,3):
        if((y+tmp)<=k and y+tmp >=0):
            rem = k-y-tmp
            if(rem%2 == 0):
                res=min(res,abs(x-(y+tmp)*d))
            else:
                res=min(res,abs(x-(y+tmp-1)*d))
                if(rem>=1):
                    res=min(res,abs(x-(y+tmp+1)*d))
    print(res)

Submission status: Wrong Answer

Solution code:
import sys

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

p = X
_p = 0
k = 0
if p < 0:
    if abs(p) > K*D:
        print(abs(p)-K*D)
        sys.exit()
    while 1:
        _p = p
        p += D
        k += 1
        if p >= 0:
            break
    if K-k % 2 == 0:
        print(abs(p))
    else:
        print(abs(_p))
elif p > 0:
    if abs(p) > K*D:
        print(abs(p)-K*D)
        sys.exit()
    while 1:
        _p = p
        p -= D
        k += 1
        if p <= 0:
            break
    if K-k % 2 == 0:
        print(abs(p))
    else:
        print(abs(_p))
else:
    if K % 2 == 0:
        print(0)
    else:
        print(D)

Submission status: Wrong Answer

Solution code:
x, k, d = map(int, input().split())
p = (x + d * k) // (2 * d)

def f(a):
  return abs(x + d * (k - 2 * a))

if p > k:
  print(min(f(0), f(k)))
else:
  print(min(f(p), f(p + 1)))

Submission status: Wrong Answer

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


x = abs(x);

t = int(x / d);

x -= (d*t);

if(t >= k):
	print (x)
else:
	k -= t;
	if(k%2 == 1):
		print (abs(x - d));
	else:
		print (x)

Submission status: Wrong Answer

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

ans = 0
count = X // D

if X >= 0:
    if K <= count:
        ans = X - D * K
    else:
        ans = X - D * count
        if ((K - count) % 2 == 1):
            ans = ans - D
else:
    if K <= (-1) * count:
        ans = X + D * K
    else:
        ans = X + D * count
        if ((K + count) % 2 == 1):
            ans = ans + D

if ans < 0:
    ans = ans * (-1)

print(ans)

Submission status: Wrong Answer

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

x = abs(x)
ans = 0

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

print(ans)

Submission status: Wrong Answer

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

n = round(X/D)

if K<=n:
    ans = abs(X-K*D)
else:
    tmp = X-D*n
    if (K-n)%2==0:
        ans = abs(tmp)
    else:
        ans = abs(tmp-D)

print(ans)

Submission status: Wrong Answer

Solution code:
import math
N=input().split()
X=int(math.fabs(int(N[0])))
K=int(N[1])
D=int(N[2])
if X-K*D>=0:
    print(X-K*D)
else:
    x=X%D
    y=X-X%D
    a=int((X-x)/D)
    b=int((X-y)/D)
    if K%2==0:
        if a%2==0:
            print(x)
        else:
            print(y)
    else:
        if a%2==1:
            print(x)
        else:
            print(y)

Submission status: Wrong Answer

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


if K <= X//D:
    print(abs(X-D*K))
    
else:
    if X%D == 0:
        print(abs(D))
    else:
        if K - X//D > 2:
            Y = X%D
            a = abs(D)+Y
            b = abs(Y-D)
            print(min(Y,a,b))

        else:
            Y = X%D
            a = abs(D)+Y
            b = abs(Y-D)
            print(min(a,b))

Submission status: Wrong Answer

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

if D * K <= X:
    print(X-D*K)
else:
    nokori = K - shou
    if nokori %2 == 0:
        print(amari)
    else:
        print(abs(amari - D))

Submission status: Wrong Answer

Solution code:
import sys, re
from math import ceil, floor, sqrt, pi, factorial, gcd
from copy import deepcopy
from collections import Counter, deque
from heapq import heapify, heappop, heappush
from itertools import accumulate, product, combinations, combinations_with_replacement
from bisect import bisect, bisect_left, bisect_right
from functools import reduce
from decimal import Decimal, getcontext
# input = sys.stdin.readline 
def i_input(): return int(input())
def i_map(): return map(int, input().split())
def i_list(): return list(i_map())
def i_row(N): return [i_input() for _ in range(N)]
def i_row_list(N): return [i_list() for _ in range(N)]
def s_input(): return input()
def s_map(): return input().split()
def s_list(): return list(s_map())
def s_row(N): return [s_input for _ in range(N)]
def s_row_str(N): return [s_list() for _ in range(N)]
def s_row_list(N): return [list(s_input()) for _ in range(N)]
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 6)
INF = float('inf')
MOD = 10 ** 9 + 7
num_list = []
str_list = []

def main():
    x, k, d = i_map()

    amari = x % d
    shou = x // d

    if k % 2 == 0:
        if amari == 0:
            if shou % 2 == 0:
                if shou <= k:
                    print(0)
                    exit()
                else:
                    print(abs(x-k*d))
                    exit()

        if shou % 2 == 1:
            if shou+1 <= k:
                print(abs(x-(shou+1)*d))
                exit()
    else:
        if amari == 0:
            if shou % 2 == 1:
                if shou <= k:
                    print(0)
                    exit()
                else:
                    print(abs(x-k*d))
                    exit()

        if shou % 2 == 0:
            if shou+1 <= k:
                print(abs(x-(shou+1)*d))
                exit()

    if shou <= k:
            print(abs(amari))
            # print('a')
    else:
        print(abs(x-k*d))  
        # print('b')



if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

if x==0 and k%2==0:print(0)
if x==0 and k%2==1:print(d)

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

Submission status: Wrong Answer

Solution code:
x, k, d = map(int, input().split())  # x現在地,k 移動回数,D移動できる距離
a = x // d  # 移動できる回数
b = x % d  # 移動した特のあまり
if k <= a:  # 移動する回数がkより大きいとき
    x = x - (k*d)
else:
    k = k - a  # ここで最短
    if k % 2 == 0:
        x = abs(x) - abs(a * d)
    else:
        x = abs(x) - abs(a * d)
        if x > 0:
            x = x + abs(d)
        else:
            x = x + abs(d)

print(abs(x))

Submission status: Wrong Answer

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

all_dis = K * D # 動ける残り変量
ans = 0
if X >= all_dis:
    ans = X - all_dis
else:
    amari_X = X % D # 最初０に動きたい時に最終的に到着した位置
    sho_X = X // D #　何回Kを使ったか
    all_dis -= sho_X * D # 到着した位置までの変量を減らす

    amari_a = all_dis % D # 余る数 
    sho_a = all_dis // D # 残りの動ける変量から何回動けるか
    ans = amari_X + amari_a
    if sho_a % 2 != 0:
        ans -= D
print(abs(ans))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import math
n, m, x = map(int, input().split())
p = math.ceil(n /x)
p = min(p, m)
n -= x * p
m -= p
if m % 2 == 0:
    print(abs(n))
else:
    a = abs(n-x)
    b = abs(n+x)
    print(min(a,b))

Submission status: Wrong Answer

Solution code:

x,k,d = map(int,input().split())
mina = 1000000000000000
cnt = 0

for i in range(k):
    if abs((x + d)) > abs((x - d)):
        x = abs(x - d)
    elif abs((x - d)) > abs((x + d)):
        x = abs(x + d)
    if mina > x:
        mina = x
        cnt = 0
    else:
        cnt += 1
    if cnt >= 10:
        break

print(x)

Submission status: Wrong Answer

Solution code:
x, k, d = map(int, input().split())
for i in range(k):
    if abs(x - d) > abs(x + d):
        x = x + d
    else:
        x = x - d
print(x)

Submission status: Wrong Answer

Solution code:

li = list(map(int ,input().split()))

x = abs(li[0])
k = li[1]
d = li[2]

if x == d and k % 2 == 0:
    print(x)
    exit()
if x == d and k & 2 == 1:
    print(0)
    exit()

for _ in range(k):
    sum_result = x + d
    minus_result = x - d
    if abs(sum_result) < abs(minus_result):
        x = sum_result
    elif abs(sum_result) > abs(minus_result):
        x = minus_result


print(abs(x))

Submission status: Wrong Answer

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

tmp = X // D
d1 = X - (D * tmp)
d2 = X - (D * (tmp + 1))

if tmp > K:
  ans = X - (D * K)
else:
  if (tmp % 2) ^ (K % 2) == 0:
    ans = d1
  else:
    ans = d2

print(abs(ans))

Submission status: Wrong Answer

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

ab=X/D

if ab.is_integer():
    if ab<=K:
        ab=int(ab)
    else:
        ab=K
        
else:
    if ab<=K:
        ab=int(ab)+1
    else:
        ab=K
X=X-ab*D

cc=X
K=K-ab

if K>1:
    asa=K//2
    cc=cc+(asa)*(D)
    K=K-asa
    cc=cc-K*D
else:
    cc=cc+K*D
    



print(abs(cc))

Submission status: Wrong Answer

Solution code:
x,k,d = map(int,input().split())
ans_1 = x%d #プラスの最小
ans_2 = abs(ans_1-d) #マイナスの最小

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

Submission status: Wrong Answer

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

pre1 = 0
pre2 = 0
now = X

for k in range(K):
    pre2 = pre1
    pre1 = now
    sei = now + D
    hu = now - D
    if(abs(sei) < abs(hu)):
        now = sei
    elif(abs(sei) > abs(hu)):
        now = hu
    if(pre2 == now):
        if((K+1-k) % 2 == 0):
            print(abs(now))
            exit(0)
        else:
            print(abs(pre1))
            exit(0)
print(abs(now))

Submission status: Wrong Answer

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()

def main():
    x, k, d = map(int, input().split())
    cnt = abs(x) // d
    point = x % d
    if x == 0:
        if k % 2 == 0:
            point = 0
        else:
            point = d
    else:
        # 最小値まで行く回数がKより多い場合
        if cnt > k:
            if x > 0:
                point = x - k * d
            elif x < 0:
                point = x + k * d
        # Kより少ないときは
        else:
            # もう一回移動して最小値を確かめる
            if point == 0:
                if (k - cnt) % 2 == 0:
                    point = 0
                else:
                    point = d
                print(point)
                exit()
            elif point < 0:
                if abs(point + d) < abs(point):
                    point = point + d
                    cnt += 1
            elif point > 0:
                if abs(point - d) < abs(point):
                    point = point - d
                    cnt += 1
            # k-cntが偶数の時はそのまま、奇数の時は一回逆方向にdだけ進む
            if (k - cnt) % 2 == 0:
                pass
            else:
                if point < 0:
                    point += d
                elif point > 0:
                    point -= d
    print(abs(point))

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
x, k, d = map(int, input().split())  # x現在地,k 移動回数,D移動できる距離
a = x // d  # 移動できる回数
b = x % d  # 移動した特のあまり
if k <= a:  # 移動する回数がkより大きいとき
    x = x - (k*d)
else:
    k = k - a  # ここで最短
    if k % 2 == 0:
        x = abs(x) - abs(a * d)
    else:
        x = abs(x) - abs(a * d)
        if x > 0:
            x = x - abs(d)
        else:
            x = x + abs(d)

print(abs(x))

Submission status: Wrong Answer

Solution code:
def main():
    x, k, d = map(int, input().split())
    if x >= 0:
        pos = x % d
        neg = (x % d) - d
        just_pos_num = x // d
        just_neg_num = x // d + 1
        if k < just_pos_num:
            ans = x - (k * d)
            print(ans)
            exit()
        else:
            m = k - just_pos_num
            if m % 2 != 0:
                ans = abs(neg)
            else:
                ans = abs(pos)
    else:
        pos = (x % d) + d
        neg = x % d
        just_neg_num = x // d
        just_pos_num = x // d + 1
        if k < just_neg_num:
            ans = x + (k * d)
            print(ans)
            exit()
        else:
            m = k - just_neg_num
            if m % 2 != 0:
                ans = abs(neg)
            else:
                ans = abs(pos)
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

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

ans = 0
if q <= K:
    if mod == 0:
        if q % 2 == 1:
            ans = D
        else:
            ans = mod
    else:
        if q % 2 == 0:
            ans = mod
        else:
            ans = D + mod
else:
    div = q - K
    ans = div * D + mod

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
X, K, D = map(int,input().split())
cnt = 0
a = X
b = 999999999
tmp = 0
if X == 0:
  if K % 2 == 0:
    print(0)
  else:
    print(D)
  sys.exit()
if X >= K*D:
  print(X - K*D)
  sys.exit()
if K % 2 == 0:
  tmp = int(X/D)
  if tmp% 2 == 0:
    a = X - D*tmp
  else:
    a = X - D*tmp + D
  b = a - D*2
else:
  tmp = int(X/D)
  if tmp% 2 == 1:
    a = X - D*tmp
  else:
    a = X - D*tmp + D
  b = a - D*2
print(min(abs(a),abs(b)))

Submission status: Wrong Answer

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

x = abs(x)
ans = 0

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

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

# print(x,k,d)

x = abs(x)
if x < d:
    if k%d:
        print(d-x)
    else:
        print(x)
    exit()

c = min(x//d, k)
x = abs(x-d*c)
k = k-c
# print(k,c,x)
if x == 0:
    if k%2==0: # even
        print(0)
    else: # odd
        print(d)

else: # +
    print(x)

Submission status: Wrong Answer

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

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

x = abs(x_org)
ans = 0 

def a(n):
    global x
    global d
    ret = x-d*(n-1)
    return ret 

n = x//d
l = math.floor(n)
r = l+1 

if n > k:
    ans = abs(x-k*d)
    print(ans)
    exit()

if x_org >= 0:
    if (x//d)%2 == k%2:
        ans = abs(x-d*l)
        print(ans)
        exit()
    else:
        ans = abs(x-d*r)
        print(ans)
        exit()
else:
    if (x//d)%2 == k%2:
        ans = abs(x-d*r)
        print(ans)
        exit()
    else:
        ans = abs(x-d*l)
        print(ans)
        exit()

Submission status: Wrong Answer

Solution code:
a, b, c = map(int, input().split())
d = 0
e = a

if a > 0:
    for i in range(b):
        a -= c
        if a < 0:
            d = b - i - 1
            break
    if d == 0:
        print(abs(e - b * c))
    else:
        if abs(a + c) > abs(a):
            if d % 2 == 1:
                print(abs(a))
            else:
                print(abs(a + c))
        else:
            if d % 2 == 1:
                print(abs(a + c))
            else:
                print(abs(a))

else:
    for i in range(b):
        a += c
        if a > 0:
            d = b - i - 1
            break
    if d == 0:
        print(abs(e - b * c))
    else:
        if abs(a - c) < abs(a):
            if d % 2 == 1:
                print(abs(a))
            else:
                print(abs(a + c))
        else:
            if d % 2 == 1:
                print(abs(a + c))
            else:
                print(abs(a))

Submission status: Wrong Answer

Solution code:
[x, k, d] = list(map(int, input().split()))
howMany = x // d
remainder = x % d
stepLeft = k - howMany
if k < howMany:
    print((howMany - k) * d)
elif stepLeft % 2 == 0:
    print(remainder)
elif stepLeft % 2 == 1:
    print(d - remainder)

Submission status: Wrong Answer

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

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