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

unsigned long long sizes[32];

unsigned long long compute(unsigned long long w, unsigned long long h, unsigned long long maxs) {
    if (w == 0 || h == 0) {
        return 0;
    }

    if (w < sizes[maxs] || h < sizes[maxs]) {
        return compute(w, h, maxs - 1);
    }

    unsigned long long count = (w / sizes[maxs]) * (h / sizes[maxs]);
    count += compute(w % sizes[maxs], h, maxs - 1);
    count += compute(w - (w % sizes[maxs]), h % sizes[maxs], maxs - 1);
    return count;
}

int main() {
    unsigned int h, w;
    scanf("%u %u", &h, &w);

    unsigned int n;
    scanf("%u", &n);

    for (int i = 0; i < n; i++) {
        scanf("%llu", &sizes[i]);
    }

    if (h % sizes[0] != 0 || w % sizes[0] != 0) {
        puts("-1");
        return 0;
    }

    printf("%llu\n", compute(w, h, n - 1));
    return 0;
}