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<cstdio>
using namespace std;
bool dp[100][100001];
int n,m,a[100],su[100];
int main(){
while(cin>>n>>m){
if(n==0&&m==0) return 0;
for(int i=0;i<n;i++)
for(int j=1;j<=m;j++)
dp[i][j]=false;
for(int i=0;i<n;i++)
scanf("%d",&a[i]),dp[i][0]=true; //dp[i][j]为真,表示j元可以凑出来 ,a[i]硬币的面额
for(int i=0;i<n;i++)
scanf("%d",&su[i]); //su[i]表示面额为a[i]的硬币的数量
for(int i=1;i<=su[0]&&a[0]*i<=m;i++) //注意a[0]*i<=m,or就会出现runtime error
dp[0][a[0]*i]=true;
for(int i=1;i<n;i++){
for(int j=1;j<=m;j++){
for(int k=1;k<=su[i];k++){
if(j-k*a[i]>=0&&dp[i-1][j-k*a[i]]) dp[i][j]=true;
else dp[i][j]=dp[i-1][j];
}
}
}
int s=0;
for(int i=1;i<=m;i++){
if(dp[n-1][i]) /*printf("%d ",i),*/s++;
}
printf("%d\n",s);
}
}
这么写的话会超时,虽然这个dp好理解,但是这个dp的复杂度为O(m(su[i]的和))
现在附上第二次AC代码:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int dp[2][100005];
int main(){
int n,m,va[102],su[102];
while(cin>>n>>m){
if(n==0&&m==0) return 0;
memset(dp,-1,sizeof(dp));
for(int i=0;i<n;i++)
scanf("%d",&va[i]); //硬币的面额
for(int i=0;i<n;i++)
scanf("%d",&su[i]); //面额为va[i]的硬币的数量
for(int i=0;i<=su[0]&&va[0]*i<=m;i++)
dp[0][va[0]*i]=su[0]-i;
for(int i=1;i<n;i++){
for(int j=0;j<=m;j++){
if(dp[(i-1)%2][j]>=0) dp[i%2][j]=su[i];
else if(j<va[i]||dp[i%2][j-va[i]]<=0) dp[i%2][j]=-1;
else dp[i%2][j]=dp[i%2][j-va[i]]-1;
}
}
int s=0;
for(int i=1;i<=m;i++)
if(dp[(n-1)%2][i]>=0) /*printf("%d ",i),*/s++;
printf("%d\n",s);
}
}
这个复杂度为O(nm),
dp[i][j]表示前i+1中硬币加和得到j元钱时,第i+1种硬币剩余的数量。

poj1742Coins(多重背包)的更多相关文章

  1. POJ1742Coins(多重背包)

    Coins Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 32309   Accepted: 10986 Descripti ...

  2. 洛谷P1782 旅行商的背包[多重背包]

    题目描述 小S坚信任何问题都可以在多项式时间内解决,于是他准备亲自去当一回旅行商.在出发之前,他购进了一些物品.这些物品共有n种,第i种体积为Vi,价值为Wi,共有Di件.他的背包体积是C.怎样装才能 ...

  3. HDU 2082 找单词 (多重背包)

    题意:假设有x1个字母A, x2个字母B,..... x26个字母Z,同时假设字母A的价值为1,字母B的价值为2,..... 字母Z的价值为26.那么,对于给定的字母,可以找到多少价值<=50的 ...

  4. Poj 1276 Cash Machine 多重背包

    Cash Machine Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 26172   Accepted: 9238 Des ...

  5. poj 1276 Cash Machine(多重背包)

    Cash Machine Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 33444   Accepted: 12106 De ...

  6. (混合背包 多重背包+完全背包)The Fewest Coins (poj 3260)

    http://poj.org/problem?id=3260   Description Farmer John has gone to town to buy some farm supplies. ...

  7. (多重背包+记录路径)Charlie's Change (poj 1787)

    http://poj.org/problem?id=1787   描述 Charlie is a driver of Advanced Cargo Movement, Ltd. Charlie dri ...

  8. 单调队列优化DP,多重背包

    单调队列优化DP:http://www.cnblogs.com/ka200812/archive/2012/07/11/2585950.html 单调队列优化多重背包:http://blog.csdn ...

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

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

  10. POJ1276Cash Machine[多重背包可行性]

    Cash Machine Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 32971   Accepted: 11950 De ...

随机推荐

  1. linux安装mysql(tar.gz)

    1. 查看卸载自带的mysql # rpm -qa|grep MySQLMySQL-X.X.X#rpm -e MySQL-X.X.X # rpm -qa|grep mariadb #有些版本还得查看卸 ...

  2. Codeforces 833B 题解(DP+线段树)

    题面 传送门:http://codeforces.com/problemset/problem/833/B B. The Bakery time limit per test2.5 seconds m ...

  3. Python自学第二天学习之《元组与字典》

    一.  元组:tuple类型,元组一级元素 不能修改 不能增加 不能删除,是有序的. 格式 :tu=(1,2,3,4,5,6) 1.类型转换: #字符串转换成元组 b=“123” c=tuple(b) ...

  4. keep-alive 被 beforeRouteEnter 骗了

    大家中秋假期快乐,假期分享一些实战文章给大家,原创不易,欢迎转发,一起学习 现在大家基本都在单页应用里面使用了 keep-alive 来缓存不活动的组件实例,而不是销毁它们. 如果你还没有使用,可以看 ...

  5. python字符串内置函数汇总

    1.capitalize 第一个单词首字母大写 2.title 每个单词首字母大写 3.upper 每个字母变大写 4.lower 每个字母变小写 5.len() 字符串长度 6.format() 格 ...

  6. python基础篇(完整版)

    目录 计算机基础之编程和计算机组成 什么是编程语言 什么是编程 为什么要编程 编程语言的分类 机器语言(低级语言) 汇编语言 高级语言 计算机的五大组成 CPU(相当于人类的大脑) 多核CPU(多个大 ...

  7. 详聊js中的原型/原型链

    先以一段简单的代码为例: function Dog(params){     this.name = param.name;     this.age = param.age;     this.ba ...

  8. RabbitMQ相关使用命令

    启动:rabbitmq-server -detached 停止:rabbitmqctl stop 状态:rabbitmqctl status 查看所有用户rabbitmqctl list_users ...

  9. runtime 理解笔记

    runtime 简称运行时,是系统运行的一种机制,在oc中通过c语言编写一个运行系统库.考进行一些非常底层的操作(oc无法完成的). 1.利用runtime,在程序运行过程中,动态创建一个类(比如KV ...

  10. 线程屏障CyclicBarrier实现原理

    生产环境中,存在需要等待多个线程都达到某种状态后,才继续运行的情景.并发工具CyclicBarrier就能够完成这种功能.本篇从源码方面,简要分析CyclicBarrier的实现原理. 使用示例 pu ...