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
#include <iostream>

constexpr int MAX_N = 30;

int d[MAX_N];

int main()
{
	std::ios_base::sync_with_stdio(false);
	std::cin.tie(nullptr);

	int h, w, n;
	std::cin >> h >> w >> n;

	for(int i = 0; i < n; ++i)
		std::cin >> d[i];

	if(w%d[0] || h%d[0])
		std::cout << "-1\n";
	else
	{
		int pow = n-1;
		long long result = 0;
		while(h)
		{
			while(w < d[pow] || h < d[pow]) --pow;
			for(int i = pow, cur_w = w, cur_h = h-h%d[pow]; i >= 0; --i)
				if(d[i] <= cur_w)
					result += (cur_w/d[i])*(long long)(cur_h/d[i]),
					cur_w %= d[i];
			h %= d[pow];
		}
		std::cout << result << '\n';
	}

	return 0;
}