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
#include<bits/stdc++.h>
#define MAXN 31

using namespace std;

int n, m, k;
int tab[MAXN];
long long w;
bool o;

long long solve(int w, int h, int d) {
	if(d == -1) {
		o = true;
		return 0;
	}
	
	int aw = (w / tab[d]) * tab[d];
	int ah = (h / tab[d]) * tab[d];
	long long s = (long long)(w / tab[d]) * (long long)(h / tab[d]);
	
	if (w > aw && h > ah) {
		s += solve(w - aw, h - ah, d - 1);
	}
	if (h > ah && aw > 0) {
		s += solve(aw, h - ah, d - 1);
	}
	if (w > aw && ah > 0) {
		s += solve(w - aw, ah, d - 1);
	}
	
	return s;
}

int main() {
	
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	
	o = false;

	cin >> n >> m >> k;
	for (int i = 0; i < k; i++) {
		cin >> tab[i];
	}
	
	w = solve(n, m, k - 1);
	
	cout<<(o ? -1 : w)<<"\n";
	
	return 0;
}