poj 1742 多重背包 气死个人明天接着看啊

来源:互联网 发布:淘宝卖家店铺装修教程 编辑:程序博客网 时间:2024/05/22 10:01
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 101 2 4 2 1 12 51 4 2 10 0

Sample Output

8

4

//位运算优化#include"iostream"using namespace std;int main(){    int n,m;    int A[100],C[100];    char dp[100010];    while(scanf("%d%d",&n,&m)&&n!=0&&m!=0)    {        for(int i=0;i<n;i++)scanf("%d",&A[i]);        for(int i=0;i<n;i++)scanf("%d",&C[i]);        memset(dp,0,m+1);dp[0]=1;        for(int i=0;i<n;i++)        {            if(A[i]>m)continue;if(A[i]*C[i]>=m)for(int j=A[i];j<=m;j++)dp[j]=dp[j]|dp[j-A[i]];else            {                int k=0,temp;                while((1<<k)<C[i])                {                    temp=A[i]<<k;                    for(int j=m;j>=temp;j--)dp[j]=dp[j]|dp[j-temp];                    C[i]-=(1<<k);                    k++;                }                temp=C[i]*A[i];                for(int j=m;j>=temp;j--)dp[j]=dp[j]|dp[j-temp];            }        }        int cnt=0;        for(int i=1;i<=m;i++)            if(dp[i])cnt++;        cout<<cnt<<endl;    }}#include <cstdio>#include <cstring>#include <iostream>using namespace std;int dp[100005];//01背包inline void ZeroOnePack(int val, int vol, int volume){    for(int i = volume; i >= vol; i--)    {        if(dp[i] < dp[i-vol] + val){            dp[i] = dp[i-vol] + val;        }    }}//完全背包inline void CompletePack(int val, int vol, int volume){    for(int i = vol; i <= volume; i++)    {        if(dp[i] < dp[i-vol] + val){            dp[i] = dp[i-vol] + val;        }    }}//多重背包inline void MultiplePack(int val, int vol, int counts, int volume){    if(vol * counts >= volume)        CompletePack(val, vol, volume);    else {        int k = 1;        while(k < counts)        {            ZeroOnePack(k * val, k * vol, volume);            counts -= k;            k <<= 1;        }        ZeroOnePack(counts * val, counts * vol, volume);    }}int main(){    int n, m;    int val[105];    while(cin>>n>>m && (n || m))    {        memset(dp, 0, sizeof(dp));        for(int i = 0; i < n; i++)            cin>>val[i];        int counts;        for(int i = 0; i < n; i++)        {            cin>>counts;            MultiplePack(val[i], val[i], counts, m);        }        int sum = 0;        for(int i = 1; i <= m; i++)            if(dp[i] == i)                sum++;        cout<<sum<<endl;    }    return 0;}//多重背包问题//dp数组为 1 表示能够凑够当前的钱数,否则为 0//used数组用来记录对于当前的第i coins,已经使用了多少次(注意是对于相关的钱数的使用的次数)#include <iostream>#include <cstring>using namespace std;int a[105], b[105], dp[100050], used[100050];int main(){int n, m;while (cin >> n >> m&&n&&m){for (int i = 1; i <= n; i++)cin >> a[i];for (int i = 1; i <= n; i++)cin >> b[i];memset(dp, 0, sizeof(dp));dp[0] = 1;int ans = 0;for (int i = 1; i <= n; i++){memset(used, 0, sizeof(used));for (int j = a[i]; j <= m; j++){if (dp[j] == 0 && dp[j - a[i]] == 1 && used[j - a[i]] < b[i]){//当前钱数没有凑够过,且j-a[i]凑够了,,且凑j-a[i]一共用的a[i]的次数少于b[i]dp[j] = 1;ans++;used[j] = used[j - a[i]] + 1;}}}cout << ans << endl;}return 0;}#include<stdio.h>#include<bitset>#include<algorithm>using namespace std;int main(){    int n,m,c,i,a[101];    while(scanf("%d%d",&n,&m),n||m)    {        for(i=1;i<=n;i++) scanf("%d",&a[i]);        bitset<100002> ans(0);//初始化时全为0        ans.set(0);//将第0位设为1        for(i=1;i<=n;i++)        {            scanf("%d",&c);            int k=1;            while(k<=c)            {                ans|=ans<<(k*a[i]);//将原来ans里面所有的1往左移k*a[i]位然后或上原来的,状态更新完毕                c-=k;                k<<=1;            }            if(c>0) ans|=ans<<(c*a[i]);//如果还有剩下的,那么也要考虑进去,不然会错        }        int sum=0;        for(i=1;i<=m;i++)            sum+=ans[i];//sum储存ans里面为1的个数        printf("%d\n",sum);    }    return 0;}