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
//ANMHLIJKTJIY!
#pragma GCC optimize(2)
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline","fast-math","unroll-loops","no-stack-protector")
#pragma GCC diagnostic error "-fwhole-program"
#pragma GCC diagnostic error "-fcse-skip-blocks"
#pragma GCC diagnostic error "-funsafe-loop-optimizations"
#include <bits/stdc++.h>
#define INF 1000000000
#define LINF 1000000000000000000
#define MOD 1000000007
#define mod 998244353
#define F first
#define S second
#define ll long long
#define N 100010
using namespace std;
ll n,a[N];
ll solve(ll x,ll y,ll r)
{
	if(x==0||y==0)
	{
		return 0;
	}
	ll ans=(x/a[r])*(y/a[r]);
	ans+=solve(x%a[r],(y/a[r])*a[r],r-1);
	ans+=solve(x,y%a[r],r-1);
	return ans;
}
int main(){
	ll x,y,i;
	cin>>x>>y>>n;
	for(i=0;i<n;i++)
	{
		cin>>a[i];
	}
	if(x%a[0]!=0||y%a[0]!=0)
	{
		puts("-1");
		return 0;
	}
	cout<<solve(x,y,n-1)<<'\n';
	return 0;
}