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

constexpr int maxn = 33;
int h, w, n, tab[maxn];
long long wyn;

long long bld(int wys, int lst){
	long long ret = 0;
	ret += wys/tab[lst];
	if (wys%tab[lst])
		ret += (tab[lst]/tab[lst-1])*bld(wys%tab[lst],lst-1);
	return ret;
}

int32_t main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	cin >> h >> w >> n;
	for (int i = 1; i <= n; ++i){
		cin >> tab[i];
	}
	if (h%tab[1] || w%tab[1])
		cout << -1, exit(0);
	for (int i = n; i > 0; --i){
		if (h > w)
			swap(h,w);
		if (tab[i] > h)
			continue;
		wyn += (w/tab[i])*bld(h,i);
		w = w%tab[i];
	}
	cout << wyn;
}