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. 冒泡排序(Bubble Sort),比较次数优化改进

    body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...

  2. 【DevExpress v17.2新功能预告】WinForms上的图表增强

    在WinForms Charts v17.2中,我们新增了一些有用的功能,开发人员和最终用户可能都会喜欢. 基于标准的过滤 Chart控件已经支持一系列的过滤,但是在这个版本中,我们用FilterCr ...

  3. 根据redis的pub/sub机制,写一个即时在线聊天应用

    在Redis中,有个Pub/Sub,他的主要的工作流程如: redis订阅一个模式频道如:chat_*,然后由小a想找人聊天了,就发送一个消息“现在有人聊天吗?chat_a”,末尾的chat_a为标识 ...

  4. STL标准库-迭代器

    技术在于交流.沟通,本文为博主原创文章转载请注明出处并保持作品的完整性 本节主要介绍STL六大部件中的Iterators迭代器. 在语言方面讲,容器是一个class template, 算法是一个仿函 ...

  5. grafana查询中的变量templating

    有时我们在管理成百上千台机器的时候,配置grafana无疑是明智的,因为你不需要一个一个的把每个机器的图形都配置一遍,利用templating就可以瞬间实现n台机器的状态显示了. templating ...

  6. CODE大全大量Flash网站收藏

    我的博客:CODE大全:www.codedq.net:业余草:www.xttblog.com:爱分享:www.ndislwf.com或ifxvn.com. http://www.wallop.com在 ...

  7. IOS 获取中英文字符串长度

    //得到中英文混合字符串长度 方法1 - (int)convertToInt:(NSString*)strtemp { int strlength = 0; char* p = (char*)[str ...

  8. Android manifest 获取源代码

    /********************************************************************************* * Android manifes ...

  9. CodeForces - 285E: Positions in Permutations(DP+组合数+容斥)

    Permutation p is an ordered set of integers p1,  p2,  ...,  pn, consisting of n distinct positive in ...

  10. PR5

    修改字幕的两种方式