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

using namespace std;
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    long long int w,h,n,a,l=0;
    cin >> h >> w >> n;
    vector<long long int>v(n);
    for (int i = 0; i < n; i++){
        cin >> a;
        v[i] = a;
    }
    queue<pair<long long int, long long int>>q;
    queue<pair<long long int, long long int>>nq;
    q.push(make_pair(w, h));
    if (w % v[0] != 0 or h % v[0] != 0){
        cout << -1;
        return 0;
    }
    while (q.size() >= 1){
        for (long long int i = v.size() - 1; i >= 0; i--){
            if (q.size() >= 1){
                w = q.front().first;
                h = q.front().second;
                if (v[i] <= w and v[i] <= h){
                    long long int x = w % v[i],y = h % v[i];
                    l += (w / v[i]) * (h / v[i]);
                    q.pop();
                    if(x != 0)nq.push(make_pair(x, h));
                    if(y != 0)nq.push(make_pair(y, w - x));
                    i = v.size();
                }
            }
            else break;
        }
        while (nq.size() >= 1){
            q.push(nq.front());
            nq.pop();
        }  
    }
    cout << l;
}