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
// OBR: Obrazy [C] | 2024-03-13 | Solution by dkgl
// https://youtu.be/Mu-chkx8sa4?si=oP9r6nOvR2EY8qif

#include <bits/stdc++.h>

using namespace std;

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

  long long h, w;
  cin >> h >> w;
  int n;
  cin >> n;
  vector<long long> sizes(n);
  for (auto &size: sizes) cin >> size;
  if (h % sizes[0] != 0 || w % sizes[0] != 0) {
    cout << "-1" << endl;
    return 0;
  }

  long long current_h = 0;
  long long current_w = 0;
  long long result = 0;
  for (auto size_it = sizes.rbegin(); size_it != sizes.rend(); ++size_it) {
    long long height_count = (h - current_h) / *size_it;
    result += height_count * current_w / *size_it;
    current_h += height_count * *size_it;

    long long width_count = (w - current_w) / *size_it;
    result += width_count * current_h / *size_it;
    current_w += width_count * *size_it;
  }

  cout << result << endl;
  return 0;
}