Problem: 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.

Submission attempts (in order):
from sys import stdin


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

    if x == 0:
        if k % 2 == 0:
            return 0
        elif k % 2 == 1:
            return d
    elif x > 0:
        tmp = abs(x) / d
        if d * tmp > d * k:
            return x - d * k
        else:
            return f((x-d*tmp), (k-tmp), d)
    elif x < 0:
        tmp = abs(x) / d
        if d * tmp > d * k:
            return x + d * k
        else:
            return f((x+d*tmp), (k-tmp), d)


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


from sys import stdin


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

    absX = abs(x)
    tmp = absX / d

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


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

from sys import stdin


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

    tmp = abs(x) / d
    if x == 0:
        if k % 2 == 0:
            return 0
        elif k % 2 == 1:
            return d

    elif x > 0:
        if tmp > k:
            return x - d * k
        else:
            x = x - d * tmp
            k = k - tmp
            if k % 2 == 0:
                return x
            else:
                return x - d
    elif x < 0:
        if tmp > k:
            return x + d * k
        else:
            x = x + d * tmp
            k = k - tmp
            if k % 2 == 0:
                return x
            else:
                return x + d


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


from sys import stdin


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

    tmp = abs(x) / d
    if x == 0:
        if k % 2 == 0:
            return 0
        elif k % 2 == 1:
            return d

    elif x > 0:
        if tmp > k:
            return x - d * k
        else:
            x = x - d * tmp
            k = k - tmp
            if k % 2 == 0:
                return x
            else:
                return x - d
    elif x < 0:
        if tmp > k:
            return x + d * k
        else:
            x = x + d * tmp
            k = k - tmp
            if k % 2 == 0:
                return x
            else:
                return x + d


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


from sys import stdin


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

    tmp = x // d

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


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


Verdicts: Runtime Error

Wrong Answer

Wrong Answer

Wrong Answer

Accepted