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

#define pb push_back
#define st first
#define nd second

using ll = long long;
using ld = long double;

using namespace std;

ll h, w, n;
vector <ll> v;
bool czy = 0;

ll rec(ll a, ll b, ll i) {
    if(i == -1) {
        if(a && b) {
            czy = 1;
        }
        return 0;
    }
    ll ans = 0;
    if(a >= v[i] && b >= v[i]) {
        ans += (a / v[i]) * (b / v[i]);
        ans += rec((a / v[i]) * v[i], b % v[i], i - 1);
        ans += rec(a % v[i], (b / v[i]) * v[i], i - 1);
        ans += rec(a % v[i], b % v[i], i - 1);
    }
    else {
        ans = rec(a, b, i - 1);
    }
    return ans;
}

int main() {
    ios_base::sync_with_stdio(false); cin.tie(nullptr);

    cin >> h >> w >> n;
    for(int i = 0; i < n; i++) {
        ll a;
        cin >> a;
        v.pb(a);
    }
    ll ans = rec(h, w, n - 1);
    if(czy) {
        cout << -1;
    }
    else {
        cout << ans;
    }

    return 0;
}