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
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ull = unsigned long long;
using db = long double;

using pi = pair<int, int>;
using vi = vector<int>;
using vl = vector<ll>;
using vpi = vector<pi>;

#define mp make_pair
#define pb push_back
#define eb emplace_back
#define x first
#define y second

#define sz(x) int((x).size())
#define all(x) (x).begin(), (x).end()
#define rep(i,a,b) for(int i = (a); i < (b); i++)
#define per(i,a,b) for(int i = (b) - 1; i >= (a); i--)

#ifdef LOCAL
template<class A, class B> auto& operator<<(auto &o, pair<A, B> p) { return o << '(' << p.x << ", " << p.y << ')'; }
template<class... A> auto& operator<<(auto &o, tuple<A...> t) {
    bool is_first = true;
    o << "(";
    apply([&o, &is_first](auto... a) {((o << (is_first ? (is_first = false, "") : ", ") << a), ...);}, t);
    o << ")";
    return o;
}
auto& operator<<(auto& o, auto a) {
    bool is_first = true;
    o << "{";
    for (auto b : a) o << (is_first ? (is_first = false, "") : ", ") << b;
    return o << "}";
}
void dump(auto... x) {
    bool is_first = true;
    ((cerr << (is_first ? (is_first = false, "") : ", ") << x), ...) << "\n";
}
#define debug(x...) cerr << "[" #x "]: ", dump(x)
#else
#define debug(...) ;
#endif

template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int h, w;
    cin >> h >> w;

    int n;
    cin >> n;
    vi d(n);
    rep(i, 0, n) cin >> d[i];

    if (__gcd(h, w) % d[0] != 0) {
        cout << "-1\n";
        return 0;
    }

    ll res = 0;
    int cur_h = 0, cur_w = 0;
    per(i, 0, n) {
        ll a = (h - cur_h) / d[i], b = (w - cur_w) / d[i];
        res += a * (cur_w / d[i]);
        res += b * (cur_h / d[i]);
        res += a * b;
        cur_h += a * d[i];
        cur_w += b * d[i];
    }
    cout << res << "\n";
    
    return 0;
}