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

using namespace std;
int dim[30];

long long int countSquares(int h, int w, int n)
{
	if (dim[0] > h)
	{
		return 0;
	}

	int maxi = 0;
	int dist = h;
	long long int cntS = 0;

	while (dim[maxi + 1] <= h && maxi <= n - 2) maxi++;
	for (int i = maxi; i >= 0 && dist > 0; i--)
	{
		int count = dist / dim[i];
		cntS += count * (dim[maxi] / dim[i]);
		dist %= dim[i];
	}
	return cntS * (w / dim[maxi]) + countSquares(w % dim[maxi], h, maxi);
}

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

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

	if (w < h) swap(w, h);

	if (dim[0] > h || h % dim[0] != 0 || w % dim[0] != 0)
	{
		cout << "-1" << endl;
	}
	else
	{
		cout << countSquares(h, w, n) << endl;
	}

	return 0;
}