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
63
64
65
66
67
68
69
70
#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <iomanip>

// #define __DEBUG

#define REP(x,n) for (int x=0;x<(n);++x)
#define VAR(x,n) __typeof(n) x = (n)
#define FOREACH(x,c) for(VAR(x, (c).begin()); x != (c).end(); ++x)
#define CONTAINS(x,elem) ((x).find(elem) != (x).end())
#ifdef __DEBUG
    #define DEBUG(x) x
#else
    #define DEBUG(x)
#endif

using namespace std;

long long solve();

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout << solve() << endl;
    return 0;
}

#define MAX_N 31

long long sizes[MAX_N];

long long solve() {
    int w,h;
    int n;
    cin>>h>>w>>n;
    REP(x, n)
        cin>>sizes[x];

    if ((h % sizes[0]) || (w % sizes[0])) {
        return -1;
    }

    long long* currentSize = sizes + n;
    int remainingW = w, remainingH = h;
    long long result = 0;
    while (remainingW != 0 || remainingH != 0) {
        if (currentSize == sizes) {
            DEBUG(cerr << "Used all sizes, still no solution!" << endl;)
            return -1;
        } else {
            --currentSize;
        }
        long long fitsW = remainingW / *currentSize;
        long long fitsH = remainingH / *currentSize;

        result += (fitsW ? fitsW * (h / *currentSize) : 0)
            + (fitsH ? fitsH * (w / *currentSize) : 0)
            - (fitsW * fitsH);
        DEBUG(cerr << "size = " << *currentSize 
                    << ": add " << (fitsW ? fitsW * (h / *currentSize) : 0) 
                    << " + " << (fitsH ? fitsH * (w / *currentSize) : 0) 
                    << " - " << (fitsW * fitsH) << " --> " << result << endl;)

        remainingW -= fitsW * *currentSize;
        remainingH -= fitsH * *currentSize;
    }
    return result;
}