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/env python3

import sys


def main():
    H, W = [int(x) for x in input().split()]
    input()
    D = [int(x) for x in input().split()]
    D.reverse()
    hs = {}
    ws = {}
    h, w = H, W
    for d in D:
        if (num := h // d) != 0:
            hs[d] = num
        if (num := w // d) != 0:
            ws[d] = num
        h = h % d
        w = w % d
    if h > 0 or w > 0:
        result = -1
    else:
        result = 0
        for h, nh in hs.items():
            for w, nw in ws.items():
                result += nh * nw * (h // w if h >= w else w // h)
    print(result)


if __name__ == "__main__":
    if len(sys.argv) == 2:
        sys.stdin = open(sys.argv[1])
    main()