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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <bits/stdc++.h>
using namespace std;

vector<int> v;
int n;

long long ile(int h, int w) {
    if(h==0 || w==0) return 0;
    int idx = -1;
    for(int i=n-1; i>=0; i--) {
        if(v[i] <= h && v[i] <= w) {
            idx = i;
            break;
        }
    }
    if(idx == -1) return -1;

    long long ileW = w/v[idx];
    long long ileH = h/v[idx];
    long long wyn = ileW * ileH;

    long long dol = ile(h%v[idx],v[idx]*ileW);
    long long bok = ile(v[idx]*ileH,w%v[idx]);
    long long skos = ile(h%v[idx], w%v[idx]);

    if(dol == -1 || bok == -1 || skos == -1) return -1;
    return wyn + dol + bok + skos;
}

int main(){
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);
    int W,H;
    cin>>H>>W>>n;
    v = vector<int>(n);
    for(int i=0;i<n;i++){
        cin>>v[i];
    }

    sort(v.begin(), v.end());
    long long w = ile(H,W);
    cout<<w<<"\n";
}

/*
 10 10
 3
 1 2 10

 6 10
3
1 3 6

 3 3
1
2

 4 4
 2 3 1

 8 7
 3 1 6 2

  8 7
 2 6 2

 1000000000
 1000000000
 1 1

 */