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
//2024-03-13
//author: Marcin Rolbiecki
#include <bits/stdc++.h>
using namespace std;

#define ll long long

const int maxN = 32;
ll d [maxN];
int n;

ll fit(ll h, ll w)
{
	if (!h || !w) return 0;
	
	ll h1, w1;
	ll res;
	for (int i = 1; i <= n; i++)
		if (d[i] <= min(h, w))
		{
			h1 = (h / d[i]) * d[i]; w1 = (w / d[i]) * d[i];
			res = (h / d[i]) * (w / d[i]);
			break;
		}
		
	return res + fit(h-h1, w1) + fit(h, w-w1);
}

int main () {
		
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	
	ll h, w;
	cin >> h >> w >> n;
	for (int i = 1; i <= n; i++)
		cin >> d[i];
		
	if (h % d[1] || w % d[1])
	{
		cout << -1;
		return 0;
	}
	
	reverse(d+1, d+1+n);
	
	cout << fit(h, w);

	return 0;
}