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
48
49
50
51
52
53
54
55
#include <iostream>
#include <vector>

using namespace std;

void input(int & h, int & w, int & n, vector<int> & paintings) {
    cin >> h >> w >> n;
    paintings.resize(n, 0);
    for(int i = 0; i < n; i++) {
        cin >> paintings[i];
    }
}

long long get_result(int const h, int const w, vector<int> const & paintings) {
    if(h % paintings[0] || w % paintings[0]) {
        return -1;
    } 

    long long result = 0;
    int filled_h = 0;
    int filled_w = 0;

    for(int i = paintings.size() - 1; i >= 0; i--) {
        long long nr_sgl_right = ((w - filled_w) / paintings[i]);
        long long nr_sgl_down = ((h - filled_h) / paintings[i]);
        long long nr_fill_col = nr_sgl_right * (filled_h / paintings[i]);
        long long nr_fill_row = nr_sgl_down * (filled_w / paintings[i]);
        long long nr_fill_ver = nr_sgl_right * nr_sgl_down;
        
        result += nr_fill_col + nr_fill_row + nr_fill_ver;
        if(((long long)filled_h * filled_w > 0) || (h >= paintings[i] && w >= paintings[i])) {
            filled_w += nr_sgl_right * paintings[i];
            filled_h += nr_sgl_down * paintings[i];
        }
    }

    return result;
}

void solve() {
    int h, w, n;
    vector<int> paintings;
    input(h, w, n, paintings);

    cout << get_result(h, w, paintings) << "\n";
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    solve();

    return 0;
}