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
// Author: Bartek Knapik

#include <cstdio>

int h, w, n;
long long ans;
int d[33];

long long solve(int cur_h, int cur_w)
{
    if (cur_h == 0 || cur_w == 0) return 0LL;
    if (cur_w < cur_h) return solve(cur_w, cur_h);
    
    int id, count_h, count_w;
    long long res;
    
    for (id = n - 1; id >= 0; --id) if (d[id] <= cur_h) break;
    count_h = cur_h / d[id];
    count_w = cur_w / d[id];
    
    res = 1LL * count_h * count_w + solve(cur_w, cur_h - count_h * d[id]) + solve(cur_w - count_w * d[id], count_h * d[id]);
    
    return res;
}


int main()
{
    scanf("%d%d", &h, &w);
    scanf("%d", &n);
    for (int i = 0; i < n; ++i) scanf("%d", &d[i]);
    
    if (h % d[0] || w % d[0]) ans = -1LL;
    else ans = solve(h, w);
    
    printf("%lld\n", ans);
    
    return 0;
}