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

vector<int2> v;

/*int2 solv_recur(int w, int h, int trynow){
    if(w==0 or h==0) return 0;
    while(v[trynow]>w or v[trynow]>h)trynow--;
    int wsizetook=(w/v[trynow])*v[trynow];
    int hsizetook=(h/v[trynow])*v[trynow];
    int2 cost=(w/v[trynow])*(h/v[trynow]);
    return cost + solv_recur(w-wsizetook, h, trynow-1) + solv_recur(wsizetook, h-hsizetook,trynow-1);
}*/
int2 solv(int2 w, int2 h, int2 trynow){
    while(v[trynow]>w or v[trynow]>h)trynow--;
    int2 gotw=v[trynow];
    int2 goth=v[trynow];
    int2 cost=1;
    while(gotw < w or goth < h){
        if(v[trynow]<=w-gotw){
            cost += ((w-gotw)/v[trynow])*(goth/v[trynow]);
            gotw += ((w-gotw)/v[trynow])*v[trynow];
        }else if(v[trynow]<=h-goth){
            cost += ((h-goth)/v[trynow])*(gotw/v[trynow]);
            goth += ((h-goth)/v[trynow])*v[trynow];
        }else{
            trynow--;
        }
        //trynow--;
    }
    return cost;
}
int main(){
    std::ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int2 h,w;
    cin >> h >> w;
    int2 n;
    cin >> n;
    for(int2 i = 0; i < n; i++){
        int2 tmp;
        cin >> tmp;
        v.push_back(tmp);
    }
    if(h%v[0] != 0 or w%v[0] != 0){
        cout << "-1";
        return 0;
    }
    cout << solv(w, h, n-1);

}