您的位置:首页 > 其它

pku 1742 Coins(多重背包dp)

2013-06-30 17:37 176 查看
Coins

Time Limit: 3000MSMemory Limit: 30000K
Total Submissions: 25161Accepted: 8509
Description

People in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dollar.One day Tony opened his money-box 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

LouTiancheng@POJ

题意:给n种一定数量一定面值的硬币,问可以得出多少种不同面值的组合

题解:多重背包当完全背包做,然后计算多少个dp值不为0即可,本人感觉二进制优化比开一个cou数组计算已用数目慢,所以这里的AC代码不是二进制优化的多重背包

#include<stdio.h>
int val[105],num[105];
int cou[100005],dp[100005];
int main()
{
int n,m,i,j,sum;

while(scanf("%d%d",&n,&m),n!=0&&m!=0)
{
for(i=0;i<n;i++) scanf("%d",val+i);
for(i=0;i<n;i++) scanf("%d",num+i);
for(i=0;i<=m;i++) dp[i]=0;
for(dp[0]=i=0;i<n;i++)
{
for(j=0;j<=m;j++) cou[j]=0;
for(j=val[i];j<=m;j++)
if(cou[j-val[i]]<num[i]&&dp[j-val[i]]+val[i]>dp[j])
dp[j]=dp[j-val[i]]+val[i],cou[j]=cou[j-val[i]]+1;
}
for(sum=i=0;i<=m;i++) cou[i]=0;
for(i=1;i<=m;i++)
{
if(cou[dp[i]]||!dp[i]) continue;
cou[dp[i]]=1,sum++;
}
printf("%d\n",sum);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: