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

long long get_num_of_paintings(long long h, long long w, const std::vector<int>& paintings, int idx) {
    if(h == 0 || w == 0) {
        return 0;
    }
    if(idx < 0) {
        return -1;
    }

    if(h < w) {
        std::swap(h, w);
    }
    long long current = paintings[idx];
    if(current > w) {
        return get_num_of_paintings(h, w, paintings, idx - 1);
    }

    long long horizontal = h / current;
    long long vertical = w / current;

    long long current_sum = horizontal * vertical;

    if(h % current != 0) {
        long long tmp = get_num_of_paintings(h % current, vertical * current, paintings, idx - 1);
        if(tmp == -1) {
            return -1;
        }
        current_sum += tmp;
    }
    if(w % current != 0) {
        long long tmp = get_num_of_paintings(h, w % current, paintings, idx - 1);
        if(tmp == -1) {
            return -1;
        }
        current_sum += tmp;
    }

    return current_sum;
}

int main() {
    std::ios_base::sync_with_stdio(0);
    int h, w, n;
    std::cin >> h >> w >> n;
    std::vector<int> paintings(n);
    for(int i = 0; i < n; ++i) {
        std::cin >> paintings[i];
    }

    auto res = get_num_of_paintings(h, w, paintings, n - 1);

    std::cout << res << "\n";

}