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
#include <iostream>

using namespace std;

int main() {
    int h, w;
    cin >> h >> w;

    int n;
    cin >> n;

    int curr, prev;
    long int cnt = 0;

    for (int i = 0; i < n; i++) {
        cin >> curr;
        if (i > 0) {
            while (h%curr != 0) {
                cnt += w/prev;
                h -= prev;
            }
            while (w%curr != 0) {
                cnt += h/prev;
                w -= prev;
            }
        }
        else {
            if (h%curr != 0 || w%curr != 0) {
                cout << -1 << "\n";
                return 0;
            }
        }
        prev = curr;
    }

    while (h > 0) {
        cnt += w/curr;
        h -= curr;
    }

    cout << cnt << "\n";
    return 0;
}