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

long long solve(int h, int w, std::vector<int> data)
{
    long long res = 0LL;
    if (h && w)
    {
        auto x = data.rbegin();
        while (x != data.rend() && *x > std::min(h, w))
            x++;
        res += 1LL * (h / *x) * (w / *x);
        res += solve(h % *x, w % *x, data);
        res += solve(h - h % *x, w % *x, data);
        res += solve(h % *x, w - w % *x, data);
    }
    return res;
}

int main()
{
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(NULL);

    int h, w, n;
    long long res;

    std::cin >> h >> w >> n;

    std::vector<int> data(n);

    for (int i = 0; i < n; i++)
    {
        std::cin >> data[i];
    }

    if (w % data[0] || h % data[0])
        res = -1;
    else
        res = solve(h, w, data);

    std::cout << res << '\n';
}