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
48
49
50
51
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
vector<ll> rec;

void print(){
	for (auto u: rec)
		cout << u << " ";
	cout << endl;
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);

	int a, b, n;
	cin >> a >> b >> n;
	for (int i=0; i<n; i++){
		int d; cin >> d;
		rec.pb(d);
	}
	sort(rec.begin(),rec.end());

	//print();

	// sprawdzanie czy -1
	if (a%rec[0] || b%rec[0]){
		cout << "-1\n";
		return 0;
	}

	ll res = 0;
	for (int i=1; i<n; i++){
		int ax, bx;
		ax = a%rec[i];
		bx = b%rec[i];
		res += (b/rec[i-1])*(ax/rec[i-1]); 
		res += (a/rec[i-1])*(bx/rec[i-1]); 
		if (ax && bx){
			res -= (ax/rec[i-1])*(bx/rec[i-1]);
		}
		a -= ax;
		b -= bx;
		//cout << rec[i] << " " << res << " : " << a << " " << b << " : " << ax << " " << bx << "\n";
	}
	res += (a/rec[n-1])*(b/rec[n-1]);

	cout << res << "\n";

}