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
#include <bits/stdc++.h>
using namespace std;
#ifdef DEBUG
auto&operator<<(auto &o, pair<auto, auto> p) {o << "(" << p.first << ", " << p.second << ")"; return o;}
auto operator<<(auto &o, auto x)->decltype(x.end(), o) {o<<"{"; for(auto e : x) o<<e<<", "; return o<<"}";}
#define debug(X) cerr << "["#X"]: " << X << '\n';
#else 
#define cerr if(0)cout
#define debug(X) ;
#endif
using ll = long long;
#define all(v) (v).begin(), (v).end()
#define ssize(x) int(x.size())
#define fi first
#define se second
#define mp make_pair
#define eb emplace_back

pair<int, int> divv(int a, int b) {
	return {a/b, a%b};
}

int main() {
	ios_base::sync_with_stdio(false); cin.tie(nullptr);

	int h, w;
	cin >> h >> w;
	int n;
	cin >> n;
	vector<int> d(n);
	for(int &x : d) cin >> x;

	if(h%d[0] != 0 || w%d[0] != 0) {cout << "-1 \n"; return 0;}
	h /= d[0]; w /= d[0];

	int a = h, b = w;
	ll res = 0;
	auto fill = [&](int x) {
		auto [k, r1] = divv(a, x);
		auto [m, r2] = divv(b, x);
		res += (ll)m*(h-a)/x + (ll)k*(w-b)/x + (ll)m*k;

		a = r1; b = r2;
	};

	for(int i = n-1; i >= 0; i--) {
		d[i] /= d[0];
		fill(d[i]);
	}

	cout << res << '\n';

	return 0;
}