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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, a, b) for(int i = a; i <= b; i++)
#define FORD(i, a, b) for(int i = a; i >= b; i--)
#define REP(i, n) for(int i = 0; i < n; i++)

#define VI vector<int>
#define PII pair<int,int>
#define SZ(x) (int)((x).size())
#define PB push_back
#define MP make_pair
#define st first
#define nd second

template<class C> void mini(C& _a4, C _b4) { _a4 = min(_a4, _b4); }
template<class C> void maxi(C& _a4, C _b4) { _a4 = max(_a4, _b4); }

template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<'='<<h<<endl; }
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while(*sdbg!=',')cerr<<*sdbg++;cerr<<'='<<h<<','; _dbg(sdbg+1, a...);
}

template<class T> ostream& operator<<(ostream& os, vector<T> V) {
  os << "["; for (auto& vv : V) os << vv << ","; os << "]";
  return os;
}

#ifdef LOCAL
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define debug(...) (__VA_ARGS__)
#define cerr if(0)cout
#endif

template<class C> ostream & operator<<(ostream &os, const set<C> &s){
    os << "{";
    for(auto a: s){
        os << a << ", ";
    }
    os << "}";
    return os;
}

int solve(int h, int w, const VI & d, int d_idx){
    if(h == 0 || w == 0){
        return 0;
    }
    if(h > w){
        swap(h, w);
    }
    while(d[d_idx] > h){
        d_idx--;
    }
    int ans = 0;
    int remaining_h = h;
    int biggest_d = d[d_idx];
    FORD(i, d_idx, 0){
        int cur_d = d[i];
        int cnt = remaining_h / cur_d;
        int width = biggest_d / cur_d;
        ans += width * cnt;
        remaining_h -= cnt * cur_d;
    }
    int fit_w = w / biggest_d;
    ans *= fit_w;
    w -= fit_w * biggest_d;
    ans += solve(h, w, d, d_idx - 1);
    return ans;
}


int32_t main(){
    ios_base::sync_with_stdio(0);
    int h,w;
    cin >> h >> w;
    int n;
    cin >> n;
    VI d(n);
    REP(i, n){
        cin >> d[i];
    }
    sort(ALL(d));
    if(h % d[0] != 0 || w % d[0] != 0){
        cout << -1;
        return 0;
    }
    cout << solve(h, w, d, n - 1) << "\n";

}