Skip to content

Instantly share code, notes, and snippets.

@kunishi
Created June 11, 2022 14:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kunishi/14f687963680943c1a4cf6e0287231be to your computer and use it in GitHub Desktop.
Save kunishi/14f687963680943c1a4cf6e0287231be to your computer and use it in GitHub Desktop.
AtCoder ABC255 Problem C
X, A, D, N = map(int, input().split())
first = min(A, A + D * (N - 1))
last = max(A, A + D * (N - 1))
if X <= first:
print(abs(first - X))
elif X >= last:
print(abs(X - last))
else:
a_min = A + D * ((X - A) // D)
a_max = a_min + D
print(min(abs(X - a_min), abs(a_max - X)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment