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
#include<iostream>
#include<algorithm>
#include<string.h>
int dp[][100003];
int val[],num[];
using namespace std;
int main(){
int n,m;
while(cin>>n>>m&&!(n==&&m==)){
memset(dp,,sizeof(dp));
for(int i=;i<n;i++){
cin>>val[i];
}
for(int i=;i<n;i++){
cin>>num[i];
}
dp[][]=;
for(int i=;i<n;i++){
for(int j=;j<=m;j++){
for(int w=;w<=num[i]&&w*val[i]<=j;w++){
dp[i+][j]|=dp[i][j-w*val[i]];
}
}
}
int ans=count(dp[n]+,dp[n]++m,);
cout<<ans<<endl;
}
return ;
}
dp[i+1][j]表示前i种数字能否拼成j
一般用DP求取bool结果的话会有不少浪费,同样的复杂度可以获得很多信息

优化

dp[i+1][j]:用前i种数加和得到j时第i种数最多能剩几个

  1. dp[i][j] := 用前i种硬币凑成j时第i种硬币最多能剩余多少个(-1表示配不出来)
  2. 如果dp[i - 1][j] >= 0(前i-1个数可以凑出j,那么第i个数根本用不着)直接为C[i]
  3. dp[i][j] =  如果j < A[i]或者dp[i][j - a[i]] <=0 (面额太大或者在配更小的数的时候就用光了)-1
  4. 其他(将第i个数用掉一个) dp[i][j-a[i]] - 1
#include<iostream>
#include<algorithm>
#include<string.h>
int dp[][100003];
int val[],num[];
using namespace std;
int main(){
int n,m;
while(cin>>n>>m&&!(n==&&m==)){
memset(dp,-,sizeof(dp));
for(int i=;i<n;i++){
cin>>val[i];
}
for(int i=;i<n;i++){
cin>>num[i];
}
dp[][]=;
for(int i=;i<n;i++){
for(int j=;j<=m;j++){
if(dp[i][j]>=)
dp[i+][j]=num[i];
else if(j<val[i]||dp[i+][j-val[i]]<=){
dp[i+][j]=-;
}
else{
dp[i+][j]=dp[i+][j-val[i]]-;
}
}
}
int ans=;
for(int i=;i<=m;i++){
if(dp[n][i]!=-)
ans++;
}
cout<<ans<<endl;
}
return ;
}

数组重复利用

#include<iostream>
#include<algorithm>
#include<string.h>
int dp[];
int val[],num[];
using namespace std;
int main(){
int n,m;
while(cin>>n>>m&&!(n==&&m==)){
memset(dp,-,sizeof(dp));
for(int i=;i<n;i++){
cin>>val[i];
}
for(int i=;i<n;i++){
cin>>num[i];
}
dp[]=;
for(int i=;i<n;i++){
for(int j=;j<=m;j++){
if(dp[j]>=)
dp[j]=num[i];
else if(j<val[i]||dp[j-val[i]]<=){
dp[j]=-;
}
else{
dp[j]=dp[j-val[i]]-;
}
}
}
int ans=;
for(int i=;i<=m;i++){
if(dp[i]!=-)
ans++;
}
cout<<ans<<endl;
}
return ;
}

