w,h = map(int, input().split()) k = int(input()) s = list(map(int,input().split())) s.sort() if w % s[0] + h % s[0] > 0: print(-1) exit(0) s.reverse() p = 0 c = 0 while w > 0 and h > 0: # w is always the larger dimension if w < h: w, h = h, w # adjust the largest possible square based on h while p < k and s[p] > h: p += 1 # figure out the strip width tw = w - (w % s[p]) th = h # greedily divide txh strip into squares for i in range(p, k): c += (th // s[i]) * (tw // s[i]) th %= s[i] w %= s[p] print(c)
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 | w,h = map(int, input().split()) k = int(input()) s = list(map(int,input().split())) s.sort() if w % s[0] + h % s[0] > 0: print(-1) exit(0) s.reverse() p = 0 c = 0 while w > 0 and h > 0: # w is always the larger dimension if w < h: w, h = h, w # adjust the largest possible square based on h while p < k and s[p] > h: p += 1 # figure out the strip width tw = w - (w % s[p]) th = h # greedily divide txh strip into squares for i in range(p, k): c += (th // s[i]) * (tw // s[i]) th %= s[i] w %= s[p] print(c) |