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 <iostream>
#include <algorithm>
#include <unordered_set>
#include <vector>
#define ll long long

using namespace std;

int n, h, w;
vector <int> o = {0};

ll getNum(int h, int w) {
    if (h == 0 || w == 0) return 0;
    int a = 0;
    while (o[a] > h || o[a] > w) a++;
    if (!o[a]) return -1;
    ll wp = w / o[a];
    ll hp = h / o[a];
    ll res = wp * hp;
    ll top = getNum(h - hp * o[a], w);
    ll left = getNum(hp * o[a], w - wp * o[a]);
    if (top == -1 || left == -1) return -1;
    return res + top + left;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    
    cin >> h >> w >> n;
    for (int i = 0; i < n; i++) {
        int a;
        cin >> a;
        o.push_back(a);
    }
    reverse(o.begin(), o.end());
    cout << getNum(h, w) << "\n";
    return 0;
}