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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <cstdio>

class wall
{
public:
int H,W,N,D[30];

	wall();
int count(int h = -1,int w = -1);
};

wall::wall()
{
	scanf("%d %d",&H,&W);
	scanf("%d",&N);
	for (int n = 0; n < N; n++)
		scanf("%d",D+n);
	D[N] = 0;
	
//	for (int n = 0; n < N+1; n++) printf("%d ",D[n]);
//	printf("\n");
	
}

int wall::count(int h, int w)
{
int tmp,r = 0,R[30],mul,i,m;

	if (h == -1) h = H;
	if (w == -1) w = W;
	if (h == 0 || w == 0) return 0;

	if (h < w)
	{
		tmp = h;
		h = w;
		w = tmp;
	}
	
	tmp = w;
	i = N-1;
	while (i >= 0 && tmp < D[i]) i--;
	m = D[i];

//	printf("Lower = %d Max_fit = %d\n",tmp,D[i]);

	for (i = 0; i < N; i++) R[i] = 0;
	i = N-1;
	while (i >= 0 && tmp > 0)
	{
		while (i >= 0 && tmp < D[i]) i--;
		if (i < 0) return -1;
		R[i] += tmp / D[i];
//		printf("Counting R (for h=%d w=%d): %d / %d = %d\n",h,w,tmp,D[i],R[i]);
		tmp %= D[i];
	}
	
//	printf("For h=%d w=%d\n",h,w);
//	for (i = 0; i < N; i++) printf("%d ",R[i]);
//	printf("\n");

	r = 0;
	for (i = N-1; i >= 0; i--)
	{
		if (D[i] >= m) mul = 1; else mul *= D[i+1] / D[i];
//		printf("R=%d D[i+1]=%d D[i]=%d R[i]=%d mul=%d\n",r,D[i+1],D[i],R[i],mul);
		r += mul * R[i];
	}	
	
	return r + count(h-m,w);
}


/////////////////////////////////////

int main()
{
wall X;

	printf("%d\n",X.count());



	return 0;
}