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
#include <cstdio>
#include <cstdint>
#include <vector>
#include <stack>

using namespace std;

vector<int> sizes;

int main () {
	int h, w;
	scanf("%d %d", &h, &w);
	int n;
	scanf("%d", &n);
	for (int i = 0; i < n; ++i) {
		int x;
		scanf("%d", &x);
		sizes.push_back(x);
	}
	uint64_t res = 0;
	stack<pair<int, pair<int, int> > > st;
	st.push(make_pair(n - 1, make_pair(w, h)));
	while (!st.empty()) {
		pair<int, pair<int, int> > item = st.top();
		st.pop();
		int d = item.first;
		int w = item.second.first;
		int h = item.second.second;
		if (d < 0) {
			if ((w > 0) && (h > 0)) {
				printf("-1\n");
				return 0;
			}
			else {
				continue;
			}
		}
		if ((w == 0) || (h == 0)) {
			continue;
		}
		int s = sizes[d];
		if ((w < s) && (h < s)) {
			st.push(make_pair(d - 1, make_pair(w, h)));
		}
		else {
			res += (uint64_t)(w / s) * (uint64_t)(h / s);
			st.push(make_pair(d - 1, make_pair(w % s, h)));
			st.push(make_pair(d - 1, make_pair(w - w % s, h % s)));
		}
	}
	printf("%lld\n", res);
	return 0;
}