def calculate_response(w, h, arr): x, y = w, h cur_dim, dim_idx = 0, 0 # print("arr, w, h", arr, x, y) sum = 0 while True: for i in range(dim_idx, len(arr)): if arr[i] > x or arr[i] > y: continue cur_dim = arr[i] dim_idx = i break #print("begin with", cur_dim, dim_idx) times_x = (x // cur_dim) x = x - cur_dim * times_x times_y = (y // cur_dim) diff_y = cur_dim * times_y y = y - diff_y sum = sum + times_x*times_y #print("x // cur_dim", x, cur_dim) #print("y // cur_dim", y, cur_dim) if x == 0: if y == 0: break x = w continue for i in range(dim_idx + 1, len(arr)): if arr[i] > x: continue times_x = (x // arr[i]) x = x - arr[i] * times_x sum = sum + times_x*(diff_y // arr[i]) if x == 0: if y == 0: return sum x = w break return sum if __name__ == "__main__": dim = input() w, h = [int(elem) for elem in dim.split()] n = int(input()) arr_in = input() arr = [int(elem) for elem in arr_in.split()] arr.reverse() if h % arr[-1] != 0 or w % arr[-1] != 0: print("-1") else: print(calculate_response(w, h, arr))
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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | def calculate_response(w, h, arr): x, y = w, h cur_dim, dim_idx = 0, 0 # print("arr, w, h", arr, x, y) sum = 0 while True: for i in range(dim_idx, len(arr)): if arr[i] > x or arr[i] > y: continue cur_dim = arr[i] dim_idx = i break #print("begin with", cur_dim, dim_idx) times_x = (x // cur_dim) x = x - cur_dim * times_x times_y = (y // cur_dim) diff_y = cur_dim * times_y y = y - diff_y sum = sum + times_x*times_y #print("x // cur_dim", x, cur_dim) #print("y // cur_dim", y, cur_dim) if x == 0: if y == 0: break x = w continue for i in range(dim_idx + 1, len(arr)): if arr[i] > x: continue times_x = (x // arr[i]) x = x - arr[i] * times_x sum = sum + times_x*(diff_y // arr[i]) if x == 0: if y == 0: return sum x = w break return sum if __name__ == "__main__": dim = input() w, h = [int(elem) for elem in dim.split()] n = int(input()) arr_in = input() arr = [int(elem) for elem in arr_in.split()] arr.reverse() if h % arr[-1] != 0 or w % arr[-1] != 0: print("-1") else: print(calculate_response(w, h, arr)) |