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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <iostream>
#include <vector>
#include <map>

using namespace std;

struct Rectange
{
	int w, h;

};

long long resolve(long long c, map<long long, long long > & dp, const vector<long long>& d, int i)
{
	long long w = c % 2000000000LL, h = c / 2000000000LL;

	if (w > h)
	{
		long long temp = h;
		h = w;
		w = temp;

		c = h * 2000000000 + w;
	}

	if (dp.find(c) != dp.end())
	{
		return dp[c];
	}



	long long w1 = w / d[i], w2 = w % d[i], h1 = h / d[i], h2 = h % d[i];

	long long result = w1 * h1;

	if (h2 > 0)
	{
		long long ncase = h2 * 2000000000 + w;
		result += resolve(ncase, dp, d, i - 1);
	}

	if (w2 > 0 && h1 > 0)
	{
		long long ncase = h1 * d[i] * 2000000000 + w2;
		result += resolve(ncase, dp, d, i - 1);
	}


	dp[c] = result;
	return result;
}

int main()
{
	long long h, w, n;
	
	cin >> h >> w >> n;
	vector<long long> d;
	d.reserve(n);

	for (int i = 0; i < n; i++)
	{
		long long di;
		cin >> di;
		d.push_back(di);
	}

	if (w % d[0] != 0 || h % d[0] != 0)
	{
		cout << -1;
		
	}
	else
	{
		map<long long, long long> dp;
		cout << resolve(w * 2000000000 + h, dp, d, d.size() - 1);
	}

	return 0;
}