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

using namespace std;
typedef long long ll;

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

    ll X, Y;
    cin >> Y >> X;

    ll n;
    cin >> n;
    vector<ll> d(n);
    for (ll &di : d)
        cin >> di;

    if (X % d[0] != 0 or Y % d[0] != 0) {
        cout << "-1\n";
        return 0;
    }

    ll ans = 0;
    for (int i = 0; i + 1 < n; i++) {
        ll x = X % d[i + 1];
        ll y = Y % d[i + 1];

        ans += x * Y / d[i] / d[i] + y * (X - x) / d[i] / d[i];

        X -= x;
        Y -= y;
    }
    ans += X * Y / d[n - 1] / d[n - 1];

    cout << ans << "\n";

    return 0;
}