def main(): line = input().split() h, w = int(line[0]), int(line[1]) d = int(input()) sizes = list(reversed([int(d) for d in input().split()])) if h % sizes[-1] != 0 or w % sizes[-1] != 0: print(-1) return def count(h, w): # print(f"{h=}, {w=}") for size in sizes: if h < size or w < size: continue # h >= size and w >= size a = h // size b = w // size reminder_a = h % size reminder_b = w % size res = a * b if reminder_a > 0: res += count(size * b, reminder_a) if reminder_b > 0: res += count(reminder_b, h) return res print(count(h, w)) 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 | def main(): line = input().split() h, w = int(line[0]), int(line[1]) d = int(input()) sizes = list(reversed([int(d) for d in input().split()])) if h % sizes[-1] != 0 or w % sizes[-1] != 0: print(-1) return def count(h, w): # print(f"{h=}, {w=}") for size in sizes: if h < size or w < size: continue # h >= size and w >= size a = h // size b = w // size reminder_a = h % size reminder_b = w % size res = a * b if reminder_a > 0: res += count(size * b, reminder_a) if reminder_b > 0: res += count(reminder_b, h) return res print(count(h, w)) if __name__ == "__main__": main() |