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

using ll = long long;

int main() {
    ll h, w;
    std::cin >> h >> w;
    int n;
    std::cin >> n;
    std::vector<ll> d(n);
    for(int i = 0; i < n; i++) {
        std::cin >> d[i];
    }
    if(h%d[0] != 0 or w % d[0] != 0) {
        std::cout << -1 << "\n";
        return 0;
    }
    ll total = 0;
    for(int i = 0; i < n - 1; i++) {
        ll h_new = h - h % d[i + 1];
        ll w_new = w - w % d[i + 1];
        ll cutoff_area = h * w - h_new * w_new;
        total += cutoff_area / (d[i] * d[i]);
        h = h_new;
        w = w_new;
    }
    total += (h * w) / (d[n-1] * d[n-1]);
    std::cout << total << "\n";
    return 0;
}