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
#include<iostream>
#include<vector>

using namespace std;

bool da_sie;
long long policz(long long x, long long y, vector<long long> &obr, long long i)
{
    long long w = 0;
    if(x == 0 || y == 0)
    {
        return 0;
    }
    while(obr[i] > x || obr[i] > y)
    {
        if(i == 0)
        {
            da_sie = false;
            return -1;
        }
        i--;
    }
    w = x/obr[i];
    w *= y/obr[i];
    w += policz(x - obr[i] * (x/obr[i]), obr[i] * (y/obr[i]), obr, i);
    w += policz(x, y - obr[i] * (y/obr[i]), obr, i);
    return w;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    long long x;
    long long y;
    da_sie = true;
    cin >> x;
    cin >> y;
    long long n;
    cin >> n;

    vector<long long> obr(n);
    for(long long i = 0; i < n; i++)
    {
        cin >> obr[i];
    }
    long long w = policz(x, y, obr, n - 1);
    if(da_sie)
    {
        cout<<w;
    }
    else
    {
        cout<<"-1";
    }
    return 0;
}