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

using namespace std;
using LL = long long;

LL solve(int h, int w, int n, const vector<int>& d)
{
	for (int i = n - 1; i >= 0; --i)
	{
		if (d[i] <= h && d[i] <= w)
		{
			int cnt_h = h / d[i];
			int cnt_w = w / d[i];

			return 1LL * cnt_h * cnt_w + solve(h - cnt_h * d[i], w, n, d) + solve(cnt_h * d[i], w - cnt_w * d[i], n, d);
		}
	}

	return 0;
}

int main()
{
	int h, w, n;
	cin >> h >> w >> n;

	vector<int> d(n);

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

	LL res = -1;

	if (h % d[0] == 0 && w % d[0] == 0)
		res = solve(h, w, n, d);

	cout << res << endl;

	return 0;
}