-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathabc255_c.py
More file actions
48 lines (43 loc) · 1.26 KB
/
abc255_c.py
File metadata and controls
48 lines (43 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
###############################################
###############################################
###############################################
###############################################
###############################################
###############################################
[kick]
X, A, D, N = map(int, input().split())
if D < 0:
A = A + D * (N - 1)
D = -D
if X < A:
print(A - X)
elif A <= X < A + D * (N - 1):
if D == 0:
print(0)
else:
print(min((X - A) % D, D - (X - A) % D))
else:
print(X - A - D * (N - 1))
###############################################
###############################################
[cpython,WA9][pypy,WA9]
x, a, d, n = map(int, input().split())
ans = abs(a - x)
left = 0
right = n
while right > left + 2:
mid1 = (2 * left + right) // 3
mid2 = (left + 2 * right) // 3
score1 = abs(a + d * mid1 - x)
score2 = abs(a + d * mid2 - x)
if score1 > score2:
left = mid1
else:
right = mid2
for k in range(left, min(right + 1, n + 1)):
ans = min(ans, abs(a + d * k - x))
print(ans)
###############################################
###############################################
###############################################
###############################################