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

#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 2137
#endif

const int N = 30;
int h, w, n;
int d[N];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> h >> w >> n;
  for (int i = 0; i < n; i++) {
    cin >> d[i];
  }
  if (h % d[0] != 0 || w % d[0] != 0) {
    cout << "-1\n";
    return 0;
  }
  int x = h / d[0];
  int y = w / d[0];
  ll ans = 0;
  for (int i = 1; i < n; i++) {
    int il = d[i] / d[i - 1];
    if (il > min(x, y)) {
      break;
    }
    int rx = x % il;
    int ry = y % il;
    ans += 1ll * rx * y + 1ll * ry * x - 1ll * rx * ry;
    x /= il;
    y /= il;
  }
  ans += 1ll * x * y;
  cout << ans << '\n';
}