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
56
57
58
59
60
61
62
#include <iostream>
#include <vector>

using namespace std;

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

    unsigned long long h, w, n; cin >> h >> w >> n;

    vector<int> sizes;
    unsigned long long x;
    for(int i = 0; i < n; ++i) {
        cin >> x;
        sizes.push_back(x);
    }

    unsigned long long cnt = 0;
    unsigned long long rh = h;
    unsigned long long rw = w;
    
    if((h % sizes[0] != 0) || (w % sizes[0] != 0)) {
        cout << -1 << "\n";
        return 0;
    }

    // Put first picture
    bool flag = false;
    for(int i = n - 1; i >= 0; --i) {
        if(sizes[i] <= h && sizes[i] <= w) {
            cnt += (h / sizes[i]) * (w / sizes[i]);
            flag = true;
            rh = h % sizes[i];
            rw = w % sizes[i];
            break;
        }
    }
    if(!flag) {
        cout << -1 << "\n";
        return 0;
    }

    unsigned long long a;
    for(int i = n - 2; i >= 0; --i) {
        a = rh / sizes[i]; // how many times can we fit picture inside remaining heigth
        rh = rh % sizes[i];
        cnt += a * (w - rw) / sizes[i];

        a = rw / sizes[i];
        rw = rw % sizes[i];
        cnt += a * (h - rh) / sizes[i];
    }

    if(rh + rw != 0) {
        cout << "-1" << "\n";
    } else {
        cout << cnt << "\n";
    }

    return 0;
}