您的位置:首页 > 其它

Coins (01背包)

2017-08-18 21:00 120 查看
##**coins**##


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

代码:

#include<stdio.h>
#include<string.h>
#include<math.h>
int max(int x,int y)
{
return x>y?x:y;
}

int main()
{
int n,m;
int num[105];
int a[105];
int val[100005];
int dp[100005];
while(scanf("%d%d",&n,&m),n&&m)
{
memset(a,0,sizeof(a));
memset(num,0,sizeof(num));
memset(val,0,sizeof(val));
memset(dp,0,sizeof(dp));
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
for(int i=1;i<=n;i++)
{
scanf("%d",&num[i]);
}
int w=1;
for(int i=1;i<=n;i++)
{
int sum=0;
for(int k=0;pow(2.0,k)<=num[i]-sum;k++)
{
val[w++]=a[i]*pow(2.0,k);
sum=sum+pow(2.0,k);
}
if(sum<num[i])
val[w++]=a[i]*(num[i]-sum);
}

for(int i=1;i<w;i++)
for(int j=m;j>=val[i];j--)
dp[j]=max(dp[j],dp[j-val[i]]+val[i]);
long long nub=0;
for(int i=1;i<=m;i++)
if(dp[i]==i)
nub++;
printf("%lld\n",nub);

}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: