POJ1742 Coin [DP补完计划]
Coins
Time Limit: 3000MS | Memory Limit: 30000K | |
Total Submissions: 41707 | Accepted: 14125 |
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.
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
分析:很显然的多重背包,由数据范围可知需要进行拆分优化或者用单调队列优化。这里蒟蒻还不会单调队列优化,所以用的是二进制拆分。
Code:
//It is made by HolseLee on 17th May 2018 //POJ 1742 #include<cstdio> #include<cstring> #include<cstdlib> #include<cmath> #include<iostream> #include<iomanip> #include<algorithm> #define Fi(i,a,b) for(int i=a;i<=b;i++) #define Fx(i,a,b) for(int i=a;i>=b;i--) using namespace std; const int N=1e5+7;bool dp[N]; int n,m,cnt,ans,k[N],a[101],c[101]; int main() { ios::sync_with_stdio(false); while(cin>>n>>m){ if(!n||!m)break; memset(dp,false,sizeof(dp)); dp[0]=true;cnt=0;ans=0; Fi(i,1,n)cin>>a[i];Fi(i,1,n)cin>>c[i]; Fi(i,1,n){ for(int j=1;j<=c[i];j<<=1){ k[++cnt]=a[i]*j;c[i]-=j;} if(c[i])k[++cnt]=a[i]*c[i];} Fi(i,1,cnt)Fx(j,m,k[i]){ dp[j]|=dp[j-k[i]];} Fi(i,1,m)if(dp[i])ans++; cout<<ans<<"\n";} return 0; }
蒟蒻写博客不易,如果有误还请大佬们提出
如需转载,请署名作者并附上原文链接,蒟蒻非常感激
名称:HolseLee
博客地址:www.cnblogs.com/cytus
个人邮箱:1073133650@qq.com
如需转载,请署名作者并附上原文链接,蒟蒻非常感激
名称:HolseLee
博客地址:www.cnblogs.com/cytus
个人邮箱:1073133650@qq.com