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

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

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

  vector<int> t(n);
  for(int& i : t)
    cin >> i;

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

  lld result = 0;
  int d = 1;
  for(int i : t) {
    i /= d;

    result += (lld) (w%i) * h;
    w -= w%i;
    result += (lld) w * (h%i);
    h -= h%i;

    d *= i;
    w /= i;
    h /= i;
  }

  result += (lld) w * h;
  cout << result << "\n";

  return 0;
}