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

ull a, b, n;
vector<ull> tab;


int main() {
    ios_base::sync_with_stdio(0);
    
    cin>>a>>b>>n;

    for(int i = 0, v; i < n; ++i){
        cin>>v;
        tab.push_back(v);
    }

    ull a_done = 0;
    ull b_done = 0;
    ull result = 0;
    for(int16_t i = n - 1; i >= 0; --i){
        ull v = tab[i];
        ull ma = (a - a_done);
        ma = ma - ma%v;
        ull mb = (b - b_done);
        mb = mb - mb%v;


        result += ((a_done + ma) * (b_done + mb) - (a_done * b_done)) / (v*v);
        a_done += ma;
        b_done += mb;

    }

    if(a_done < a || b_done < b){
        cout<<-1<<endl;
        return 0;
    }

    cout<<result<<endl;

    return 0;
}