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

int main(){
    long long w, h, zh;
    cin>>w>>h;

    int n;
    cin>>n;

    vector<int> d(n);

    for(int i = 0; i < n; i++){cin>>d[n-i-1];}

    long long l = 0, c, p;
    long long o = 0;
    long long fo = 0;

    while(w and h){
        if(w > h){swap(w, h);}
        c = w;
        l = 0;
        o = 0;

        while(c and l < n){
            if(c < d[l]){
                l++;
            }
            else{
                if(c==w){
                    zh=d[l];
                }
                o += (zh/d[l])*(floor(c/d[l]));
                // cout<<"#1 "<<(d[p]/d[l])<<" * "<<(floor(c/d[l]))<<" = "<<(d[p]/d[l])*(floor(c/d[l]))<<"\n";
                // c-=d[l];
                c%=d[l];
            }
            // cout<<c<<" ";
        }

        if(c){
            fo = -1;
            w = 0;
        }
        else{
            // cout<<"#2 "<<h/zh<<"\n";
            o*=floor(h/zh);
            fo+=o;
            h%=zh;
        }
    }

    cout<<fo<<"\n";
}