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
#include <vector>
#include <iostream>

int main() {
	int h, w;
	std::cin >> h >> w;
	int n;
	std::cin >> n;
	std::vector<int> paintings;
	for (int i = 0; i < n; i++) {
		int tmp;
		std::cin >> tmp;
		paintings.push_back(tmp);
	}
	
	if (h % paintings.front() != 0 || w % paintings.front() != 0) {
		std::cout << -1 << "\n";
		return 0;
	}
		
	int prevH = h;
	int prevW = w;
	
	int prevPaintingSize = paintings.front();
	long long count = static_cast<long long>((prevH / prevPaintingSize)) * (prevW / prevPaintingSize);
	
	for (int i = 1; i < paintings.size(); i++) {
		int currentPaintingSize = paintings[i];
		int currentH = (prevH / currentPaintingSize) * currentPaintingSize;
		int currentW = (prevW / currentPaintingSize) * currentPaintingSize;
		if (currentH == 0 || currentW == 0) {
			break;
		}
		long long toDeduct = static_cast<long long>((currentH / prevPaintingSize)) * (currentW / prevPaintingSize);
		long long toAdd = static_cast<long long>((currentH / currentPaintingSize)) * (currentW / currentPaintingSize);
		count -= toDeduct;
		count += toAdd;
		prevH = currentH;
		prevW = currentW;
		prevPaintingSize = currentPaintingSize;
	}
	std::cout << count << "\n";
	return 0;
}