## poj 1742 Coins(多重背包)

2019年04月13日 算法 ⁄ 共 3809字 ⁄ 字号 评论关闭
Coins
 Time Limit: 3000MS Memory Limit: 30000K Total Submissions: 26605 Accepted: 9026

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

```#include<algorithm>
#include<iostream>
#include<string.h>
#include<sstream>
#include<stdio.h>
#include<math.h>
#include<vector>
#include<string>
#include<queue>
#include<set>
#include<map>
using namespace std;
const int INF=0x3f3f3f3f;
const double eps=1e-8;
const double PI=acos(-1.0);
const int maxn=100010;
typedef __int64 ll;

int n,m,lim,f[maxn],a[110],c[110];

void zero(int wei,int val)
{
for(int i=lim; i>=wei; i--)
f[i]=max(f[i],f[i-wei]+val);
}
void complete(int wei,int val)
{
for(int i=wei; i<=lim; i++)
f[i]=max(f[i],f[i-wei]+val);
}
void mul(int wei,int val,int mou)
{
if(wei*mou>=lim)
{
complete(wei,val);
return ;
}
for(int k=1; k<mou; k<<=1)//必须这样能用lowbit优化。因为这样可以组成任意数。而lowbit不行
{
zero(wei*k,val*k);
mou-=k;
}
zero(mou*wei,mou*val);
}
int main()
{
int i,ans;
while(scanf("%d%d",&n,&m),n||m)
{
ans=0,lim=m;
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
scanf("%d",&c[i]);
memset(f,0,sizeof f);
for(i=0;i<n;i++)
mul(a[i],a[i],c[i]);
for(i=1;i<=m;i++)
if(f[i]==i)
ans++;
printf("%d\n",ans);
}
return 0;
}```

```#include<algorithm>
#include<iostream>
#include<string.h>
#include<sstream>
#include<stdio.h>
#include<math.h>
#include<vector>
#include<string>
#include<queue>
#include<set>
#include<map>
using namespace std;
const int INF=0x3f3f3f3f;
const double eps=1e-8;
const double PI=acos(-1.0);
const int maxn=100010;
typedef __int64 ll;

int n,m,lim,a[110],c[110];
bool f[maxn];

void zero(int wei,int val)
{
for(int i=lim; i>=wei; i--)
f[i]|=f[i-wei];
}
void complete(int wei,int val)
{
for(int i=wei; i<=lim; i++)
f[i]|=f[i-wei];
}
void mul(int wei,int val,int mou)
{
if(wei*mou>=lim)
{
complete(wei,val);
return ;
}
for(int k=1; k<mou; k<<=1)//必须这样能用lowbit优化。因为这样可以组成任意数。而lowbit不行
{
zero(k*wei,k*val);
mou-=k;
}
zero(mou*wei,mou*val);
}
int main()
{
int i,ans;
while(scanf("%d%d",&n,&m),n||m)
{
ans=0,lim=m;
for(i=0; i<n; i++)
scanf("%d",&a[i]);
for(i=0; i<n; i++)
scanf("%d",&c[i]);
memset(f,0,sizeof f);
f[0]=1;
for(i=0; i<n; i++)
mul(a[i],a[i],c[i]);
for(i=1; i<=m; i++)
if(f[i])
ans++;
printf("%d\n",ans);
}
return 0;
}
```

```#include<algorithm>
#include<iostream>
#include<string.h>
#include<sstream>
#include<stdio.h>
#include<math.h>
#include<vector>
#include<string>
#include<queue>
#include<set>
#include<map>
using namespace std;
const int INF=0x3f3f3f3f;
const double eps=1e-8;
const double PI=acos(-1.0);
const int maxn=100010;
typedef __int64 ll;
int n,m,lim,a[110],c[110],num[maxn],ans;
bool f[maxn];
void pack(int val,int mou)//多重背包O(N*V)。
{
for(int i=val;i<=lim;i++)
{
if(f[i])
num[i]=0;
else if(f[i-val])
{
if(i<2*val)//初始化盲区超过这个区域的会在上面if语句初始化
{
num[i]=1;
f[i]=1;
ans++;
}
else if(num[i-val]<mou)
{
num[i]=num[i-val]+1;
f[i]=1;
ans++;
}
}
}
}
int main()
{
int i;
while(scanf("%d%d",&n,&m),n||m)
{
ans=0,lim=m;
for(i=0; i<n; i++)
scanf("%d",&a[i]);
for(i=0; i<n; i++)
scanf("%d",&c[i]);
memset(f,0,sizeof f);
f[0]=1;
for(i=0; i<n; i++)
pack(a[i],c[i]);
printf("%d\n",ans);
}
return 0;
}```