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
#include<stdio.h>
#define int long long
inline char nc()
{
	static char buf[99999],*l,*r;
	return l==r&&(r=(l=buf)+fread(buf,1,99999,stdin),l==r)?EOF:*l++;
}
inline void read(int&x)
{
	char c=nc();for(;c<'0'||'9'<c;c=nc());
	for(x=0;'0'<=c&&c<='9';x=(x<<3)+(x<<1)+(c^48),c=nc());
}
int n,m,p,a[30],ans;
main()
{
	read(n);read(m);read(p);for(int i=0;i<p;read(a[i++]));
	if(n%a[0]||m%a[0]){printf("-1");return 0;}
	for(int i=p-1;i>=0;--i)if(n>=a[i])if(m>=a[i])
	{
		ans+=(n/a[i])*(m/a[i]);
		int x=n/a[i]*a[i],y=m%a[i];
		for(int j=i-1;j>=0;--j)
		{
			ans+=(x/a[j])*(y/a[j]);
			y%=a[j];
		}
		n-=x;
	}
	printf("%lld",ans);
}