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
// #pragma GCC optimize("O3,unroll-loops")
#include <bits/stdc++.h>
// #include <x86intrin.h>
using namespace std;
#if __cplusplus >= 202002L
using namespace numbers;
#endif



int main(){
	cin.tie(0)->sync_with_stdio(0);
	cin.exceptions(ios::badbit | ios::failbit);
	int h, w, n;
	cin >> h >> w >> n;
	vector<int> a(n);
	copy_n(istream_iterator<int>(cin), n, a.begin());
	map<array<int, 2>, long long, greater<>> q{{{min(h, w), max(h, w)}, 1}};
	long long res = 0;
	while(!q.empty()){
		auto [shape, cnt] = *q.begin();
		q.erase(q.begin());
		auto [h, w] = shape;
		if(!h || !w){
			continue;
		}
		int i = int(ranges::upper_bound(a, min(h, w)) - a.begin()) - 1;
		if(!~i){
			cout << "-1\n";
			return 0;
		}
		int x = a[i];
		res += cnt * (h / x) * (w / x);
		for(auto [nh, nw, coef]: {array{h % x, x, w / x}, array{x, w % x, h / x}, array{h % x, w % x, 1}}){
			if(!coef){
				continue;
			}
			q[{min(nh, nw), max(nh, nw)}] += coef * cnt;
		}
	}
	cout << res << "\n";
	return 0;
}

/*

*/