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 ll long long

long long calc(ll a, ll b, ll c) {
    return (a / c) * (b / c);
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int a, b, n;
    cin >> a >> b;
    cin >> n;
    int x = -1;
    int y;
    long long res = 0;
    for (int i = 0; i < n; ++i) {
        cin >> y;
        if (a % y != 0 || b % y != 0) {
            if (x == -1) {
                cout << -1;
                return 0;
            }
            //cout << a << " " << b << " " << x << endl;
            res += calc (a % y, b, x);
            res += calc (b % y, a, x);
            if (a % y != 0 && b % y != 0) {
                res-=calc(a % y, b % y, x);
            }
            a -= a % y;
            b -= b % y;
        }
        x = y;
    }
    cout << res + calc(a, b, x);
}