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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <bits/stdc++.h>
using namespace std;

constexpr int maxN = 30;

int h, w;
int n;
long long d[maxN + 1];

long long side[maxN + 1], cost[maxN + 1];
unordered_map<long long, long long> mem;

long long hash_p(pair<int, int> p) {
    return ((long long)p.first << 32) + p.second;
}

long long solve(int h_, int w_) {
    if(h_ > w_) {
        swap(h_, w_);
    }

    if(h_ == 0) {
        return 0;
    }

    if(mem[hash_p({h_, w_})] != 0) {
        return mem[hash_p({h_, w_})];
    }

    for(int i=maxN; i>=1; i--) {
        if(d[i] <= h_) {
            long long res[2];

            res[0] = solve(h_, w_ % d[i]);
            if(res[0] == -1) {
                return -1;
            }

            res[1] = solve(h_ % d[i], (w_ / d[i]) * d[i]);
            if(res[1] == -1) {
                return -1;
            }

            return mem[hash_p({h_, w_})] = ((h_ / d[i]) * (w_ / d[i]) * cost[i]) + res[0] + res[1];
        }
    }

    return -1;
}

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

    cin >> h >> w;
    cin >> n;

    for(int i=1; i<=n; i++) {
        cin >> d[i];

        side[i] = cost[i] = 1;
    }

    for(int i=n+1; i<=maxN; i++) {
        d[i] = (d[i - 1] << 1);

        side[i] = (side[i - 1] << 1);
        cost[i] = side[i] * side[i];
    }

    cout << solve(h, w);
}