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

ll rec(ll w, ll h, vector <ll> &d, ll ind){
    //cout << h << " " << w << "\n";
    if(w == 0 || h == 0) return 0;

    while(ind >= 0 && d[ind] > h){
        ind--;
    }

    ll b = h%d[ind];
    ll a = w;

    ll c = h - b;
    ll dd = w%d[ind];

    if(c < dd) swap(c, dd);

    return rec(a, b, d, ind) + rec(c, dd, d, ind) + (w/d[ind]) * (h/d[ind]);
}

int main(){
    ll h, w;
    cin >> h >> w;

    ll n;
    cin >> n;

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

    if(h%d[0] || w%d[0]){
        cout << -1 << "\n";
        return 0;
    }

    if(w < h) swap(w, h); // w > h

    cout << rec(w, h, d, n-1) << "\n";
}