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
//GRT_2018
#include <bits/stdc++.h>
#define PB push_back
#define ST first
#define ND second
#define all(x) x.begin(), x.end()
//mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

using namespace std;

using ll = long long;
using pi = pair<int,int>;
using vi = vector<int>;

const int nax = 50;
int n;
int d[nax];

bool bad;

ll cover(int a, int b, int num) {
    if (a == 0 || b == 0) return 0LL;
    while (num >= 0 && (d[num] > a || d[num] > b)) num--;
    if (num < 0) {
        bad = true;
        return 0LL;
    }
    ll me = (ll)(a / d[num]) * (b / d[num]);
    int r1 = a % d[num], r2 = b % d[num];
    return me + cover(r1, r2, num) + cover(a - r1, r2, num) + cover(r1, b - r2, num);
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
    int a, b;
    cin >> a >> b;
    cin >> n;
    for (int i = 0; i < n; ++i) cin >> d[i];
    ll ans = cover(a, b, n - 1);
    if (bad) cout << "-1";
    else cout << ans;
}