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
/* 2024
 * Maciej Szeptuch
 */
#include <cstdio>

const int MAX_PAINTINGS = 31;

int width;
int height;
int paintings;
int size[MAX_PAINTINGS];

long long int fill(int p, int w, int h);

int main(void)
{
    scanf("%d %d %d", &width, &height, &paintings);
    for(int p = 0; p < paintings; ++p)
        scanf("%d", &size[p]);

    if(width % size[0] || height % size[0])
    {
        puts("-1");
        return 0;
    }

    printf("%lld\n", fill(paintings - 1, width, height));
    return 0;
}

inline long long int fill(int p, int w, int h)
{
    if(w == 0 || h == 0 || p < 0)
        return 0;

    int wc = w / size[p];
    int hc = h / size[p];
    return (long long int)wc * hc + fill(p - 1, w - wc * size[p], hc * size[p]) + fill(p - 1, w, h - hc * size[p]);
}