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 ...
随机推荐
- codeforces 931E Logical Expression dp
time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standa ...
- B1257 [CQOI2007]余数之和 数学,分块
这个题想明白之后很好做,但是不好想.我根本没想出来,上网看了一下才知道怎么做... 这个题其实得数是一个等差数列,然后一点点求和就行了. 上次NOIP就是没看出来规律,这次又是,下次先打表找规律!!! ...
- bzoj 3743 [ Coci 2015 ] Kamp —— 树形DP
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3743 一开始想到了树形DP,处理一下子树中的最小值,向上的最小值,以及子树中的最长路和向上的 ...
- SpringAOP使用注解实现5种通知类型
spring aop的5种通知类型都有 Before前置通知 AfterReturning后置通知 Around环绕通知 AfterThrowing异常通知 After最终通知 首先创建接口和实现类 ...
- Aspnet_Session
cmd: aspnet_regsql.exe -ssadd -sstype c -d ZZCasSession -S 192.168.0.3 -U sa -P szhweb2010 <!--会话 ...
- django模型层(二)
多表操作 创建模型 实例:我们来假定下面这些概念,字段和关系 作者模型:一个作者有姓名和年龄. 作者详细模型:把作者的详情放到详情表,包含生日,手机号,家庭住址等信息.作者详情模型和作者模型之间是一对 ...
- Leetcode0092 & 0206--Reverse Linked List 链表逆转
[转载请注明]http://www.cnblogs.com/igoslly/p/8670038.html 链表逆序在链表题目中还是较为常见的,这里将Leetcode中的两道题放在一起,分别是 0092 ...
- 【SQL】约束
1. 添加约束 1)使用ALTER TABLE语句 •添加或删除约束,不会修改其结构 •启用和禁用约束 •通过使用MODIFY子句添加NOTNULL约束 ALTER TABLE <table_n ...
- C#—接口和抽象类的区别?
一.接口 接口是指对协定进行定义的引用类型,其他类型实现接口,以保证它们支持某些操作.接口指定必须由类提供的成员或实现它的其他接口.与类相似,接口可以包含方法.属性.索引器和事件作为成员. 1.接口存 ...
- APICloud 获取缓存以及清除缓存(常用第三方方法)
一.app中经常会有缓存的清除这个操作,具体如下 1.获取缓存大小 apiready = function() { api.getCacheSize(function(ret, err) { //si ...