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
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int h, w, n;
    cin >> h >> w >> n;
    vector<int> sizes;
    sizes.resize(n);
    for (int i = 0; i < n; ++i) {
        cin >> sizes[i];
    }
    if (w % sizes[0] || h % sizes[0]) {
        cout << -1 << endl;
        return 0;
    }
    long long wRemining = w, hRemining = h, sum{};
    for (int i = n - 1; i >= 0; --i) {
        auto wRound = lldiv(wRemining, sizes[i]);
        auto hRound = lldiv(hRemining, sizes[i]);
        sum += wRound.quot * (h - hRemining) / sizes[i] + hRound.quot * (w - wRemining) / sizes[i] + wRound.quot * hRound.quot;
        wRemining = wRound.rem;
        hRemining = hRound.rem;
    }
    cout << sum << endl;
}