1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// 2024 HOPE IN VALUABLE
#include<bits/stdc++.h>
using namespace std;
int h,w,n,a[31]; long long ans;
int main(){
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin>>h>>w>>n;
	for(int i=1;i<=n;i++) cin>>a[i];
	if(h%a[1]||w%a[1]) return cout<<"-1\n",0;
	if(h>w) swap(h,w);
	int p=n;
	while(h){
		while(h<a[p]) p--;
		int row=h/a[p]*a[p],tmp=w,q=p;
		ans+=1ll*(row/a[p])*(tmp/a[p]); tmp%=a[p];
		while(tmp){
			while(tmp<a[q]) q--;
			ans+=1ll*(row/a[q])*(tmp/a[q]); tmp%=a[q];
		}
		h%=a[p];
	}
	cout<<ans<<'\n';
	return 0;
}