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 <bits/stdc++.h>

using namespace std;

long long height;
long long width;
long long amountOfPaintings;
long long paintings[30];

long long countPaintings(long long height, long long width)
{
    for (int i = amountOfPaintings - 1; i >= 0; --i)
    {
        long long amount = (height / paintings[i]) * (width / paintings[i]);
        if (amount > 0)
        {
            return amount + countPaintings(height, width % paintings[i]) + countPaintings(height % paintings[i], width -  (width % paintings[i]));
        }
    }
    return 0;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    cin >> height >> width;
    cin >> amountOfPaintings;
    
    for (int i = 0; i < amountOfPaintings; ++i)
    {
        cin >> paintings[i];
    }
    sort(paintings, paintings + amountOfPaintings);
    if (height % paintings[0] != 0 || width % paintings[0] != 0)
    {
        cout << "-1\n";
        return 0;
    }
    long long minimalAmount = countPaintings(height, width);
    cout << minimalAmount << '\n';
}