#!/usr/bin/env python3
import sys
class NotPossible(Exception):
pass
def foo(x, ds):
out = []
for d in ds:
k, x = divmod(x, d)
if k > 0:
out.append((d, k))
if x == 0:
break
if x != 0:
raise NotPossible()
return out
def main():
h, w = map(int, sys.stdin.readline().split())
n = int(sys.stdin.readline())
ds = list(map(int, sys.stdin.readline().split()))
assert len(ds) == n
ds.reverse()
ds = [d for d in ds if d <= min(w, h)]
try:
c1 = foo(h, ds)
c2 = foo(w, ds)
except NotPossible:
print('-1')
return
out = 0
for dd, kk in c2:
for d, k in c1:
out += max(d, dd) // min(d, dd) * k * kk
print(out)
if __name__ == "__main__":
main()
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 | #!/usr/bin/env python3 import sys class NotPossible(Exception): pass def foo(x, ds): out = [] for d in ds: k, x = divmod(x, d) if k > 0: out.append((d, k)) if x == 0: break if x != 0: raise NotPossible() return out def main(): h, w = map(int, sys.stdin.readline().split()) n = int(sys.stdin.readline()) ds = list(map(int, sys.stdin.readline().split())) assert len(ds) == n ds.reverse() ds = [d for d in ds if d <= min(w, h)] try: c1 = foo(h, ds) c2 = foo(w, ds) except NotPossible: print('-1') return out = 0 for dd, kk in c2: for d, k in c1: out += max(d, dd) // min(d, dd) * k * kk print(out) if __name__ == "__main__": main() |
English