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

long long t[35];

long long kwadraty(long long h, long long w, int n)
{
    if(w == 0)
        return 0;
    if(h == 0)
        return 0;
    long long wyn = (h / t[n]) * (w / t[n]);
    int h2 = (h / t[n]) * t[n], w2 = (w / t[n]) * t[n];
    long long add = kwadraty(h - h2, w2, n - 1) + kwadraty(h, w - w2, n - 1);
    //add = min(add, kwadraty(h - h2, w, n - 1) + kwadraty(h2, w - w2, n - 1));
    return wyn + add;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    long long h, w;
    cin >> h >> w;
    int n;
    cin >> n;
    for(int i = 1; i <= n; i++)
    {
        cin >> t[i];
    }
    if((h % t[1] != 0) || (w % t[1] != 0))
        cout << -1;
    else
    {
        cout << kwadraty(h, w, n);
    }
    return 0;
}