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

int d[35];

long long cover(long long h, long long w, int n){
    if(h == 0 || w == 0) return 0;
    // if(h > w) return cover(w, h, n);

    int i = n;
    while(d[i] > h || d[i] > w) i--;
    long long h1 = (h/d[i]);
    long long w1 = (w/d[i]);
    long long cnt = h1*w1;
    
    return cnt + cover(h-h1*d[i], w, n) + cover(h1*d[i], w-w1*d[i], n);

}

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

    cout<<cover(h, w, n)<<endl;
}