Skip to content

Instantly share code, notes, and snippets.

@uchidama
Created July 8, 2021 16:00
Show Gist options
  • Save uchidama/63153e363a821847670533d0ec2da453 to your computer and use it in GitHub Desktop.
Save uchidama/63153e363a821847670533d0ec2da453 to your computer and use it in GitHub Desktop.
AtCoder Beginner Contest 175 [ C - Walking Takahashi ] https://atcoder.jp/contests/abc175/tasks/abc175_c
'''
[問題]
https://atcoder.jp/contests/abc175/tasks/abc175_c
'''
import sys
import math
sys.setrecursionlimit(10 ** 6) # 再帰上限の引き上げ
input = sys.stdin.readline
INF = 2 ** 63 - 1
X, K, D = map(int, input().split())
# X がマイナス座標開始だった時の対応
if X < 0:
X *= -1
# XがDより小さくなるまで移動する回数をカウント
a = math.ceil(X/D - 1)
if a >= K:
print(abs(X-D*K))
exit()
K -= a
# Dが0を超える方向に移動、戻るの繰り返し
if K%2 == 0:
print(abs(X-D*a))
else:
print(abs(X-D*(a+1)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment