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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <bits/stdc++.h>
#define int long long
using namespace std;

vector<int>obr;
int s=0;

void pro(int x, int y){
    if(x==0 || y==0) return; 

    int biggest=0;
    for(int i=0;i<obr.size();i++)
    {
        if(obr[i]<=min(x, y)){
            biggest=obr[i];
            break;
        }
    }

    int hmx=x/biggest;
    int hmy=y/biggest;
    s+=hmy*hmx;

    int x1,y1,x2,y2,x3,y3;
    x1=x-(hmx*biggest);
    y1=hmy*biggest;

    x2=hmx*biggest;
    y2=y-(hmy*biggest);

    x3=x1;
    y3=y2;

    pro(x1,y1);
    pro(x2,y2);
    pro(x3,y3);
}

int32_t main(){
    cin.tie(0)->sync_with_stdio(0);

    int x,y;
    cin>>x>>y;
    int z;
    cin>>z;
    for(int i=0;i<z;i++)
    {
        int t;
        cin>>t;
        if(t<=min(x,y)) obr.push_back(t); 
    }
    sort(obr.begin(),obr.end(),greater());

    if(obr.size()==0){
        cout<<-1; return 0;
    }

    int mini=obr[obr.size()-1];
    if(x%mini!=0 || y%mini!=0){
        cout<<-1; return 0;
    }


   

    pro(x,y);
    cout<<s;
    
    
    

}