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
63
64
65
66
67
68
69
// PA2024 runda 3C -  https://sio2.mimuw.edu.pl/c/pa-2024-1/p/obr/
// 15:30-16:30
//-std=c++20
#include<iostream>
#include <cstddef>
#include <algorithm>
#include<vector>

using I = int64_t;


struct Obr {
    I h, w; // <1, 9B>
    I n; //   <1, 30>
    std::vector<I> ds; // <1, 9B>

    void run() {
        input_data();
        I result = solve(h, w, n);
        std::cout << result;
    }

    I solve(I x, I y, I ds_it) {
        if (x == 0 || y == 0) {
            return 0;
        }
        if (x < y) {
            std::swap(x, y);
        }
        //ds_it -> first bigger picture
        while (ds_it > 0 && ds[ds_it - 1] > y) {
            ds_it--;
        }
        if (ds_it == 0) {
            return -1;
        } else {
            I d = ds[ds_it - 1];
            if (x % d == 0 && y % d == 0) {
                return x / d * y / d;
            } else {
                I r1 = solve(d, y, ds_it);
                I factor = x / d;
                I r2 = solve(x % d, y, ds_it);
                if (r1 == -1 || r2 == -1) {
                    return -1;
                } else {
                    return r1 *factor+ r2;
                }
            }

        }
    }

    void input_data() {
        std::cin >> h >> w;
        std::cin >> n;
        ds.resize(n, 0);
        for (I i = 0; i < n; ++i) {
            std::cin >> ds[i];
        }
    }
};

int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);
    Obr obr;
    obr.run();
}