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
#include <cstdio>

int d[30], n;

static int split(int h, int w)
{
    int l = h < w ? h : w;
    int dx, lx, ly, mx, my;

    if (!h || !w)
        return 0;

    for (int i = n - 1; i >= 0; i--)
        if (d[i] <= l) {
            dx = d[i];
            break;
        }

    lx = w / dx;
    ly = h / dx;
    mx = lx * dx;
    my = ly * dx;

    return lx*ly + split(my, w - mx) + split(h - my, w);
}

int main(void)
{
    int h, w, i;

    scanf("%d%d%d", &h, &w, &n);

    for (i = 0; i < n; i++)
        scanf("%d", d + i);

    if ( (h % d[0]) || (w % d[0]) ) {
        printf("-1\n");
        return 0;
    }

    printf("%d\n", split(h, w));

    return 0;
}