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

using namespace std;

vector<int> sizes;

int main(){
    int x, y;
    cin>>x>>y;
    int n;
    cin>>n;

    int a;
    for(int i = 0; i<n; i++){
        cin>>a;
        sizes.push_back(a);
    }

    if(x%sizes[0] or y%sizes[0]){
        cout<<-1;
        return 0;
    }

    long long out = 0;
    reverse(sizes.begin(), sizes.end());

    vector<long long> dhf;
    dhf.assign(n, 0);

    int cx = x, cy = y;
    for(int i = 0; i<n; i++){
        cx = x; cy = y;
        long long tmp = 0;
        for(int j = i; j<n; j++){
            int cv = sizes[j];
            tmp += (long long)(cx/cv) * (sizes[i]/cv);
            cx -= cv * (cx/cv);
        }
        dhf[i] = tmp;
    }

    cx = x; cy = y;
    int i = 0;
    for(int val : sizes){
        long long tmp = cy/val;
        out += tmp * dhf[i];
        cy -= tmp * val;
        i++;
    }

    cout<<out;
    return 0;
}