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

using namespace std;

int main(){
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int h, w; cin >> h >> w;
    int n; cin >> n;
    vector<int> obr(n);
    for(int &i : obr) cin >> i;
    sort(obr.begin(), obr.end());
    if(!(h%obr[0] == 0 && w%obr[0] == 0)){
        cout << -1 <<"\n";
        return 0;
    }
    long long res = 0;
    int i = n - 1;
    while(h > 0){
        while(i >= 0 && (obr[i] > h || obr[i] > w)) i--;
        long long pion = h/obr[i];
        h %= obr[i];
        long long poziom = w/obr[i];
        int zostalo = w%obr[i];
        int j = i - 1;
        while(j >= 0 && zostalo > 0){
            if(obr[j] <= zostalo){
                poziom += (obr[i]/obr[j]) * (zostalo/obr[j]);
                zostalo %= obr[j];
            }
            j--;
        }
        res += poziom*pion;
    }
    cout << res << "\n";
}