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
import math

h, w = [int(x) for x in input().split(" ")]
n = input()

ds = list(map(lambda x: int(x), input().split(" ")))

def findMaxD(h, w, ds):
    max_d = 0
    for d in ds:
        if d <= w and d <= h:
            max_d = d
        else:
            break
    return max_d

def images(h, w, ds):
    if h == 0 or w == 0:
        return 0
    
    d = findMaxD(h, w, ds)

    if d == 0: return 0

    upper = images(h - d, d, ds)
    right = images(h, w - d, ds)

    # if upper == -1 and right == -1:
    #     return -1

    return 1 + upper + right

def main():
    lcm_h = False
    lcm_w = False
    for d in ds:
        if not lcm_h and math.lcm(d, h) == h:
            lcm_h = True

        if not lcm_w and math.lcm(d, w) == w:
            lcm_w = True

    if lcm_w and lcm_h:
        print(images(h, w, ds))
    else:
        print(-1)

main()