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

vector < int > v;

ll rek(int a, int b){
    
    if(a == 0 || b == 0){
        return 0;
    }
    int i = 0;
    while(i < v.size() && v[i] > b){
        i++;
    }
    ll sum = 0;
    if(v[i] == b){
        if(a == b){
            return 1;
        }
        sum = rek(max(a - v[i], b), min(a - v[i], b)) + 1;
    }
    else if(v[i] < b) {
        sum = rek(max(a - v[i], v[i]), min(a - v[i], v[i])) + rek(max(b - v[i], a), min(b - v[i], a)) + 1;
    }
    return sum;
}

ll rek2(int a, int b){
    int i = 0;
    while(v[i] > b){
        i++;
    }
    ll maly = rek(b, v[i]);

    if(a % v[i] == 0){
        return maly * (a / v[i]);
    }
    else {
        return maly * (a / v[i]) + rek2(max(a % v[i], b), min(a % v[i], b));
    }
}


int main(){
    std::ios_base::sync_with_stdio(0);
    cin.tie(0);
    int a, b;
    cin >> a >> b;
    int n;
    cin >> n;
    for(int i = 0; i < n; i++){
        int x;
        cin >> x;
        v.push_back(x);
    }
    sort(v.begin(), v.end(), greater < int >());
    if(a % v[v.size() - 1] != 0 || b % v[v.size() - 1] != 0){
        cout << "-1";
        return 0;
    }
    
    cout << rek2(max(a, b), min(a, b));
}