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

const int mx = 31;
long long lengths[mx];
long long h, w, ans, n;

int main(){
    ios::sync_with_stdio(false);
    cin >> h >> w;
    cin >> n;
    for(int i = 0; i < n; ++i){
        cin >> lengths[i];
    }

    bool is_height_divisible = (h % lengths[0]) == 0;
    bool is_width_divisible = (w % lengths[0]) == 0;

    if(is_height_divisible && is_width_divisible){
        ans = (h / lengths[0]) * (w / lengths[0]);

        for(int i = 1; i < n; ++i){
            long long number_of_squares = (h / lengths[i]) * (w / lengths[i]);
            long long saved_per_square = (lengths[i] / lengths[i-1]) * (lengths[i] / lengths[i-1]) - 1;
            long long saved = number_of_squares * saved_per_square;
            ans -= saved;
        }

        cout << ans;
    }
    else{
        cout << -1;
    }

    return 0;
}