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

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using ld = long double;
using vi = vector<int>;
using vll = vector<ll>;
using vii = vector<pii>;

vi sizes;

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

  int h, w;
  cin >> h >> w;

  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    sizes.push_back(a);
  }
  sort(sizes.begin(), sizes.end());

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

  ll ans = 0;
  ll w_prev = 0, h_prev = 0;
  for (int i = n - 1; i >= 0; i--) {
    ll h_cnt = h / sizes[i];
    ll w_cnt = w / sizes[i];
    ll h_prev_cnt = h_prev / sizes[i];
    ll w_prev_cnt = w_prev / sizes[i];

    ans += (h_cnt * w_cnt) - (h_prev_cnt * w_prev_cnt);
    h_prev = h_cnt * sizes[i];
    w_prev = w_cnt * sizes[i];
  }
  cout << ans << "\n";
}