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

long long bestfit(long long h, long long w, const std::vector<long long>& obr, int n, int start_index) {
    int bestind;

    for (int i = start_index; i >= 0; i--) {
        if (obr[i] <= h && obr[i] <= w) {
            bestind = i;
            break;
        }
    }

    long long res = (h/obr[bestind]) * (w/obr[bestind]);
    if (h % obr[bestind] != 0)
        res += bestfit(h % obr[bestind], w - w % obr[bestind], obr, n, bestind - 1);
    if (w % obr[bestind] != 0)
        res += bestfit(h - h % obr[bestind], w % obr[bestind], obr, n, bestind - 1);
    if (h % obr[bestind] != 0 && w % obr[bestind] != 0)
        res += bestfit(h % obr[bestind], w % obr[bestind], obr, n, bestind - 1);
    return res;
}

int main() {
    std::vector<long long> obr;
    long long h, w;
    int n;
    std::cin >> h >> w >> n;
    for (int i = 0; i < n; i++) {
        long long o;
        std::cin >> o;
        obr.emplace_back(o);
    }
    if (h % obr[0] != 0 || w % obr[0] != 0) {
        std::cout << -1;
        return 0;
    }

    std::cout << bestfit(h, w, obr, n, n - 1);

    return 0;
}