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

#define int long long
int wyn,n;
vector<int> v;

void licz(int w, int h, int kt){
    if (v[kt] > w || v[kt] > h) return licz(w, h, kt+1);
    // cerr<<w<<" "<<h<<" "<<v[kt]<<" "<<(int)(w/v[kt])*(int)(h/v[kt])<<"\n";
    wyn += (int)(w/v[kt])*(int)(h/v[kt]);
    if (w%v[kt]) licz(w%v[kt], h, kt+1);
    if (h%v[kt]) licz(w-(w%v[kt]), h%v[kt], kt+1);
}

int32_t main(){
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int w,h;
    cin>>w>>h>>n;
    v.resize(n);
    for (int i = 0; i < n; i++) cin>>v[i];
    sort(v.begin(), v.end(), greater<int>());
    if (w%v.back() != 0 || h%v.back() != 0){
        // cout<<w%v.back()<<" "<<h%v.back()<<" "<<v.back()<<"\n";
        cout<<"-1\n";
        return 0;
    }
    licz(w, h, 0);
    cout<<wyn<<"\n";
}