Coins(多重&&完全背包)

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 <iostream>
#include <cstdio>
#include <string>
#include <cmath>
#include <algorithm>
#include <cstring>
#define rush() int T;cin>>T;while(T--)
#define sf(a) scanf("%lf\n",&a)
#define sd(a) scanf("%d\n",&a)
#define sdt(a,b) scanf("%d%d\n",&a,&b)
#define go(a) while(cin>>a&&a)
#define ms(a,b) memset(a,b,sizeof a)
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define pf(a) printf("%.8lf",a)
#define valu first
#define num second
#define exp 1e-8
using namespace std;
typedef long long ll;
const double pi=acos(-1.0);
const int inf=0x3f3f3f3f;
const int idata=1e5+5;
const int mod=10007;

int n,m,t;
int maxx,minn,ans;
int i,j,k;
int dp[idata];
pair< int,int >p[100+5];

void  completepack(int volu)
{
    for(int j=p[i].valu;j<=volu;j++)
    {
        dp[j]=max(dp[j],dp[j-p[i].valu]+p[i].valu);
    }
}
void multiplepack(int volu)
{
    for(int k=1;k<=p[i].num;k<<1)
    {
        for(int j=volu;j>=k*p[i].valu;j--)
        {
            dp[j]=max(dp[j],dp[j-k*p[i].valu]+k*p[i].valu);
        }
        p[i].num-=k;
    }
    if(p[i].num>0)
    {
        for(int j=m;j>=p[i].num*p[i].valu;j--)
        {
            dp[j]=max(dp[j],dp[j-p[i].num*p[i].valu]+p[i].num*p[i].valu);
        }
    }
}

int main()
{
    cin.tie(0);
    iostream::sync_with_stdio(false);
    while(cin>>n>>m&&n&&m)//寻找dp[i]==i
    {
        ms(dp,0);
        for(i=1;i<=n;i++) cin>>p[i].valu;
        for(i=1;i<=n;i++) cin>>p[i].num;
        for(i=1;i<=n;i++)
        {
            if(p[i].valu*p[i].num>=m)
            {
                completepack(m);
            }
            else
            {
                multiplepack(m);
            }
        }
        ans=0;
        for(i=1;i<=m;i++)
        {
            if(dp[i]==i) ans++;
        }
        cout<<ans<<endl;
    }
    return 0;
}
原创文章 410 获赞 16 访问量 3万+

猜你喜欢

转载自blog.csdn.net/C_Dreamy/article/details/105810463