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

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

    if (h % sizes[0] != 0 || w % sizes[0] != 0) {
        std::cout << -1 << std::endl;
        return 0;
    }

    long long total = 0;
    if (h > w) std::swap(h, w);
    for (int i = n - 1; i >= 0; --i) {
        long long chunks = h / sizes[i];
        long long w_remaining = w;
        h = h % sizes[i];
        if (chunks > 0) {
            total += chunks * (w_remaining / sizes[i]);
            w_remaining = w_remaining % sizes[i];
            for (int j = i - 1; j >= 0; --j) {
                if (!w_remaining) break;
                chunks *= sizes[j + 1] / sizes[j];
                total += chunks * (w_remaining / sizes[j]);
                w_remaining = w_remaining % sizes[j];
            }
        }

        if (!h) break;
    }

    std::cout << total << std::endl;
    return 0;
}