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
#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

vector<long long>inp;

int main(){
  long long x,y,n,a,ax=0,ay=0,sx=0,sy=0, suma=0;
  cin>>x>>y>>n;
  for(int i=0;i<n;i++){
    cin>>a;
    inp.push_back(a);
  }
  sort(inp.begin(), inp.end());

  for(int i=n-1;i>=0;i--){
    ax+=((x-sx)/inp[i])*inp[i];
    ay+=((y-sy)/inp[i])*inp[i];
    suma+=((ax*ay-sx*sy)/(inp[i]*inp[i]));
    sx=ax, sy=ay;
   // cout<<ax<<" "<<ay<<" "<<suma<<"\n";
  }
  if(ax==x&&ay==y)cout<<suma;
  else cout<<-1;
}