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

typedef unsigned long long int ULONG;

ULONG solve(ULONG H, ULONG W, const std::vector<ULONG> &len) {
    ULONG s = std::min(H, W);

    if (s==0) {
        return 0;
    }

    int index = len.size()-1;
    while (index > 0 && len[index] > s) {
        index--;
    }

    ULONG hcnt = H / len[index];
    ULONG wcnt = W / len[index];
    ULONG result = hcnt * wcnt;

    result += solve(H-hcnt*len[index], wcnt*len[index], len);
    result += solve(H, W-wcnt*len[index], len);
    return result;
}

int main() {
    int H,W,N;
    std::vector<ULONG> len;

    scanf("%d %d %d", &H, &W, &N);
    for (int i=0; i<N; ++i) {
        int d;
        scanf("%d", &d);
        len.push_back(d);
    }

    if (H % len[0] != 0 || W % len[0] != 0) {
        printf("-1\n");
    } else {
        ULONG res = solve(H,W,len);
        printf("%llu\n", res);
    }

    return 0;
}