Coins

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 91   Accepted Submission(s) : 36

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

Problem Description

Whuacmers use coins.They have coins of value A1,A2,A3...An Silverland dollar. One day Hibix opened purse 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

Source

2009 Multi-University Training Contest 3 - Host by WHU 
 
       题目的意思是在1到m,有多少面值可以被已有的硬币凑成。
      比如例2,拥有2种硬币,1面值的2个,4面值的1个,在1到5内可以凑的有1,2,4,5共4个数字,所以输出4。
      思路是用前一状态推后一个状态,比如要凑j(j<=m),如果j-v[i]可以被凑成,那么加上当前的v[i],j也是可以凑成,但是要注意当前凑j所用的 当前硬币个数为  凑j-v[i]的硬币个数+1。凑j所用的 当前硬币个数<=c[i]
 
#include <iostream>
#include <cstring>
#include <algorithm>
#include <string>
using namespace std;
int main()
{
int n,m;
int v[],c[];
int dp[];
int cnt[];//记录当前已用的硬币个数
while(cin>>n>>m)
{
if(m==&&n==) break;
int i,j,k;
memset(dp,,sizeof(dp));
dp[]=;//因为,比如,硬币面值为2,此时dp[2]!=0&&dp[2-2]==1
for(i=;i<=n;i++)
{
cin>>v[i];
}
for(i=;i<=n;i++)
{
cin>>c[i];
}
int cnts=;
for(i=;i<=n;i++)//每一种硬币遍历
{
memset(cnt,,sizeof(cnt));//因为只针对当前硬币
for(j=v[i];j<=m;j++)
{
if(dp[j]!=&&dp[j-v[i]]==&&cnt[j-v[i]]<=c[i]-)
{//价值j还没能被凑成,但j-v[i]可以凑成,且当前所用硬币个数+1<=c[i]
dp[j]=;
cnt[j]=cnt[j-v[i]]+;//在凑成j-v[i]的基础上多用了一个当前的硬币
//如果凑成j-v[i]用了1个当前硬币,j就要用2个
cnts++;//种数++
}
}
}
cout<<cnts<<endl;
}
return ;
}

DP Coins hdoj的更多相关文章

  1. DP:Coins(POJ 1742)

      用硬币换钱 题目大意:就是有面值为A1,A2,A3....的硬币,各有C1,C2,C3...的数量,问在钱数为m的范围内,能换多少钱?(不找零) 这题看名字就知道是完全背包,但是这题又有点不一样, ...

  2. 二分+DP+Trie HDOJ 5715 XOR 游戏

    题目链接 XOR 游戏 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  3. 【HDOJ】【4336】Card Collector

    概率DP/数学期望/状压DP/容斥原理 kuangbin总结中的第14题 好神奇的做法……题解看kuangbin的代码好了…… //HDOJ 4336 #include<cstdio> # ...

  4. LeetCode OJ 322. Coin Change DP求解

    题目链接:https://leetcode.com/problems/coin-change/ 322. Coin Change My Submissions Question Total Accep ...

  5. [LeetCode] Coin Change 2 硬币找零之二

    You are given coins of different denominations and a total amount of money. Write a function to comp ...

  6. [Swift]LeetCode518. 零钱兑换 II | Coin Change 2

    You are given coins of different denominations and a total amount of money. Write a function to comp ...

  7. [LeetCode] 518. Coin Change 2 硬币找零之二

    You are given coins of different denominations and a total amount of money. Write a function to comp ...

  8. LeetCode之找零钱

    题目:已知不同面值的钞票,求如 何用最少数量的钞票组成某个金额,求可 以使用的最少钞票数量.如果任意数量的已知面值钞票都无法组成该金额, 则返回-1. 示例: Input: coins = [1, 2 ...

  9. LeetCode.518 零钱兑换Ⅱ(记录)

    518题是背包问题的变体,也称完全背包问题. 解法参考了该篇文章,然后对自己困惑的地方进行记录. 下面是该题的描述: 有一个背包,最大容量为 amount,有一系列物品 coins,每个物品的重量为 ...

随机推荐

  1. oracle 统计sql

    最近在研究项目时发现如下sql, select 3 agentOfGCount, 0 workingCount, 0 restingCount, 0 busyingCount, 0 connectin ...

  2. tomcat conf目录下文件的作用

    本文以apache-tomcat-6.0.26为准介绍 1.   web.xml: 默认(欢迎)文件的设置  配置包括:主要是配置servlet,包括初始化参数,设置session等 <serv ...

  3. php 无限极分类问题

  4. UIWebView和UIWebViewDelegate的基本用法 (转)

    一.UIWebView主要有三种方法实现页面的装载,分别是: 1. (void)loadRequest:(NSURLRequest *)request;  (直接装载URL) 2. (void)loa ...

  5. 冷知识:excel 2013有多少行列

    XFD1048576 列:24*26*26+6*26+4=16384 行:2^20=1048576

  6. AR.Drone 2.0四轴飞机体验:最好的玩具航拍器

    http://digi.tech.qq.com/a/20140513/007458.htm?pgv_ref=aio2012&ptlang=2052 AR.Drone 2.0四轴飞机体验:最好的 ...

  7. kindle看扫描版pdf的解决办法

    (1)先把PDF crop (2)转JPEG (3)JPEG根据kindle的屏幕的分辨率,调整JPEG图像大小,kindle whater paper 大小为1448*1072 (4)   最后把j ...

  8. CF1093:E. Intersection of Permutations(树状数组套主席树)

    题意:给定长度为N的a数组,和b数组,a和b都是1到N的排列: 有两种操作,一种是询问[L1,R1],[L2,R2]:即问a数组的[L1,R1]区间和b数组的[L2,R2]区间出现了多少个相同的数字. ...

  9. 移动端 元素外面使用伪类after加边框 导致其内部元素无法选中

    解决方法:给内部元素增加属性 position: relative; z-index: 3; 这样就能选中其内部元素了.

  10. 在 Laravel 5 中使用 Repository 模式实现业务逻辑和数据访问的分离

    1.概述 首先需要声明的是设计模式和使用的框架以及语言是无关的,关键是要理解设计模式背后的原则,这样才能不管你用的是什么技术,都能够在实践中实现相应的设计模式. 按照最初提出者的介绍,Reposito ...