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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
using namespace std;

#define	DMAX	1000000001
#define	DD	30

int	 ddd[DD+1];
int	 dn;

int	 getd(int w, int h)
{
	int	i, x;

	x	= w;
	if (x > h)
		x	= h;
	
	i	= 0;
	while (x >= ddd[i+1])
		i++;

//	std::cout << "getd(" << w << "," << h << ") = " << ddd[i] << "\n";

	return ddd[i];
}

long long getN(int w, int h)
{
	long long r = 0;
	int	 wn, hn, d;

	if (w > 0 && h > 0)
	{
		d	= getd(w, h);
		wn	= w / d;
		hn	= h / d;

		r	= ((long long) wn) * ((long long) hn);
		r	+= getN(w, h - d * hn);
		r	+= getN(w - d * wn, d * hn);
	}

//	std::cout << "getN(" << w << "," << h << ") = " << r << "\n";

	return r;
}

int	main()
{
	int	 i, w, h;
	long long r = -1;

	cin >> w >> h;

	cin >> dn;
	for (i = 0; i < dn; i++)
		cin >> ddd[i];
	ddd[dn]	= DMAX;

	if (w % ddd[0] == 0 && h % ddd[0] == 0)
		r	= getN(w, h);

	cout << r << "\n";

	return 0;
}