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

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    long long h, w, n, hd=0, wd=0, ctr=0;
    cin>>h>>w>>n;
    vector<long long>v(n);
    long long ost;
    for(int i=0; i<n; i++)
        cin>>v[i];
    reverse(v.begin(), v.end());
    for(int i=0; i<n; i++)
    {
        ctr+=(((h/v[i])*(w/v[i]))-((hd/v[i])*(wd/v[i])));
        hd=(h/v[i])*v[i];
        wd=(w/v[i])*v[i];
    }
    if(hd==h&&wd==w)
        cout<<ctr;
    else
        cout<<-1;
}