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

using namespace std;

typedef long long ll;

ll d[30];

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    ll h, w;
    cin >> h >> w;

    ll n;
    cin >> n;

    cin >> d[0];
    if(h%d[0] != 0 || w%d[0] != 0) {
        cout << -1 << "\n";
        return 0;
    }

    for(int i = 1; i < n; i++) {
        cin >> d[i];
    }

    ll r = 0;
    for(int i = 1; i < n; i++) {
        ll nh = h/d[i]*d[i];
        ll nw = w/d[i]*d[i];

        r += ((h - nh)/d[i - 1])*w/d[i - 1];
        r += ((w - nw)/d[i - 1])*h/d[i - 1];
        r -= ((h - nh)/d[i - 1])*((w - nw)/d[i - 1]);

        h = nh;
        w = nw;
    }

    cout << r + (h/d[n - 1])*(w/d[n - 1]) << "\n";

    return 0;
}