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
思路:
这道题需要优化,不优化会时间超限,我的思路是把在一次把所有值的个数用完,用多重背包写的,不过下标当成值,dp当成标记,来记录就行。
代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<iomanip>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#define ll long long
#define mes(x,y); memset(x,y,sizeof(x))
#define mv 2147483648+30
using namespace std;
int dp[1000000];
struct node{
int value,times;
}a[200000];
int w[200000];
int main(){
int n,m;
while(cin>>n>>m&&n!=0&&m!=0){
int x,y,cv=1;mes(dp,0);mes(w,0);mes(a,0);
for(int i=1;i<=n;i++){
cin>>a[i].value;
}
for(int i=1;i<=n;i++){
cin>>a[i].times;
}
dp[0]=1;
int sum=0;
for(int i=1;i<=n;i++){
mes(w,0);
for(int j=a[i].value;j<=m;j++){
if(dp[j-a[i].value]&&!dp[j]&&w[j-a[i].value]<a[i].times){
// cout<<j<<endl;
dp[j]=1;
sum++;
w[j]=w[j-a[i].value]+1;
}
}
}
cout<<sum<<endl;
}
}