POJ1742 Coin [DP补完计划]
Posted H
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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:
1 //It is made by HolseLee on 17th May 2018 2 //POJ 1742 3 #include<cstdio> 4 #include<cstring> 5 #include<cstdlib> 6 #include<cmath> 7 #include<iostream> 8 #include<iomanip> 9 #include<algorithm> 10 #define Fi(i,a,b) for(int i=a;i<=b;i++) 11 #define Fx(i,a,b) for(int i=a;i>=b;i--) 12 using namespace std; 13 const int N=1e5+7;bool dp[N]; 14 int n,m,cnt,ans,k[N],a[101],c[101]; 15 int main() 16 { 17 ios::sync_with_stdio(false); 18 while(cin>>n>>m){ 19 if(!n||!m)break; 20 memset(dp,false,sizeof(dp)); 21 dp[0]=true;cnt=0;ans=0; 22 Fi(i,1,n)cin>>a[i];Fi(i,1,n)cin>>c[i]; 23 Fi(i,1,n){ 24 for(int j=1;j<=c[i];j<<=1){ 25 k[++cnt]=a[i]*j;c[i]-=j;} 26 if(c[i])k[++cnt]=a[i]*c[i];} 27 Fi(i,1,cnt)Fx(j,m,k[i]){ 28 dp[j]|=dp[j-k[i]];} 29 Fi(i,1,m)if(dp[i])ans++; 30 cout<<ans<<"\n";} 31 return 0; 32 }
以上是关于POJ1742 Coin [DP补完计划]的主要内容,如果未能解决你的问题,请参考以下文章