HDU 2844 Coins (多重背包+二进制优化)

127 篇文章 0 订阅
85 篇文章 2 订阅

Coins

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Problem Description

Whuacmers use coins.They have coins of value A1,A2,A3…An Silverland dollar. One day Hibix opened purse and found there were some coins. He decided to buy a very nice watch in a nearby shop. He wanted to pay the exact price(without change) and he known the price would not more than m.But he didn’t know the exact price of the watch.

You are to write a program which reads n,m,A1,A2,A3…An and C1,C2,C3…Cn corresponding to the number of Tony’s coins of value A1,A2,A3…An then calculate how many prices(form 1 to m) Tony can pay use these coins.

Input

The input contains several test cases. The first line of each test case contains two integers n(1 ≤ n ≤ 100),m(m ≤ 100000).The second line contains 2n integers, denoting A1,A2,A3…An,C1,C2,C3…Cn (1 ≤ Ai ≤ 100000,1 ≤ Ci ≤ 1000). The last test case is followed by two zeros.

Output

For each test case output the answer on a single line.

Sample Input

3 10
1 2 4 2 1 1
2 5
1 4 2 1
0 0

Sample Output

8
4

问题分析

现在一共有n种硬币,给出每种硬币的价值与个数,问用这些硬币可以组合出多少种价值。(总价值不超过输入的m)

lz一开始傻傻的直接用多重背包,直接枚举,结果可想而知…对于这个数据肯定要tle的。后来知道可以用二进制来优化。so,接下来稍微简单的解释一下二进制优化。
假如现在有

8种物品,可以用2^0,2^1,2^2,2^3来组合出这所有8种物品;
16种物品,可以用2^0,2^1,2^2,2^3,2^4来组合出这所有16种物品。
(任意一个实数都可以用二进制来表示)

多重背包单纯的枚举个数和二进制分成堆来枚举组合,最终的结果是一样的。你想喔,一个一个的枚举选与不选,最后选取的个数不都是在1->n吗,这些都可以用上面的栗子来表示喔。

二进制优化完之后,再01枚举硬币重组后的价值。最后再枚举dp,如果dp[i]==i,则说明是一种结果,最后加起来就是答案。

ok,上code。

#include<algorithm>
#include<iostream>
#include<stdio.h>
#include<string.h>
#define N 110010
#define max(a,b) a>b?a:b
using namespace std;
int dp[N],V[N],n,m;
struct coin
{
    int v,cnt;
}co[105];

int binaryOptimize()    //二进制优化
{
    int k = 1;
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= co[i].cnt; j<<=1)
        {
            V[k++] = j*co[i].v;
            co[i].cnt -= j;
        }
        if(co[i].cnt>0)
        V[k++] = co[i].cnt*co[i].v;
    }
    return k;
}

void zeroOne()  //01枚举重新分配后的价值
{
    int k = binaryOptimize();
    for(int i = 1; i < k; i++)
        for(int j = m; j >= V[i]; j--)
        dp[j] = max(dp[j],dp[j-V[i]]+V[i]);
}

void out()  //输出
{
    int num = 0;
    for(int i = 1; i <= m; i++)
    if(dp[i]==i)
    num++;
    printf("%d\n",num);
}

int main()
{
    // freopen("in.txt","r",stdin);
    while(~scanf("%d%d",&n,&m)&&(n!=0||m!=0))
    {
        memset(co,0,sizeof(co));
        memset(dp,0,sizeof(dp));
        memset(V,0,sizeof(V));
        for(int i = 1; i <= n; i++)
        scanf("%d",&co[i].v);
        for(int i = 1; i <= n; i++)
        scanf("%d",&co[i].cnt);
        zeroOne();
        out();
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值