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

using namespace std;

int main() {
   long long h, w;
   cin >> h >> w;
   long long n;
   cin >> n;
   vector<long long> d(n);
   for (int i = 0; i < n; ++i) {
      cin >> d[i];
   }
   sort(d.begin(), d.end());
   if (h % d[0] != 0 || w % d[0] != 0) {
       cout << -1 << "\n";
       return 0;
   }
   long long ans = 0;
   long long last = 0;
   for (int i = n - 1; i >= 0; --i) {
       long long k = (h / d[i]) * (w / d[i]);
       ans += k - last / d[i] / d[i];
       last = k * d[i] * d[i];
   }
   cout << ans << "\n";
   return 0;
}