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

using ll = int64_t;

int main(){
	ios_base::sync_with_stdio(false), cin.tie(nullptr);
	int H, W;
	cin >> H >> W;
	int N;
	cin >> N;
	vector<ll> D(N);
	for(ll& x : D) cin >> x;
	if((H % D[0] != 0) || (W % D[0] != 0)){
		cout << -1 << '\n';
		exit(0);
	}
	vector<ll> hd(N), wd(N);
	for(int i = N-1; i >= 0; i--){
		hd[i] += H / D[i];
		H %= D[i];
		wd[i] += W / D[i];
		W %= D[i];
	}
	ll ans = 0;
	for(int a = 0; a < N; a++){
		for(int b = 0; b < N; b++){
			ans += hd[a] * wd[b] * (D[a] / D[min(a,b)]) * (D[b] / D[min(a,b)]);
		}
	}
	cout << ans << '\n';
}