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
47
#include <bits/stdc++.h>


using namespace std;


long long solve(int h, int w, int n, vector <int> &d) {
    if (h > w) {
        swap(h, w);
    }

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

    for (int i = 0; i < n; i++) if (i == n - 1 || d[i + 1] > h) {
        long long part1 = (long long) (w / d[i]) * (h / d[i] + solve(h % d[i], d[i], n, d));
        long long part2 = solve(w % d[i], h, n, d);

        return part1 + part2;
    }

    assert(false);
}

int main() {
    ios_base::sync_with_stdio(false);

    int h, w;
    cin >> h >> w;

    int n;
    cin >> n;

    vector <int> d(n);
    for (int &di : d) {
        cin >> di;
    }

    if (h % d[0] || w % d[0]) {
        cout << -1;
    } else {
        cout << solve(h, w, n, d);
    }

    return 0;
}