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 <algorithm>
#include <bits/stdc++.h>
#include <iterator>

using namespace std;

int n;

int calc_res(vector<int> v, int w, int h) {
  if (v.empty())
    return 0;
  int l = v.back();
  int res = (w / l) * (h / l);
  v.pop_back();
  return res + calc_res(v, (w % l), h - (h % l)) + calc_res(v, w, (h % l));
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  int w, h;
  cin >> w >> h;
  cin >> n;
  vector<int> v(n);
  for (int &i : v)
    cin >> i;

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

  cout << calc_res(v, w, h) << '\n';
}