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
60
61
#include <iostream>
#include <vector>

using namespace std;

struct TestCase {
  size_t w, h;
  vector<size_t> ds;
};

TestCase read_test_case() {
  TestCase tc;
  cin >> tc.w >> tc.h;
  size_t cnt;
  cin >> cnt;
  tc.ds.resize(cnt);
  for (auto& d : tc.ds) cin >> d;
  return tc;
}

void solve_test_case(TestCase tc);

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

  solve_test_case(read_test_case());
}

void solve_test_case(TestCase tc) {
  if (tc.w % tc.ds[0] != 0 || tc.h % tc.ds[0] != 0) {
    cout << -1;
    return;
  }

  size_t sum = 0;

  size_t w = tc.w;
  size_t h = tc.h;

  for (size_t i = 0; i + 1 < tc.ds.size(); i++) {
    size_t d = tc.ds[i];
    size_t d2 = tc.ds[i + 1];

    size_t rw = w % d2;
    size_t rh = h % d2;

    size_t up = (rh / d) * ((w - rw) / d);
    size_t right = (rw / d) * ((h - rh) / d);
    size_t corner = (rh / d) * (rw / d);

    sum += up + right + corner;
    w -= rw;
    h -= rh;
  }

  size_t d = tc.ds[tc.ds.size() - 1];
  sum += (w / d) * (h / d);

  cout << sum << "\n";
}