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

using namespace std;

long long d[50];

int main(){
    cin.tie(0);
    ios_base::sync_with_stdio(false);

    int h,w; cin >> h >> w;
    int n; cin >> n;
    for(int i = 0; i < n; i++){
        cin >> d[i];
    }

    if(h % d[0] != 0 || w % d[0] != 0){
        cout << "-1\n";
        return 0;
    }
    long long ans = 0;
    long long th = 0,tw = 0;
    int ptr1 = n-1;
    long long t1,t2,t3,t4;
    while(th != h || tw != w){
        // cout << ans << endl;
        t1 = th / d[ptr1];
        t2 = (w - tw) / d[ptr1];
        t3 = tw / d[ptr1];
        t4 = (h - th) / d[ptr1];

        // cout << t1 << " " << t2 << " " << t3 << " " << t4 << endl;
        ans += t1 * t2 + t3 * t4 + t2 * t4;
        // cout << ans << endl;
        // tw*(h-th)/(d[ptr1] * d[ptr1]) + (w-tw)*(h-th)/(d[ptr1]*d[ptr1]);
        th = h - h % d[ptr1];
        tw = w - w % d[ptr1];
        ptr1--;
    }
    cout << ans << "\n";
}