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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

long long solve(int w, int h, const vector<int>& pictures) {
    if (w == 0 || h == 0) {
        return 0;
    }
    for (auto p: pictures) {
        if (p <= w && p <= h) {
            return (long long)(w / p) * (h / p) + solve(w - (w % p), h % p, pictures) + solve(w % p, h, pictures);
        }
    }
    return 0;
}

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

    int n;
    cin >> n;
    vector<int> pictures(n);
    for (int i = 0; i < n; i++) {
        cin >> pictures[i];
    }

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

    reverse(pictures.begin(), pictures.end());
    cout << solve(w, h, pictures) << endl;
    return 0;
}