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
#include<bits/stdc++.h>
using namespace std;

long long sizes[50];

long long solveVertical(long long indexOfWidth, long long height)
{
	//cerr<<"solveVertical "<<indexOfWidth<<" "<<height<<endl;
	long long originalWidth = sizes[indexOfWidth];
	long long result = 0;
	long long heightRemaining = height;
	while (heightRemaining>0)
	{
		//cerr<<"adding "<<heightRemaining/sizes[indexOfWidth]*originalWidth/sizes[indexOfWidth]<<" of "<<sizes[indexOfWidth]<<endl;
		result+=heightRemaining/sizes[indexOfWidth]*originalWidth/sizes[indexOfWidth];
		heightRemaining = heightRemaining % sizes[indexOfWidth];
		indexOfWidth++;
	}
	//cerr<<"returned "<<result<<" from vertical"<<endl;
	return result;

}

long long solveHorizontal(long long n, long long remainingH, long long remainingW, long long startingI)
{
	////cerr<<"solveHorizontal "<<remainingH<<" "<<remainingW<<" "<<startingI<<endl;
	long long result=0;
	if(remainingW==0)
		return 0;
	for(long long i=startingI;i<n;i++)
	{
		if(remainingW>=sizes[i] && remainingH>=sizes[i])
		{
			long long w = max(remainingW, remainingH);
			long long h = min(remainingH, remainingW);
			//////cerr<<"solution horizontal with "<<sizes[i]<<endl;
			auto s = solveVertical(i,h);
			//cerr<<"vertical multiplied = "<<s <<" * "<< w/sizes[i]<<" = " << s*(w/sizes[i]) << endl;
			return s * (w/sizes[i]) + solveHorizontal(n, h, w % sizes[i], i+1);
		}
	}
	return 0;
}

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(NULL);
	long long h,w,n,a;
	cin>>h>>w>>n;
	for(long long i=0;i<n;i++)
		cin>>sizes[i];
	sort(sizes,sizes+n, greater<long long>());
	if(h%sizes[n-1] + w% sizes[n-1]>0)
	{	
		cout<<-1;
		return 0;
	}
	cout<<solveHorizontal(n, min(h,w), max(h,w),0);
	return 0;
}