HDU-2844 Coins 多重背包

给定各种数量的硬币,求能够组合成多少种符合题意的组合数。

代码如下:

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <iostream>
#define MAXN 100000
using namespace std;

int N, M, dp[MAXN+5], p[105], n[105];

void zobag(int x, int k)
{
    int ans = 0;
    if (M>=k*p[x]) {
        for (int i = M-k*p[x]; i >= 0; --i) {
            if (dp[i]) {
                dp[i+k*p[x]] = 1;
            }
        }
    }
}

int DP()
{
    int ans = 0;
    for (int i = 0; i < N; ++i) {
        int k = 1;
        while (k <= n[i]) {
            zobag(i, k);
            n[i] -= k;
            k <<= 1;
        }
        if (n[i]) {
            zobag(i, n[i]);
        }
    }
    for (int i = 1; i <= M; ++i) {
        if (dp[i]) {
            ++ans;
        }
    }
    return ans;
}

int main()
{
    while (scanf("%d %d", &N, &M), N|M) {
        memset(dp, 0, sizeof (dp));
        dp[0] = 1;
        for (int i = 0; i < N; ++i) {
            scanf("%d", &p[i]);
        }
        for (int i = 0; i < N; ++i) {
            scanf("%d", &n[i]);
        }
        printf("%d\n", DP());
    }
    return 0;
}

 

posted @ 2012-04-23 15:08  沐阳  阅读(282)  评论(0编辑  收藏  举报