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
#include<iostream>
#include<vector>
#include<algorithm>
#include<map>
using namespace std;
typedef unsigned long long ll;
map< pair<int,int>,ll>mm;
vector<ll>v;
ll f(ll a,ll b){
	if(a>b)swap(a,b);
	if(a==0)return 0;
	if(mm.find({a,b})!=mm.end())return mm[{a,b}];
	vector<ll>::iterator it;
	it=upper_bound(v.begin(),v.end(),a);
	if(it==v.begin())return 1000000001;
	it--;
	mm[{a,b}]=min(f(a,b%(*it))+f(a%(*it),b-b%(*it))+(a/(*it))*(b/(*it)),f(a%(*it),b)+f(a-a%(*it),b%(*it))+(a/(*it))*(b/(*it)));
	return mm[{a,b}];
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
	ll n,m,k,v1,wyn;
	cin>>n>>m>>k;
	while(k--){
		cin>>v1;
		v.push_back(v1);
	}
	if(n%v[0]||m%v[0]){
		cout<<-1;
		return 0;
	}
	v.push_back(1000000001);
	wyn=f(n,m);
	cout<<wyn;
	return 0;
}