def ile(h,w,d): i = h//d j = w//d return [i,j] h, w = list(map(int,input().split())) n = int(input()) a = list(map(int,input().split())) if h%a[0] != 0 or w%a[0]!=0: print(-1) else: a = a[::-1] dp = [[h,w]] ans = 0 for i in range(n): d = a[i] ndp = [] for p in dp: ch, cw = p k, l = ile(ch, cw, d) ans += k*l if (ch%d)*l!=0: ndp.append([ch%d, l*d]) if k*(cw%d)!=0: ndp.append([k*d, cw%d]) if (ch%d)*(cw%d)!=0: ndp.append([ch%d, cw%d]) dp = ndp print(ans)
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 | def ile(h,w,d): i = h//d j = w//d return [i,j] h, w = list(map(int,input().split())) n = int(input()) a = list(map(int,input().split())) if h%a[0] != 0 or w%a[0]!=0: print(-1) else: a = a[::-1] dp = [[h,w]] ans = 0 for i in range(n): d = a[i] ndp = [] for p in dp: ch, cw = p k, l = ile(ch, cw, d) ans += k*l if (ch%d)*l!=0: ndp.append([ch%d, l*d]) if k*(cw%d)!=0: ndp.append([k*d, cw%d]) if (ch%d)*(cw%d)!=0: ndp.append([ch%d, cw%d]) dp = ndp print(ans) |