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

using namespace std;

vector<long long> O;

long long pictures(long long h, long long w, auto idx) {
	if (h == 0 || w == 0) return 0;
	if (idx == O.rend()) return -1;
	long long ch = h / *idx;
	long long cw = w / *idx;
    long long	sum = ch * cw;
    //printf("%d %d %d\n", h, w, sum);

	long long right_w = w - cw * *idx;
	long long right_h = ch * *idx;

	long long bottom_w = w;
	long long bottom_h = h - ch * *idx;

	auto right_idx = idx;
	right_idx++;
	long long c = pictures(right_h, right_w, right_idx);
	if (c == -1) return -1;
	sum += c;

	auto bottom_idx = idx;
	bottom_idx++;
	c = pictures(bottom_h, bottom_w, bottom_idx);
	if(c == -1) return -1;
	sum += c;

	return sum;
}

int main() {
	long long h,w,d,n;
	scanf("%lld%lld", &h, &w);
	scanf("%lld", &n);
	while(n--) {
		scanf("%lld", &d);
		O.push_back(d);
	}

	printf("%lld\n", pictures(h, w, O.rbegin()));
}