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

using namespace std;

typedef long long ll;
typedef long double db;
typedef pair<int,int> pii;
typedef vector<int> vi;
#define pb push_back
#define fi first
#define se second
#define rep(i,b,e) for(int i=(b); i<(e); i++)
#define each(a,x)  for(auto &a : (x))
#define all(x) (x).begin(), (x).end()
#define sz(x)  (int)(x).size()

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int X,Y,n;
    cin >> Y >> X >> n;
    vector<ll> arr(n);
    each(x,arr) cin >> x;
    if(X%arr[0] || Y%arr[0]){
        cout << -1;
        return 0;
    }
    ll koszt=0;
    stack<pii> toCut;
    toCut.push({X,Y});
    while(!toCut.empty()){
        tie(X,Y) = toCut.top();
        toCut.pop();
        if(X<=0 || Y<=0) continue;

        int maxRect=0;
        for(int i=0;i<arr.size();++i){
            if(arr[i] <= min(X,Y)) maxRect = arr[i];
            else break;
        }  
        int rectX = X/maxRect*maxRect;
        int rectY = Y/maxRect*maxRect;
        koszt += ll(rectX/maxRect) * (rectY/maxRect);
        toCut.push({rectX,Y-rectY});
        toCut.push({X-rectX,rectY});
        toCut.push({X-rectX,Y-rectY});
    }   
    cout << koszt;
    return 0;
}