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
#include <bits/stdc++.h>
using namespace std;

#define loop(i, a, b) for(int i = a; i <= b; i++)
#define sz(x) int(x.size())
using ll = int64_t;

ll solve(ll h, ll w, vector<ll> divs) {
  if(sz(divs) == 0 || h == 0 || w == 0) return 0;
  ll div = divs.back(); divs.pop_back();
  return (h / div) * (w / div)
       + solve(h % div, w, divs)
       + solve(div * (h / div), w % div, divs);
}

int main() {
  cin.tie(0)->sync_with_stdio(false);
  ll h, w; cin >> h >> w;

  int n; cin >> n;
  vector<ll> divs(n);

  loop(i, 0, n-1) {
    cin >> divs[i];
  }
  ranges::sort(divs);

  if(h % divs[0] != 0 || w % divs[0] != 0) {
    cout << "-1\n"; return 0;
  }
  else {
    cout << solve(h, w, divs) << '\n';
  }

}