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
#include <bits/stdc++.h>
using namespace std;
long long tab[35];
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    long long N,M,k;
    cin>>N>>M>>k;

    long long x,y,s,a,b,C1,C2;

    for(int i=k;i>0;i--){cin>>tab[i];}

    C1=tab[1]*(M/tab[1]);
    C2=tab[1]*(N/tab[1]);
    y=C1;
    x=C2;
    s=(M/tab[1])*(N/tab[1]);
    //cout<<tab[1]<<": "<<s<<endl;

    for(int i=2;i<=k;i++){
        a=(M-y)/tab[i];
        b=(N-x)/tab[i];

        s+=a*(x/tab[i]);
        s+=b*(y/tab[i]);
        s+=a*b;

        //cout<<tab[i]<<": "<<a*(x/tab[i])+b*(y/tab[i])+a*b<<endl;
        x+=b*tab[i];
        y+=a*tab[i];

    }
    if(x==N&&y==M){cout<<s;}else cout<<-1;

return 0;}