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
41
42
43
44
45
46
47
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

vector<int>d;

int main() {
    ios_base::sync_with_stdio(0);
    int h, w; cin >> h >> w;

    int n; cin >> n;
    d.resize(n);
    for(int &x : d)
        cin >> x;

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

    long long answer = 0;

    while(w > 0) {
        int ind = upper_bound(d.begin(), d.end(), min(h, w)) - d.begin() - 1;

        long long local_answer = 0;

        int start = d[ind];

        int my_h = h;
        while(my_h > 0) {
            int layers = my_h / d[ind];
            local_answer += 1ll * (start / d[ind]) * layers;
            my_h %= d[ind];
            ind--;
        }

        answer += (w / start) * local_answer;
        w %= start;
    }

    cout << answer << "\n";

    return 0;
}