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

点击打开链接

Coins

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

Total Submission(s): 8167    Accepted Submission(s): 3327

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

Source

2009 Multi-University Training Contest 3 - Host
by WHU

有n种货币,告诉你每种货币的价值和数量,让你求这些货币能够恰好构成1~m的情况数。

多重背包模板题。

//499MS	1444K
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define M 100007
using namespace std;
int dp[M],val[107],num[107],m;
int n;
void ZeroOnePack(int value)
{
    for(int i=m;i>=value;i--)
        dp[i]=max(dp[i],dp[i-value]+value);
}
void CompletePack(int value)
{
    for(int i=value;i<=m;i++)
        dp[i]=max(dp[i],dp[i-value]+value);
}
void MultiplePack(int count,int value)
{
    if(count*value>m)CompletePack(value);
    else
    {
        int k=1;
        while(k<count)
        {
            ZeroOnePack(k*value);
            count-=k;
            k<<=1;
        }
        ZeroOnePack(count*value);
    }
}
int main()
{
    while(scanf("%d%d",&n,&m),n|m)
    {
        int ans=0,k=0;
        memset(dp,0,sizeof(dp));
        for(int i=1;i<=n;i++)
            scanf("%d",&val[i]);
        for(int i=1;i<=n;i++)
            scanf("%d",&num[i]);
        for(int i=1;i<=n;i++)
            MultiplePack(num[i],val[i]);
        for(int i=1;i<=m;i++)
            if(dp[i]==i)ans++;
        printf("%d\n",ans);
    }
}

时间: 2024-12-29 11:29:22

HDU 2844 Coins 多重背包(二进制优化)的相关文章

hdu 2844 Coins 多重背包模板题 ,二进制优化。据说是楼教主的男人八题之一

Coins Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 8052    Accepted Submission(s): 3291 Problem Description Whuacmers use coins.They have coins of value A1,A2,A3...An Silverland dollar. One

HDu -2844 Coins多重背包

这道题是典型的多重背包的题目,也是最基础的多重背包的题目 题目大意:给定n和m, 其中n为有多少中钱币, m为背包的容量,让你求出在1 - m 之间有多少种价钱的组合,由于这道题价值和重量相等,所以就是dp[i] = i, 其中dp[i]表示当前背包容量为i 的时候背包能装的价值. 题目思路: 模板 二进制优化 话说那个二进制真的很奇妙,只需要2的1次方 到 2的k-1次方, 到最后在加上一项当前项的个数 - 2 的k次方 + 1,也就是这些系数分别为1; 2; 22 .....2k-1;Mi

HDU - 2844 Coins(多重背包+完全背包)

题意 给n个币的价值和其数量,问能组合成\(1-m\)中多少个不同的值. 分析 对\(c[i]*a[i]>=m\)的币,相当于完全背包:\(c[i]*a[i]<m\)的币则是多重背包,考虑用二进制优化解决.最后扫一遍\(dp[i]\)统计答案. import java.util.*; import java.math.*; public class Main{ static int MAXN = 100005; static int []dp = new int[MAXN]; static i

HDU 2844 Coins (多重背包计数 空间换时间)

Coins Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 8999    Accepted Submission(s): 3623 Problem Description Whuacmers use coins.They have coins of value A1,A2,A3...An Silverland dollar. One

HDU 1059 多重背包+二进制优化

Dividing Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 16909    Accepted Submission(s): 4729 Problem Description Marsha and Bill own a collection of marbles. They want to split the collection

HDU 2844 Coins (组合背包)

题意   给你n种面额不同的金币和每种金币的个数  求这些金币能组合成的面额在m内有多少种 还是明显的背包问题  d[i]表示这些金币在i内能组合成的最大面额  初始化d为负无穷  d[0]=0  这样就可以保证d[i]恰好为i时才能为正值 原因可以自己想想  然后就用背包背吧  直接多重背包也可以过  但是分成多重背包和完全背包要快一点 #include<cstdio> #include<cstring> #include<algorithm> using names

台州 OJ 2537 Charlie&#39;s Change 多重背包 二进制优化 路径记录

描述 Charlie is a driver of Advanced Cargo Movement, Ltd. Charlie drives a lot and so he often buys coffee at coffee vending machines at motorests. Charlie hates change. That is basically the setup of your next task. Your program will be given numbers

[多重背包+二进制优化]HDU1059 Dividing

题目链接 题目大意: 两个人要把一堆宝珠,在不能切割的情况下按照价值平分,他们把宝珠分成6种价值,每种价值的宝珠n个. n<=200000 思考: 首先如果加和下来的价值是一个偶数 那么还分毛啊,直接gg. 之后多重背包二进制优化 转换为 01背包. 我们可以把价值 同时当做宝珠的空间和价值. 那么我们现在要求的是 在 空间为一半的情况下,能否找到价值为 一半的情况. 1 #include <cstdio> 2 #include <algorithm> 3 #include

14年省赛---多重部分和问题(多重背包+二进制优化)

1210: F.多重部分和问题 时间限制: 1 Sec  内存限制: 64 MB提交: 18  解决: 14 题目描述 有n种不同大小的数字,每种各个.判断是否可以从这些数字之中选出若干使它们的和恰好为K. 输入 首先是一个正整数T(1<=T<=100)接下来是T组数据 每组数据第一行是一个正整数n(1<=n<=100),表示有n种不同大小的数字 第二行是n个不同大小的正整数ai(1<=ai<=100000)第三行是n个正整数mi(1<=mi<=100000