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
#include <iostream>

using namespace std;

const int SIZE = 30;

long long d[SIZE];

long long fit(long long h, long long w, int n) {
    if (h == 0 || w == 0) {
        return 0L;
    }
    long long ih = h / d[n];
    long long iw = w / d[n];
    return ih*iw + fit(h-ih*d[n], iw*d[n], n-1) + fit(h, w-iw*d[n], n-1);
}

int main(int argc, char const *argv[])
{
    long long h, w;
    int n;
    cin >> h >> w >> n;
    for (int i=0; i<n; i++) {
        cin >> d[i];
    }
    
    if (h % d[0] > 0 || w % d[0] > 0) {
        cout << -1 << endl;
        return 0;
    }

    cout << fit(h, w, n-1) << endl;

    return 0;
}