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
/**
 * Patryk Kisielewski
 * 
 * Potyczki Algorytmiczne 2024
 * Zadanie: OBR - Obrazy [C]
*/
#include <vector>
#include <utility>
#include <cstdio>
#include <iostream>
#include <sstream>

using namespace std;

long long h, w;
int n;
vector<long long> d;

long long calc(long long h, long long w, int index) {
    if (h == 0 || w == 0) return 0;
    long long di = d[index];
    long long i_h = h / di;
    long long i_w = w / di;
    if (i_h > 0 && i_w > 0) {
        long long h2 = i_h * di;
        long long w2 = i_w * di;
        return i_h * i_w + calc(h - h2, w2, index - 1) + calc(h, w - w2, index - 1);
    }
    return calc(h, w, index - 1);
}

int main() {

    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> h >> w >> n;

    d = vector<long long>(n);

    for (auto& di : d) {
        cin >> di;
    }

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

    cout << calc(h, w, n-1) << endl;

    return 0;
}