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
#!/usr/bin/python3


H, W = input().split()
H, W = int(H), int(W)

n = int(input())
D = [int(d) for d in input().split()]
assert(len(D) == n)


def solve():
    if H % D[0] or W % D[0]:
        return -1

    d = D.pop()
    ile_H, left_H = divmod(H, d)
    ile_W, left_W = divmod(W, d)
    wy = ile_H * ile_W

    while left_H or left_W:
        d = D.pop()
        ile_H, left_H = divmod(left_H, d)
        ile_W, left_W = divmod(left_W, d)
        # print(f"d={d}: ile_H={ile_H}, left_H={left_H}, ile_W={ile_W}, left_W={left_W}")
        wy += ile_H * (W // d)
        wy += ile_W * (H // d)
        wy -= ile_H * ile_W  # bo zasada włączeń i wyłączeń
        # print(f"wy={wy}")

    return wy


print(solve())