LoginSignup
1
1

More than 3 years have passed since last update.

ABC175C 【場合分け】

Last updated at Posted at 2020-12-13

ABC175C
後で解くとき、忘れそうなのでメモとして残します。


abs(X/D)回移動すると、座標0に辿り着く。
整数であるn1 = int(abs(X/D))回 か n2 = n1+1回のとき、
最も座標0に近づく。(近づいたあとは、その場を往復してやり過ごす)

n1とn2で最終的に留まれるのがどちらか、Kの偶奇と等しい方を選択する。

また、Kがn1以下の場合は、D*Kの移動が一番座標0に近づく。

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

n1 = int(abs(X/D))
n2 = n1+1

if K <= n1:
    tmp = D*K
elif n2 <=K:
    if K %2 == n2%2:
        tmp = D*n2
    else:
        tmp = D*n1

if X <0:
    print(abs(X+tmp))
else:
    print(abs(X-tmp))

お読み頂きありがとうございます。

URL
https://atcoder.jp/contests/abc175/tasks/abc175_c

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1