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
#include <bits/stdc++.h>
using namespace std;

vector<long long> t;

long long calc(long long h, long long w, long long i) {
    if (h == 0 || w == 0)
        return 0;
    if (i == -1)
        return -1;
    if (h < t[i] || w < t[i])
        return calc(h, w, i - 1);

    long long ans = (h / t[i]) * (w / t[i]);
    long long h_c = h % t[i];
    long long w_c = w % t[i];

    long long ans1 = calc(h_c, w - w_c, i - 1);
    long long ans2 = calc(h - h_c, w_c, i - 1);
    long long ans3 = calc(h_c, w_c, i - 1);

    if (ans1 == -1 || ans2 == -1 || ans3 == -1)
        return -1;

    return ans + ans1 + ans2 + ans3;
}

int main() {
    long long h, w, n;
    cin >> h >> w >> n;

    for (long long i = 0, a; i < n; i++) {
        cin >> a;
        t.push_back(a);
    }

    cout << calc(h, w, n - 1) << "\n";
}