forked from sitz/UVa-Online-Judge
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path10532.cpp
96 lines (94 loc) · 1.18 KB
/
10532.cpp
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
86
87
88
89
90
91
92
93
94
95
96
#include <bits/stdc++.h>
using namespace std;
/*
Combination Once Again
10532
*/
typedef unsigned long long ss;
int F[52], A[52], Fre[52];
ss R[52][52], Sum;
int N, M, ind;
ss Count(int type, int r, int sum)
{
int i;
ss ways = 0;
if (Sum - sum < r)
{
return 0;
}
if (r == 0)
{
return 1;
}
if (R[type][r])
{
return R[type][r];
}
if (type == 0 && r <= A[0])
{
R[type][r] = 1;
return 1;
}
if (type == 0)
{
return 0;
}
for (i = 0; i <= A[type] && r - i >= 0; i++)
{
ways += Count(type - 1, r - i, sum + Fre[type]);
}
R[type][r] = ways;
return ways;
}
void Cal()
{
int i, n;
for (i = 0; i < M; i++)
{
scanf("%d", &n);
printf("%llu\n", Count(ind - 1, n, 0));
}
}
void Free()
{
int i, j;
for (i = 0; i <= N; i++)
{
F[i] = 0;
for (j = 0; j <= N; j++)
{
R[i][j] = 0;
}
}
}
int main()
{
int i, n, kase = 1;
while (scanf("%d%d", &N, &M) == 2)
{
if (N == 0)
{
break;
}
Sum = 0;
for (i = 0; i < N; i++)
{
scanf("%d", &n);
F[n]++;
}
ind = 0;
for (i = 0; i <= N; i++)
{
if (F[i])
{
A[ind++] = F[i];
Sum += F[i];
Fre[ind - 1] = F[i];
}
}
printf("Case %d:\n", kase++);
Cal();
Free();
}
return 0;
}