-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp_fast.py
More file actions
35 lines (27 loc) · 685 Bytes
/
p_fast.py
File metadata and controls
35 lines (27 loc) · 685 Bytes
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
import sys
input = sys.stdin.readline
def extgcd(a, b):
if b == 0:
return a, 1, 0
g, y, x = extgcd(b, a % b)
y -= a // b * x
return g, x, y
def main():
n = int(input())
a, b, c = reversed(sorted(map(int, input().split())))
lim = 10000
ans = lim
g, x, y = extgcd(b, c)
for i in range(lim):
rem = n - a * i
if rem >= 0 and rem % g == 0:
j = x * (rem // g)
k = y * (rem // g)
tk = k % (b // g)
j -= (tk - k) // (b // g) * (c // g)
k = tk
if j >= 0:
ans = min(ans, i + j + k)
print(ans)
if __name__ == "__main__":
main()