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
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, lower, upper) for(int (i) = (lower); i <= (int)(upper); (i)++)
#define FORD(i, upper, lower) for(int (i) = (upper); i >= (int)(lower); (i)--)

long long h, w;
int n;
long long d[31];
long long ans;

void solve(long long h, long long w, int i) {
    //cout << h << ' ' << w << ' ' << i << ' ';
    if(h == 0 || w == 0) return;
    if(i == n) {
        ans += (h/d[i]) * (w/d[i]);
        return;
    }
    ans += ((h % d[i + 1]) * w + (w % d[i + 1]) * h - (h % d[i + 1]) * (w % d[i + 1])) / (d[i] * d[i]);
    //cout << ((h % d[i + 1]) * w + (w % d[i + 1]) * h - (h % d[i + 1]) * (w % d[i + 1])) / (d[i] * d[i]) << '\n';
    solve(h - (h % d[i + 1]), w - (w % d[i + 1]), i + 1);
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); 
    cout.tie(0);
    cin >> h >> w >> n;
    FOR(i, 1, n) {
        cin >> d[i];
    }
    if(h % d[1] + w % d[1] != 0) {
        cout << "-1";
        return 0;
    }
    solve(h, w, 1);
    cout << ans;
    return 0;
}