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
#include <bits/stdc++.h>
using namespace std;
long long x, y, n, res, idx;
long long tab[40];
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> x >> y >> n;
    for (int i = 0; i < n; ++i)
    {
        cin >> tab[i];
    }
    sort(tab, tab + n);
    tab[n] = 2e9;
    idx = n - 1;
    while (x != 0 && y != 0)
    {
        long long block = 0, nx, ny, cx, cy;
        for (int i = idx; i >= 0; --i)
        {
            if (tab[i] <= x)
            {
                block = tab[i];
                idx = i;
                break;
            }
        }
        if (block == 0)
        {
            cout << -1;
            return 0;
        }
        cx = nx = x % block;
        cy = ny = y % block;
        res += (x/block) * (y/block);
        for (int i = idx - 1; i >= 0; --i)
        {
            res += (x - nx) / tab[i] * (cy/tab[i]);
            cy %= tab[i];
            res += (y - ny) / tab[i] * (cx/tab[i]);
            cx %= tab[i];
        }
        if (cx != 0 || cy != 0)
        {
            cout << -1;
            return 0;
        }
        x = nx;
        y = ny;
    }
    cout << res;
}