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
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	
	ll h, w; cin >> h >> w;
	
	int n; cin >> n;
	vector<ll> dl(n);
	for(int i = 0; i < n; ++i) cin >> dl[i];
	sort(dl.begin(),dl.end());
	
	if((h % dl[0]) != 0 || (w % dl[0]) != 0){cout << -1; return 0;}
	
	ll wyn = (h / dl[0]) * (w / dl[0]); ll nh, nw;
	for(int i = 1; i < n; ++i){
		nh = (h / dl[i]) * dl[i]; nw = (w / dl[i]) * dl[i];
		wyn -= (nh / dl[i - 1]) * (nw / dl[i - 1]);
		wyn += (nh / dl[i]) * (nw / dl[i]);
		w = nw; h = nh; 
	}
	
	cout << wyn << "\n";
	
	
	return 0;
}