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
52
53
54
55
56
57
58
59
#pragma GCC optimize("O3,unroll-loops") 
//#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
 
//a poem about frogs
 
#include <bits/stdc++.h>
 
#define st first
#define nd second
#define eb emplace_back
#define ll long long
#define vi vector<int>
#define vvi vector<vector<int>>
#define mp make_pair
#define ii pair<int, int>
#define all(a) a.begin(), a.end()
#define fastio() ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define rep(a, b) for (int a = 0; a < b; ++a)
 
using namespace std;

int main() {
	ll w, h, n;
	cin >> w >> h >> n;
	vector<ll> v(n);
	rep(i, n) cin >> v[i];

	if (w % v[0] != 0 || h % v[0] != 0) {
		cout << "-1\n";
		return 0;
	}

	w /= v[0], h /= v[0]; //could remove this simple scaling, but naaaah
	for (int i = n-1; i >= 0; --i) v[i] /= v[0];

	/*
	 *   ################  
	 *   ################  
	 *   ################  
	 *   ######            
	 *   ######            
	 *   ######            
	 *   ######            
	 *   ######            
	 *	                   
	 */
	ll lw=w, lh=h; //left width, left height
	ll res = (lw/v[n-1])*(lh/v[n-1]);
	lw %= v[n-1], lh %= v[n-1];

	for (int i = n - 2; i >= 0; --i) {
		res += (w/v[i])*(h/v[i]) - ((w-lw)/v[i])*((h-lh)/v[i]) ; //(lw/v[i])*h + (lh/v[i])*w - 
		lw %= v[i], lh %= v[i];
	}

	cout << res << '\n';

	return 0;
}