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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <map>
#include <vector>
#include <utility>
#include <algorithm>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

constexpr int S = 33;

int wy, sz, n, min_i = -1;
vector<int> tab;
map<pii, ll> dp;

int najwiekszy(int h)
{
    auto it = upper_bound(tab.begin(), tab.end(), h);
    if(it == tab.begin())
        return -1;
    else return *(--it);
}

ll cache(int h, int w)
{
    ll naj, ileh, ilew, p1, p2;
    if(h > w) swap(h, w);
    if(h == 0)
        return 0;
    if(dp.find({h, w}) != dp.end())
        return dp[{h, w}];
    naj = najwiekszy(h);
    ileh = h / naj;
    ilew = w / naj;
    p1 = cache(h, w - ilew * naj);
    p2 = cache(h - ileh * naj, ilew * naj);
    dp[{h, w}] = ileh * ilew + p1 + p2;
    return ileh * ilew + p1 + p2;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> wy >> sz >> n;
    tab.resize(n);
    for(int i = 0; i < n; i++)
    {
        cin >> tab[i];
        dp[{tab[i], tab[i]}] = 1;
    }
    sort(tab.begin(), tab.end());
    if(wy % tab[0] != 0 || sz % tab[0] != 0)
    {
        cout << -1;
        return 0;
    }
    cout << cache(wy, sz);
    return 0;
}