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
48
49
50
51
52
// https://sio2.mimuw.edu.pl/c/pa-2024-1/p/obr/
#include <bits/stdc++.h>
#define st first
#define nd second
#define pb push_back
#define all(x) x.begin(), x.end()
#define rep(i, a) for (int i = 0; i < (a); i++)
#define rep1(i, a) for (int i = 1; i <= (a); i++)
using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
const int MAX_N = 1;
vector<int> t;
bool no = 0;
ll sol(ll h, ll w, int x) {
    // cerr << ' ' << h << ' ' << w << ' ' << t[x] << '\n';
    // cerr << "  " << x << ' ' << t.size() << '\n';
    // cerr << "    "<<(x == t.size() - 1) << h % t[x] << ' ' << w % t[x] <<
    // '\n';
    if (no)
        return -1;
    if (h * w == 0)
        return 0LL;
    if ((x == t.size() - 1) && (h % t[x] != 0 || w % t[x] != 0)) {
        no = 1;
        return -1;
    }
    // if (w > h)
    //     swap(w, h);
    ll v = h / t[x];
    ll y = w / t[x];
    ll res = v * y;
    res += sol(h % t[x], w - w % t[x], x + 1);
    res += sol(h, w % t[x], x + 1);
    if (no)
        return -1;
    return res;
}
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int h, w, n;
    cin >> h >> w;
    cin >> n;
    rep(i, n) {
        int x;
        cin >> x;
        t.pb(x);
    }
    sort(all(t), greater<int>());
    cout << sol(h, w, 0);
}