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;

int find_max_id(long long w, vector<long long> &img, long long h) {
    int max_i=0;
    for(max_i; max_i<img.size(); ++max_i) {
        if(img[max_i] <= w && img[max_i] <= h)
            continue;
        else {
            max_i--;
            break;
        }
    }
    if(max_i == img.size())
        max_i--;
    return max_i;
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    long long h,w,n;
    cin>>h>>w>>n;
    vector<long long> img(n);
    for(int i=0; i<n; ++i)
        cin>>img[i];
    
    if(h%img[0] != 0 || w%img[0] != 0) {
        cout<<-1<<'\n';
        return 0;
    }

    long long ans=0, ans_tmp=0;
    int org_w = w;
    int id;
    while(h > 0) {
        int first_id = find_max_id(org_w, img, h);
        int id = first_id;
        w = org_w;
        ans_tmp = 0;
        while(w > 0) {
            ans_tmp += (w/img[id])*(img[first_id]/img[id]);
            w -= img[id]*(w/img[id]);
            id = find_max_id(w, img, img[first_id]);
        }
        ans += (h/img[first_id])*ans_tmp;
        h -= img[first_id]*(h/img[first_id]);
    }

    cout<<ans<<'\n';
    return 0;
}