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
#include <iostream>
#include <cmath>

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

    int n;
    std::cin >> n;
    int sizes[30];

    for(int i = 0; i < n; ++i) {
        std::cin >> sizes[i];
    }

    int i = 0;
    long long width = w / sizes[i];
    long long height = h / sizes[i]; 

    long long count = width * height;

    ++i;
    while(i < n) {
        width = width / (sizes[i] / sizes[i - 1]);
        height = height / (sizes[i] / sizes[i - 1]);
        if(width > 0 && height > 0) {
            count = count - width * height * (sizes[i] / sizes[i - 1]) * (sizes[i] / sizes[i - 1]) + width * height;
            ++i;
        }
        else {
            break;
        }
    }

    if(h % sizes[0] == 0 && w % sizes[0] == 0) {
        std::cout << count;
    }
    else {
        std::cout << -1;
    }

    return 0;
}