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
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;

LL h, w, n;
LL d[44];

LL f(LL h, LL w, int i) {
	LL res = 0;
	for (; i < n; i++) {
		if (d[i] <= h && d[i] <= w) {
			res += (h / d[i]) * (w / d[i]);
			break;
		}
	}

	if (h % d[i]) {
		res += f(h % d[i], (w / d[i]) * d[i], i + 1);
	}
	if (w % d[i]) {
		res += f((h / d[i]) * d[i], w % d[i], i + 1);
	}
	if (h % d[i] && w % d[i]) {
		res += f(h % d[i], w % d[i], i + 1);
	}

	return res;
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	cin >> h >> w;
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> d[i];
	}
	if (h % d[0] != 0 || w % d[0] != 0) {
		cout << "-1\n";
		return 0;
	}
	reverse(d, d + n);
	cout << f(h, w, 0) << "\n";

	return 0;
}