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

using namespace std;

#define st first
#define nd second
typedef long long ll;
typedef long double ld;
typedef __int128 int128;
typedef vector<int> vi;
typedef pair<int,int> pi;
typedef pair<double,double> pd;
typedef pair<ll,ll> pl;

const int max_n = 37;
ll arr[max_n];

int main(){

    ios::sync_with_stdio(0);
    cin.tie(0);

    ll h,w;
    cin >> h >> w;

    int n;
    cin >> n;
    for(int i = 1; i <= n; i++) cin >> arr[i];

    if(h%arr[1] != 0 || w%arr[1] != 0)
        cout << -1;
    
    else{
        ll r_h = h;
        ll r_w = w;

        ll ans = 0;
        for(int i = n; i >= 1; i--){
            ll ile_w = r_w/arr[i];
            ll ile_h = r_h/arr[i];

            r_w = r_w%arr[i];
            r_h = r_h%arr[i]; 

            /* cout << "i: " << i << "\n";
            cout << "ile_w: " << ile_w << '\n';
            cout << "ile_h: " << ile_h << '\n';
            cout << "r_w: " << r_w << " r_h: " << r_h << "\n"; */

            ans += ile_w*((h-r_h)/arr[i]);
            ans += ile_h*((w-r_w)/arr[i]);

            ans -= ile_w*ile_h;
        }

        cout << ans;
    }

    return 0;
}

//g++ -O3 -static -Wall .cpp -std=c++17