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
#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
#define ll long long
#define ld long double
#define ull unsigned long long
void dbg_out() { cout << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cout << ' ' << H; dbg_out(T...); }
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)

ll helper(vector<ll> &list, ll h, ll w, ll n){
  if(h == 0 || w == 0) return 0;

  ll res = 0;
  ll newH, newW;
  for(ll i = n-1; i >= 0; i--){
    if(min(w, h) - list[i] >= 0){
      res += (w/list[i]) * (h/list[i]);
      newH = h/list[i] * list[i];
      newW = w/list[i] * list[i];
      h -= h/list[i] * list[i];
      w -= w/list[i] * list[i];
      break;
    }
  }

  res += helper(list, newH, w, n);
  res += helper(list, h, newW, n);
  res += helper(list, h, w, n);
  return res;
}

void solve()
{
  ll h,w,n;
  cin >> h >> w >> n;
  vector<ll> list(n);
  for(auto &el : list) cin >> el;
  if(h%list[0] != 0 || w%list[0] != 0){
    cout << -1 << endl;
    return;
  }
  cout << helper(list, h, w, n) << endl;
} 
 
int main()
{

  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

// #ifndef ONLINE_JUDGE
//   freopen("../../in.in", "r", stdin);
//   freopen("../../out.out", "w", stdout);
// #endif
  
  solve();
  
}