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
47
48
49
50
51
52
53
#include <bits/stdc++.h>
using namespace std;

void input(long long &h, long long &w, int &n, vector <long long> &v){
    cin >> h >> w >> n;

    v.resize(n);

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

    return;
}

void solve(long long h, long long w, int n, vector <long long> &v){
    sort(v.begin(), v.end());

    if(h % v[0] != 0 || w % v[0] != 0){
        cout << "-1";

        return;
    }

    long long wynik = (h * w / (v[0] * v[0]));

    for(int i = 1; i < n; i++){
        long long k = h / v[i], l = w / v[i];

        wynik -= (((v[i] * v[i]) / (v[i - 1] * v[i - 1])) * k * l);
        
        wynik += (k * l);
    }

    cout << wynik;

    return;
}

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

    int n;
    long long h, w;
    vector <long long> v;

    input(h, w, n, v);
    solve(h, w, n, v);

    return 0;
}