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

using namespace std;

long long solve(const vector<long long> &P, long long h, long long w)
{
    vector<long long> C(P.size());
    C[0] = h/P[0] * w/P[0];

    for(int i=1; i<P.size(); i++)
    {
        C[i] = (h/P[i]) * (w/P[i]);
        long long q = P[i]/P[i-1] * P[i]/P[i-1];
        C[i-1] -= C[i]*q;
    }

    long long s = 0;
    for(auto it:C) s += it;
    return s;
}

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

    long long h, w, n;
    cin >> h >> w >> n;
    vector<long long> P(n);
    for(auto &it:P) cin >> it;

    if(h % P[0] > 0 || w % P[0] > 0) cout << -1 << "\n";
    else cout << solve(P, h, w) << "\n";
}