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

using namespace std;

int main()
{
    int h,w,n,ol;
    scanf("%d %d", &h, &w);
    scanf("%d", &n);
    vector<int> o;
    scanf("%d", &ol);
    if(h % ol != 0 || w % ol != 0){
        printf("-1");
        return 0;
    }
    o.push_back(ol);
    for(int i = n-2; i >= 0; i--){
        scanf("%d", &ol);
        o.insert(o.begin(), ol);
    }
    if(h<w){
        int temp = h;
        h = w;
        w = temp;
    }
    int out = 0, left, biggest, a, b, c, biggestB, currentOut, biggestI = -1;
    while(true){
        left = h;
        biggest = 0;
        currentOut = 0;
        for(int i = biggestI + 1; i < n; i++){
            if(o[i] <= left && o[i] <= w){
                if(o[i] > biggest){
                    biggest = o[i];
                    biggestI = i;
                }
                a = left / o[i];
                b = biggest / o[i];
                if(biggest == o[i]){
                    biggestB = b;
                }
                currentOut += a * b;
                left -= a * o[i];
                if(left == 0){
                    break;
                }
            }
        }
        c = w / biggest;
        out += currentOut * c;
        w -= biggest * biggestB * c;
        if(w == 0){
            break;
        }
    }
    printf("%d", out);
    return 0;
}