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
#include <bits/stdc++.h>
using namespace std;
int main() {
	int H, W;
	cin >> H >> W;
	int n;
	cin >> n;
	vector<int> d(n);
	for (int i = 0; i < n; i++) {
		cin >> d[i];
	}
	int h = 0, w = 0;
	long long answer = 0;
	for (int i = n - 1; i >= 0; i--) {
		int A = (H - h) / d[i];
		int B = (W - w) / d[i];
		answer -= (long long) h / d[i] * w / d[i];
		h += A * d[i];
		w += B * d[i];
		answer += (long long) h / d[i] * w / d[i];
	}
	if (h == H && w == W) {
		cout << answer << endl;
	}
	else {
		cout << "-1" << endl;
	}
}