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
#include <bits/stdc++.h>
using namespace std;
vector<long long> obrazy;
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    long long h,w;
    cin>>h>>w;
    long long n;
    cin>>n;
    for(long long i=0;i<n;i++) {
        long long a;
        cin>>a;
        obrazy.push_back(a);
    }
    queue<pair<long long,long long>> que;
    long long count = 0;
    if(h%obrazy[0] != 0 or w%obrazy[0] != 0) {
        cout << -1 << "\n";
        return 0;
    }
    que.push(make_pair(h,w));
    while(!que.empty()) {
        long long bok;
        long long x = que.front().first;
        long long y = que.front().second;
        que.pop();
        long long q = x;
        x = min(x,y);
        y = max(q,y);
        long long to_top = y;
        for(long long i = n-1; i >=0; i--) {
            if(obrazy[i] <= x) {
                bok = obrazy[i];
                break;
            }
        }
        long long a = x/bok;
        long long c = y/bok;
        long long d = c*bok;
        long long left = x%bok;
        to_top -= d;
        count+=a*c;
        long long width = a*bok;
        while (to_top > 0) {
            for(long long i = n-1; i >=0; i--) {
                if(obrazy[i] <= min(width,to_top)) {
                    bok = obrazy[i];
                    break;
                }
            }
            long long a = width/bok;
            long long c = to_top/bok;
            long long d = c*bok;
            to_top -= d;
            count+=a*c;
        }
        if(left != 0 and y != 0) {
            que.push(make_pair(left, y));
        }
    }
    cout<<count<<"\n";
    return 0;
}