利用最大公倍数和最小公因数来减小枚举空间

from math import gcd
n,a,b = map(int,input().split())
c = gcd(a,b)
d = a*b//c
l = set()
for i in range(b//c):
    for j in range(a//c):
        temp = i*a+j*b
        if temp < d:
            l.add(temp)
n %= d
min1 = n
for i in l:
    if n-i >= 0:
        min1 = min(min1,n-i)
print(min1)

c是最大公因数,d是最小公倍数,l是个集合,来枚举。