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
45
46
47
#include <iostream>
#include <numeric>
#include <vector>
#define nl '\n'

using namespace std;
using ll = long long;

ll res, w, h;
int n;
vector<ll> blocks;

int main()
{
	cin>>w>>h>>n;
	blocks.resize(n);
	for(int i=0; i<n; i++){
		cin>>blocks[i];
	}
	if(gcd(gcd(w, h), blocks[0]) != blocks[0]){
		cout<<-1<<nl;
		return 0;
	}
	vector<ll> decompx(n), decompy(n);
	for(int i=n-1; i>=0; i--){
		decompx[i] = w / blocks[i];
		w %= blocks[i];
		decompy[i] = h / blocks[i];
		h %= blocks[i];
	}
	/*for(int i=0; i<n; i++){
		cerr<<decompx[i]<<' ';
	}
	cerr<<nl;
	for(int i=0; i<n; i++){
		cerr<<decompy[i]<<' ';
	}
	cerr<<nl;*/
	for(int i=0; i<n; i++){
		for(int j=0; j<n; j++){
			ll a = max(blocks[i], blocks[j]), b = gcd(blocks[i], blocks[j]); 
			res += decompx[i] * decompy[j] * a / b;
		}
	}
	cout<<res<<nl;
	return 0;
}