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
#include <bits/stdc++.h>
using namespace std;
long long a,b,n,tab[35];
int main(){
    cin>>a>>b;
    long long A=a,B=b;
    if(a<b)
        swap(a,b);
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>tab[i];
    }
    if(a%tab[0]!=0 || b%tab[0]!=0){
        cout<<-1;
        return 0;
    }
    long long res=0;
    a=0,b=0;
    for(int i=n-1;i>=0;i--){
        long long tb=b/tab[i],ta=a/tab[i],tA=A/tab[i],tB=B/tab[i];
        res+=(tA)*(tB)-(ta)*(tb);
        a+=(tA-ta)*tab[i];
        b+=(tB-tb)*tab[i];
        //cout<<res<<" "<<a<<" "<<b<<" "<<ta<<" "<<tb<<" "<<tA<<" "<<tB<<'\n';
    }
    cout<<res;

}