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

using namespace std;

const int N = 30;

//unordered_map<pair<int, int>, long long> dp;

int dims[N];

long long pokryj(int n, int h, int w) {
    if (h == 0 || w == 0)
        return 0;
    if (h < dims[0] || w < dims[0])
        return -1;
    
    int i;
    for (i = 0; i < n; i++)
        if (dims[i] > h || dims[i] > w)
            break;
    i--;
    long long left = pokryj(n, h, w % dims[i]);
    long long right = pokryj(n, h % dims[i], w - w % dims[i]);
    if (left == -1 || right == -1)
        return -1;
    return left + right + (long long)(h / dims[i]) * (long long)(w / dims[i]);
}

int main() {
    int n, h, w;
    cin >> h >> w;
    cin >> n;
    for (int i = 0; i < n; i++)
        cin >> dims[i];
    
    cout << pokryj(n, h, w) << "\n";
    return 0;
}