题目地址:http://poj.org/problem?id=1285
其实就是很简单的一道背包题目。 并且泛化背包并没有什么难点。
这道题只需要枚举每个数的个数就好了, dp[i[背包容量为i的答案, 状态转移方程:dp[i] += dp[i - x] (x为枚举某个数的个数)
#include <cstdio>
#include <algorithm>
#include <string>
#include <cmath>
#include <set>
#include <map>
#include <vector>
#include <cstdlib>
#include <queue>
#include <iostream>
#include <functional>
#include <cstring>
#define UFOR(i, a, b) for(int i = a; i <= b; i++)
#define DFOR(i, a, b) for(int i = a; i >= b; i--)
#define MEM0(a) memset(a, 0, sizeof(a))
#define MEM1(a) memset(a, -1, sizeof(a))
#define MEMINF(a) memset(a, 0x3f3f3f3f, sizeof(a))
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
#define FIN freopen("in.txt", "r", stdin)
#define FOUT freopen("out.txt", "w", stdout)
using namespace std;
//typedef __int64 LL;
typedef long long LL;
const int MXN = 2e3 + 10;
const int MXM = 2e4 + 130;
const int HS = 1000007;
const int INF = 0x3f3f3f3f;
const int MOD = 1000000007;
int num[55];
LL dp[55];
int main ()
{
int n, m, kase = 1;
while (~scanf("%d %d", &n, &m), n > 0) {
MEM0(num);
MEM0(dp);
UFOR (i, 1, n) {
int k;
scanf("%d", &k);
num[k]++;
}
dp[0] = 1;
UFOR (i, 1, n) {
if (num[i] == 0) continue;
DFOR (j, n, 0) {
UFOR (x, 1, num[i]) { //注意x不能为0 , 因为j是倒着循环的。
if (j - x >= 0) dp[j] += dp[j - x];
}
}
}
printf("Case %d:\n", kase++);
while (m--) {
int x;
scanf("%d", &x);
printf("%I64d\n", dp[x]);
}
}
return 0;
}