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
#include<iostream>
#include<vector>
std::vector<int> w;
long long res = 0;
long long a, b, n, x;
void prostokat(long long a, long long b, long long ind) {
	long long min = std::min(a, b);
	if (min == 0) {
		return;
	}
	while (w[ind] > min and ind < n - 1) {
		ind++;
	}
	res += (a / w[ind]) * (b / w[ind]);
	prostokat(b % w[ind], w[ind] * int(a / w[ind]), ind);
	prostokat(a % w[ind], b, ind);
}

int main() {
	std::ios_base::sync_with_stdio(0);
	std::cin.tie(0);
	std::cin >> a >> b >> n;
	w.resize(n);
	x = 1e9 / 2;
	for (int i = 0; i < n; i++) {
		std::cin >> x;
		w[n - 1 - i] = x;
	}
	if (b > a) std::swap(a, b);
	if (a % w[w.size() - 1] != 0 or b % w[w.size() - 1] != 0) {
		std::cout << -1;
		exit(0);
	}
	prostokat(a, b, 0);
	std::cout << res;

	return 0;

}