LightOJ - 1232 - Coin Change (II)
先上题目:
Time Limit: 1 second(s) | Memory Limit: 32 MB |
In a strange shop there are n types of coins of value A1, A2 ... An. You have to find the number of ways you can make K using the coins. You can use any coin at most K times.
For example, suppose there are three coins 1, 2, 5. Then if K = 5 the possible ways are:
11111
1112
122
5
So, 5 can be made in 4 ways.
Input
Input starts with an integer T (≤ 100), denoting the number of test cases.
Each case starts with a line containing two integers n (1 ≤ n ≤ 100) and K (1 ≤ K ≤ 10000). The next line contains n integers, denoting A1, A2 ... An (1 ≤ Ai ≤ 500). All Ai will be distinct.
Output
For each case, print the case number and the number of ways K can be made. Result can be large, so, print the result modulo 100000007.
Sample Input |
Output for Sample Input |
2 3 5 1 2 5 4 20 1 2 3 4 |
Case 1: 4 Case 2: 108 |
题意:有n种硬币,各有其价值,给你一个数k,问你有多少种方法可以用这n种硬币凑出k,其中每种硬币最多可以用k个。
根据题意分析,可以发现这是一条完全背包。
分析:对于每一种的硬币可以用k个,如果硬币的价值最小(等于1),最多可以用k个,那就说明对于每一个物品在这里都是刚好可以放满体积为k的背包或者有剩余(或者说溢出,总之就是数量是过量的意思),这样我们就可以将它看成是一个完全背包了。
状态转移方程:dp[i][j]=(dp[i][j]%MOD+dp[i-1][j-l*a[i]]%MOD)%MOD
优化以后就是dp[i]=(dp[i]%MOD+dp[i-[i]]%MOD)%MOD (注意这里的i和上面的i意思不一样,上面的i代表第i中硬币,这里的i代表背包体积)。
上代码:
#include <cstdio>
#include <cstring>
#define MAX 10002
#define MOD 100000007
using namespace std; int a[];
int dp[MAX]; int main()
{
int t,n,k;
//freopen("data.txt","r",stdin);
scanf("%d",&t);
for(int cal=;cal<=t;cal++){
memset(dp,,sizeof(dp));
scanf("%d %d",&n,&k);
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
}
dp[]=;
for(int i=;i<=n;i++){
for(int j=a[i];j<=k;j++){
dp[j]=(dp[j]+dp[j-a[i]])%MOD;
}
}
printf("Case %d: %d\n",cal,dp[k]);
}
return ;
}
1232
LightOJ - 1232 - Coin Change (II)的更多相关文章
- Coin Change (II)(完全背包)
Coin Change (II) Time Limit: 1000MS Mem ...
- Lightoj 1231 - Coin Change (I) (裸裸的多重背包)
题目链接: Lightoj 1231 - Coin Change (I) 题目描述: 就是有n种硬币,每种硬币有两个属性(价值,数目).问用给定的硬币组成K面值,有多少种方案? 解题思路: 赤果果的 ...
- LightOJ - 1231 - Coin Change (I)
先上题目: 1231 - Coin Change (I) PDF (English) Statistics Forum Time Limit: 1 second(s) Memory Limit: ...
- LightOJ 1235 - Coin Change (IV) (折半枚举)
题目链接: http://www.lightoj.com/volume_showproblem.php?problem=1235 题目描述: 给出n个硬币,每种硬币最多使用两次,问能否组成K面值? 解 ...
- Lightoj 1235 - Coin Change (IV) 【二分】
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1235 题意: 有N个硬币(N<=18).问是否能在每一个硬币使用不超过两 ...
- C - Coin Change (III)(多重背包 二进制优化)
C - Coin Change (III) Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu ...
- [LeetCode] Coin Change 硬币找零
You are given coins of different denominations and a total amount of money amount. Write a function ...
- HDOJ 2069 Coin Change(母函数)
Coin Change Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDU 2069 Coin Change
Coin Change Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...
随机推荐
- 10.3arcmap矢量数据制作步骤
注意:在制作之前需要点将图放到原本大小.并且保存一下不然容易造成数据丢失. 在数据制作之前需要新建一个页面,用来放需要处理的原始数据 勾选ArcMap中Customize——ArcMap Option ...
- 修改input:file样式
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- 麦森数--NOIP2003
题目描述 形如2P−12^{P}-12P−1 的素数称为麦森数,这时PPP 一定也是个素数.但反过来不一定,即如果PPP 是个素数,2P−12^{P}-12P−1 不一定也是素数.到1998年底,人们 ...
- 基于Spark ML的Titanic Challenge (Top 6%)
下面代码按照之前参加Kaggle的python代码改写,只完成了模型的训练过程,还需要对test集的数据进行转换和对test集进行预测. scala 2.11.12 spark 2.2.2 packa ...
- C语言程序判断文件夹是否存在
#include <stdio.h> #include <io.h> int main(void){ if ( !access("C://Users/hui" ...
- 机器学习——Day 2 简单线性回归
写在开头 由于某些原因开始了机器学习,为了更好的理解和深入的思考(记录)所以开始写博客. 学习教程来源于github的Avik-Jain的100-Days-Of-MLCode 英文版:https:// ...
- RabbitMQ .NET消息队列使用入门(二)【多个队列间消息传输】
孤独将会是人生中遇见的最大困难. 实体类: DocumentType.cs public enum DocumentType { //日志 Journal = 1, //论文 Thesis = 2, ...
- Spring 整合 Redis (零配置) 的简单使用
pom.xml <!--jedis--> <dependency> <groupId>redis.clients</groupId> <artif ...
- spring 九种设计模式
spring中常用的设计模式达到九种,我们举例说明: 第一种:简单工厂 又叫做静态工厂方法(StaticFactory Method)模式,但不属于23种GOF设计模式之一. 简单工厂模式的实质是由一 ...
- CSS元素水平垂直居中的方法
1. 元素水平居中 1.1 设置父元素的属性 text-align: center; 说明:此属性只针对父元素的子元素为内联元素时有效,比如:img,input,select,button等(行内 ...