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.

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 }

原文地址:https://www.cnblogs.com/cytus/p/9053093.html

时间: 2024-10-11 23:38:38

POJ1742 Coin [DP补完计划]的相关文章

CodeVS1169 传纸条 [DP补完计划]

题目传送门 题目描述 Description 小渊和小轩是好朋友也是同班同学,他们在一起总有谈不完的话题.一次素质拓展活动中,班上同学安排做成一个m行n列的矩阵,而小渊和小轩被安排在矩阵对角线的两端,因此,他们就无法直接交谈了.幸运的是,他们可以通过传纸条来进行交流.纸条要经由许多同学传到对方手里,小渊坐在矩阵的左上角,坐标(1,1),小轩坐在矩阵的右下角,坐标(m,n).从小渊传到小轩的纸条只可以向下或者向右传递,从小轩传给小渊的纸条只可以向上或者向左传递. 在活动进行中,小渊希望给小轩传递一

哈萨比斯的人类补完计划

在著名动漫<新世纪福音战士>里,碇源堂和他背后的SEELE组织始终在执行一项叫做"人类补完计划"的神秘行动. 这个计划到底是什么意思,粉丝们已经争吵了很多年.但大体上应该是说利用"神性"来补完人类族群,从而消除人类社会中的种种问题.也就是说,这个计划是"把人类补完的计划". 但"人类补完"这个词,其实还可以有另一种解释,就是"用人类去补完某种东西的计划".有东西需要用人类来补完吗?当然有,比如说

【图论补完计划】poj 3635 (最短路变形)

Full Tank? Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7427   Accepted: 2399 Description After going through the receipts from your car trip through Europe this summer, you realised that the gas prices varied between the cities you v

【图论补完计划】poj 3463 (次短路计数 dijkstra)

Sightseeing Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9127   Accepted: 3206 Description Tour operator Your Personal Holiday organises guided bus trips across the Benelux. Every day the bus moves from one city S to another city F. O

【图论补完计划】poj 3522 (最小生成树)

Slim Span Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 7933   Accepted: 4227 Description Given an undirected weighted graph G, you should find one of spanning trees specified as follows. The graph G is an ordered pair (V, E), where V

【图论补完计划】poj 2723(2-SAT)

Get Luffy Out Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8688   Accepted: 3371 Description Ratish is a young man who always dreams of being a hero. One day his friend Luffy was caught by Pirate Arlong. Ratish set off at once to Arlo

hdu2844 &amp; poj1742 Coin ---多重背包--两种方法

题意:你有N种硬币,每种价值A[i],每种数量C[i],问.在不超过M的情况下,我们用这些硬币,付款有多少种情况.也就是:1,2,3,4,5,....,M这么多种情况下,你能用你的硬币不找钱,付款多少种情况. 例如: 你有一种硬币,价值2,个数2,那么 你是不能付款 3元的..你只能付款2,或者4元.. OK,题意差不多就是这样啦. 那么这里有两种方式! 分析: 那么这里我们可以用多重背包来解决,我们把价值和重量看成一样的w[i] = A[i]:用M作为背包.那么dp 过后,我们就可以知道 dp

专题补全计划

专题补全计划 概述 时间: 寒假+大三下学期 要求: 1.每一类型的专题都要有专门的总结贴,有新的体会应该去更新总结贴. 2.大量做题和总结,及时复习,整理模板. 3.适当与外界交流,必要时需要完善训练计划. 4.持之以恒! 模板链接:(待完善) https://github.com/Wowkiee/ACM-ICPC/tree/master/template 知识点(待完善) 数据结构 ST表 线段树 主席树 动态规划 动态规划 区间dp 图论 最短路 最小生成树 网络流 联通分量 数学 pol

补番计划 (长沙理工大学第十一届程序设计竞赛)(双端队列+set容器+string)

补番计划 Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 65535/32768K (Java/Other) Total Submission(s) : 8   Accepted Submission(s) : 1 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description 阿聪是一个日漫狂魔.暑假在家的时候,他有12小时在补番,12小时在睡