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
# include <bits/stdc++.h>
# define For(i, l, r) for(int i = (l); i <= (r); i++)
# define Rep(i, n) For(i, 0, (n) - 1)
# define size(x) (ll)x.size()
# define MAXSZ 300005
# define all(x) x.begin(),x.end()
using namespace std;
typedef long long ll;
typedef long double ld;
const ll inf = 1e9 + 7;
const ll mod = 1e9 + 7;
ll n , m , k , q , t , h , w;
vector<ll>vec(30);
ll rec(ll ind , ll cur_h , ll cur_w) {
      if (cur_h == 0 || cur_w == 0) return 0;
      if (ind == n) return -1;
      ll kol_op = 0;
      For (i , ind , n - 1) {
            ll kol1 = cur_h / vec[i] , kol2 = cur_w / vec[i];
            if (kol1 * kol2 == 0) continue;
            ll new_h = cur_h - kol1 * vec[i];
            ll new_w = cur_w - kol2 * vec[i];
            kol_op += kol1 * kol2;
            ll to1 = rec(i + 1 , new_h , kol2 * vec[i]);
            ll to2 = rec(i + 1 , kol1 * vec[i] , new_w);
            ll to3 = rec(i + 1 , new_h , new_w);
            if (to1 == -1 || to2 == -1 || to3 == -1) return -1;
            kol_op += to1 + to2 + to3;
            return kol_op;
      }
      return -1;
}
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> h >> w;
    cin >> n;
    Rep (i , n) {
        cin >> vec[i];
    }
    sort(vec.rbegin() , vec.rend());
    ll ans = rec(0 , h , w);

    cout << ans;
}
//odjgoadfhfoav hash1 i hash2 (hash1 * 1e9 + hash