码迷,mamicode.com
首页 > 其他好文 > 详细

HDU2844 Coins 多重背包

时间:2016-08-02 20:47:07      阅读:111      评论:0      收藏:0      [点我收藏+]

标签:

Coins

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 12330    Accepted Submission(s): 4922


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

 

 

题意:Tony想要买一个东西,他只有n中硬币每种硬币的面值为a[i]每种硬币的数量为c[i]要买的物品价值不超过m

输入:第一行输入n和m,第二行输入n个硬币的面值和n个硬币的数量,输入0 0结束

输出:1到m之间有多少价格Tony可以支付

 

技术分享
#include <iostream>
#include <cstring>

using namespace std;
int f[111111],a[111],c[111];
int n,m;
int main()
{
    while(cin>>n>>m)
    {
        if(n==0&&m==0)  break;
        for(int i=0;i<n;i++)
            cin>>a[i];
        for(int i=0;i<n;i++)
            cin>>c[i];
        memset(f,0,sizeof(f));
        f[0]=1;
        for(int i=0;i<n;i++)
        {

            int cut=c[i],k;
            for(k=1;k<=cut;k<<=1)
            {
                for(int j=m;j>=k*a[i];j--)
                    f[j]+=f[j-k*a[i]];
                cut-=k;
            }
            if(cut)
                for(int j=m;j>=cut*a[i];j--)
                    f[j]+=f[j-a[i]*cut];
        }
        int sum=0;
        for(int i=1;i<=m;i++)
                if(f[i])    sum++;
        cout<<sum<<endl;
    }
    return 0;
}
View Code

 

HDU2844 Coins 多重背包

标签:

原文地址:http://www.cnblogs.com/asuml/p/5730400.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!