POJ1742--Coins(动态规划)的更多相关文章

  1. POJ1742 coins 动态规划之多重部分和问题

    原题链接:http://poj.org/problem?id=1742 题目大意:tony现在有n种硬币,第i种硬币的面值为A[i],数量为C[i].现在tony要使用这些硬币去买一块价格不超过m的表 ...

  2. POJ1742 Coins(男人八题之一)

    前言 大名鼎鼎的男人八题,终于见识了... 题面 http://poj.org/problem?id=1742 分析 § 1 多重背包 这很显然是一个完全背包问题,考虑转移方程: DP[i][j]表示 ...

  3. POJ1742 Coins[多重背包可行性]

    Coins Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 34814   Accepted: 11828 Descripti ...

  4. POJ1742:Coins(多重背包)

    Description People in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dollar. ...

  5. poj1742 Coins【多重背包】【贪心】

    Coins Time Limit: 3000MS   Memory Limit: 30000K Total Submissions:43969   Accepted: 14873 Descriptio ...

  6. poj1742 Coins(多重背包+单调队列优化)

    /* 这题卡常数.... 二进制优化或者单调队列会被卡 必须+上个特判才能过QAQ 单调队列维护之前的钱数有几个能拼出来的 循环的时候以钱数为步长 如果队列超过c[i]就说明队头的不能再用了 拿出来 ...

  7. POJ1742 Coins 背包

    题目大意:给出一些钱币的价值和对应的数目,求在一定价值限定下这些钱币能凑成的价值数. 本题用多重背包直接拆分或二进制拆分法都太慢.说起处理一组物品,完全背包可算是比较效率高的,但是本题中物体的数目是有 ...

  8. $POJ1742\ Coins$ 多重背包+贪心

    Vjudge传送门 $Sol$ 首先发现这是一个多重背包,所以可以用多重背包的一般解法(直接拆分法,二进制拆分法...) 但事实是会TLE,只能另寻出路 本题仅关注“可行性”(面值能否拼成)而不是“最 ...

  9. 背包问题(01背包,完全背包,多重背包(朴素算法&&二进制优化))

    写在前面:我是一只蒟蒻~~~ 今天我们要讲讲动态规划中~~最最最最最~~~~简单~~的背包问题 1. 首先,我们先介绍一下  01背包 大家先看一下这道01背包的问题  题目  有m件物品和一个容量为 ...

  10. 常规DP专题练习

    POJ2279 Mr. Young's Picture Permutations 题意 Language:Default Mr. Young's Picture Permutations Time L ...

随机推荐

  1. Vue 快速原型开发

    快速原型开发 注意: 是:serve 而不是 server 通过使用 vue serve 和 vue build 命令对单个 *.vue 文件进行快速原型开发,不过这需要先额外安装一个全局的扩展 go ...

  2. Oracle性能优化3-sql优化一定要等价

    做sql优化的前提瞧见是sql等价 1.MAX MIN写法的分与合 drop table t purge; create table t as select * from dba_objects; a ...

  3. 什么是MVVM模式

    问题引入1 场景一:团队辛辛苦苦完成了一个项目,抱着激动的心情去给用户做demo,而用户给你的反馈是UI很不满意,要重新修改,否则拒绝验收.大规模修改UI,晴天霹雳!2 场景二:产品在一家客户上线运行 ...

  4. 立即响应ScrollView上的子视图的手势

    self.myScrollView.delaysContentTouches = YES; self.myScrollView.CanCancelContentTouches=NO; 写了一个继承sc ...

  5. UOJ 274 温暖会指引我们前进 - LCT

    Solution 更新掉路径上温暖度最小的边就可以了~ Code #include<cstdio> #include<cstring> #include<algorith ...

  6. How to Disable/Enable IP forwarding in Linux

    This article describes how to Disable or Enable an IP forwarding in Linux. Current IP forwarding sta ...

  7. DNSlog实现Mysql注入

    step1: 通过DNSlog盲注需要用到load_file()函数.show variables like '%secure%' 查看load_file()可以读取的磁盘. 1.当secure_fi ...

  8. [JAVA]多线程下如何确定执行顺序性

    最近在讨论一个下载任务:要求文件下载后进行打包,再提供给用户下载: 如何确保打包的线程在所有下载文件的线程执行完成后进行呢? 看看下面三个兄弟的本事: CountDownLatch.CyclicBar ...

  9. IOS初级:UIScrollView & UIPageControl

    UIScrollView其实构建的就像一列很长的火车,每滑动一个屏幕,展示一节车厢. //主屏幕高度 #define kScreenHeight [UIScreen mainScreen].bound ...

  10. [C#]this.Invoke和this.BeginInvoke的区别

    private void button1_Click(object sender, EventArgs e) { "; this.Invoke(new EventHandler(delega ...