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

int w, h, n;
int tab[30];

long solve(int x, int y, int m)
{
    if (x == 0 || y == 0) return 0;
    int i = m - 1;
    while (tab[i] > x || tab[i] > y) i--;
    return ((long)(x / tab[i])) * ((long(y / tab[i])))
            + solve(x % tab[i], (y / tab[i]) * tab[i], i) + solve(x, y % tab[i], i);
}

int main()
{
    std::cin >> w >> h >> n;
    for (int i = 0; i < n; i++)
        std::cin >> tab[i];
    
    if (w % tab[0] != 0 || h % tab[0] != 0) std::cout << "-1";
    else std::cout << solve(w, h, n);

    return 0;
}