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.
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
解题思路:这题用二进制解法在poj却超时了,而杭电却不会(单调队列就别想了-->TLE);考虑多重部分和问题,时间复杂度是O(nW)。
hdu 2844 #AC代码(343ms):有坑,测试数据种W有为负数的,这就是一直RE的原因=_=...简单判断一下即可。
#include<algorithm>
#include<string.h>
#include<cstdio>
#include<iostream>
using namespace std;
int n,W,val[],cnt[],dp[];
int main(){
while(~scanf("%d%d",&n,&W)&&(n|W)){
for(int i=;i<=n;++i)scanf("%d",&val[i]);
for(int i=;i<=n;++i)scanf("%d",&cnt[i]);
if(W<=){puts("");continue;}
memset(dp,-,sizeof(dp));dp[]=;//注意初始化dp[0]为0
for(int i=;i<=n;++i){
for(int j=;j<=W;++j){
if(dp[j]>=)dp[j]=cnt[i];
else if(j<val[i]||dp[j-val[i]]<=)dp[j]=-;//面额太大或者或者在配更小的数时数量已经用光了
else dp[j]=dp[j-val[i]]-;
}
}
int ans=count_if(dp+,dp+W+,bind2nd(greater_equal<int>(),));//统计不小于0的个数
printf("%d\n",ans);
}
return ;
}
poj 1742 #AC代码(1813ms):中规中矩,W不会出现负数。
#include<algorithm>
#include<string.h>
#include<cstdio>
#include<iostream>
using namespace std;
int n,W,val[],cnt[],dp[];
int main(){
while(~scanf("%d%d",&n,&W)&&(n|W)){
memset(dp,-,sizeof(dp));dp[]=;
for(int i=;i<=n;++i)scanf("%d",&val[i]);
for(int i=;i<=n;++i)scanf("%d",&cnt[i]);
for(int i=;i<=n;++i){
for(int j=;j<=W;++j){
if(dp[j]>=)dp[j]=cnt[i];
else if(j<val[i]||dp[j-val[i]]<=)dp[j]=-;
else dp[j]=dp[j-val[i]]-;
}
}
int ans=count_if(dp+,dp+W+,bind2nd(greater_equal<int>(),));
printf("%d\n",ans);
}
return ;
}