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
#include <iostream>
#include <ranges>
#include <vector>
#include <algorithm>
#include <numeric>

std::vector<int> d;

long rec(int h, int w) {
	if(!(h && w)) return 0;
	//std::cerr << "Filling " << w << 'x' << h << '\n';
	for(auto current : d | std::views::reverse) {
		long xtimes = (w / current);
	long ytimes = (h / current);
		if(ytimes && xtimes) {
			int nw = w - xtimes * current;
			int nh = h - ytimes * current;
			//std::cerr << current << 'x' << xtimes * ytimes << ' ' << w << ' ' << h << '\n';
			long result = xtimes * ytimes;
#define P(...) {auto x = rec(__VA_ARGS__);if(x == -1) return -1; result += x;}
			P(nw, ytimes * current);
			P(w, nh);
			return result;
		}
	}
	return -1;
}

// ugly-style code (to match "10 lines" comment)
int main() {
	int h, w; std::cin >> h >> w;
	int n; std::cin >> n;
	d.resize(n);
	for(auto &x : d) std::cin >> x;
	std::ranges::sort(d);
	//std::cerr << w << ' ' << h << '\n';
	std::cout << rec(h, w) << '\n';
}