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
#include <iostream>
#include <vector>
#include <algorithm>

int removeSquareFromRectangles(std::vector<int>& tab, int index, int a, int b) {
    if(a == 0 || b == 0) {
        return 0;
    }
    if(a >= tab[index] && b >= tab[index]) {
        int countOfSquares = a / tab[index] * b / tab[index];
        return countOfSquares
        + removeSquareFromRectangles(tab, index - 1, a % tab[index], b)
        + removeSquareFromRectangles(tab, index - 1, a, b % tab[index])
        - removeSquareFromRectangles(tab, index - 1, a % tab[index], b % tab[index]);
    }
    return removeSquareFromRectangles(tab, index - 1, a, b);
}

int main() {
    std::ios_base::sync_with_stdio(false);
    int a, b;
    int n;
    std::cin >> a >> b;
    std::cin >> n;
    std::vector<int> tab(n);
    for (int i = 0; i < n; i++) {
        std::cin >> tab[i];
    }

    if(a % tab[0] != 0 || b % tab[0] != 0) {
        std::cout << -1;
        return 0;
    }


    int sumOfSquares = 0;
    int index = n - 1;
    
    std::cout << removeSquareFromRectangles(tab, index, a, b);

    return 0;
}