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
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <unordered_map>
#include <assert.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/trie_policy.hpp>
//using namespace __gnu_pbds

using namespace std;

template<class A, class B>
ostream& operator<<(ostream& o, const pair<A, B>& p) {
    return o << '(' << p.first << ", " << p.second << ')';
}
template<class A, class B, class C>
ostream& operator<<(ostream& o, const tuple<A, B, C>& t) {
    return o << '(' << get<0>(t) << ", " << get<1>(t) << ", " << get<2>(t) << ')';
}
template<class T>
auto operator<<(ostream& o, const T& x) -> decltype(x.end(), o) {
    o << '{';
    int i = 0;
    for (const auto& e : x) {
        o << (", ") + 2 * !i++ << e;
    }
    return o << '}';
}
auto operator<<(ostream& o, const string& s) -> decltype(o) {
    for (const auto& e : s) {
        o << e;
    }
    return o;
}
//#define DEBUG
#ifdef DEBUG
#define fastio()
#define debug(x...) cerr << "[" #x "]: ", [](auto... $) {((cerr << $ << "; "), ...); }(x), cerr << '\n'
#define asrt(x) if (!(x)) { cerr << "Assertion failed: " << #x << " in line " << __LINE__ << endl; exit(1); }
#else
#define fastio() ios_base::sync_with_stdio(0); cin.tie(0);
#define debug(...)
#define asrt(x)
#endif
#define pii pair<int,int>
#define inf 2'000'000'000
#define llinf 8'000'000'000'000'000'000
#define mod 998'244'353
#define mod7 1'000'000'007
#define mod9 1'000'000'009
#define eps 0.00000000001
#define all(x) x.begin(),x.end()
#define rev(x) x.rbegin(),x.rend()
#define decrease(x) x,vector<x>,greater<x>
#define maxn 30
typedef long long ll;
typedef long double ld;

int arr[maxn + 5];

ll scr(ll a, ll b, ll c, ll d, int i) {
    if (c == 0 && d == 0) return 0;
    if (i == 0) {
        printf("-1\n");
        exit(0);
    }
    ll max_right = c / arr[i];
    ll max_down = d / arr[i];
    ll added = max_right * (b / arr[i]) + max_down * (a / arr[i]) + max_right * max_down;
    // debug(a, b, c, d, i, added);
    added += scr(a + max_right * arr[i], b + max_down * arr[i], c % arr[i], d % arr[i], i - 1);
    return added;
}

void solve() {
    int a, b;
    scanf("%d%d", &a, &b);
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) scanf("%d", &arr[i]);
    printf("%lld\n", scr(0, 0, a, b, n));
}

signed main() {
    int t = 1;
    //cin >> t;
    while (t--) solve();
}