LightOj 1231 Coin Change (II)(完全背包)
来源:互联网 发布:手机arp扫描软件 编辑:程序博客网 时间:2024/05/17 09:36
Coin Change (II)
Description
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
2
3 5
1 2 5
4 20
1 2 3 4
Sample Output
Case 1: 4
Case 2: 108
解题思路:
完全背包。
状态转移方程:dp[i][j] = (dp[i][j]+dp[i-1][j-l*a[i]])%MOD。
优化以后就是: dp[j] = (dp[j]+dp[j-a[i]])%MOD 。
AC代码:
#include <iostream>#include <cstdio>#include <cstring>using namespace std;const int MOD = 100000007;const int maxn = 10005;int dp[maxn];int a[105],b[105];int main(){ int T,t = 1; scanf("%d",&T); while(T--){ int n,m; scanf("%d%d",&n,&m); for(int i = 1; i <= n; i++) scanf("%d",&a[i]); memset(dp,0,sizeof(dp)); dp[0] = 1; for(int i = 1; i <= n; i++){ for(int j = a[i]; j <= m; j++){ dp[j] = (dp[j]+dp[j-a[i]])%MOD; } } printf("Case %d: %d\n",t++,dp[m]); } return 0;}
- LightOj 1231 Coin Change (II)(完全背包)
- LightOJ 1232 - Coin Change (II) 【完全背包】
- LightOJ 1232 Coin Change (II)(完全背包)
- lightoj 1232 - Coin Change (II) 完全背包
- LightOj 1231 Coin Change (I)(部分背包)
- LightOJ 1231 Coin Change (I)(部分背包)
- uva 674 Coin Change(完全背包)
- uva674 - Coin Change(完全背包)
- UVA 674 - Coin Change(完全背包)
- UVA 674 Coin Change(完全背包)
- UVA 674 Coin Change(完全背包)
- UVA 674 Coin Change(完全背包)
- LightOJ 1231 Coin Change (I) (线性dp 背包计数)
- LightOJ 1231 Coin Change (I) (背包计数模板题)
- LightOJ 1231 Coin Change (I)
- UVA 674 Coin Change(dp + 完全背包)
- uva 674 Coin Change(类似完全背包)
- uva 674 && hdu 2069 coin change(水完全背包)
- 求逆元偷懒方法
- 永远不要打探别人工资
- 杭电4858
- 第四周项目5.4—编制递归函数返回第n个Fibnacci数
- POJ 1017 Packets (贪心)
- LightOj 1231 Coin Change (II)(完全背包)
- Alcatraz
- 二路归并排序
- JAVA实现网络爬虫
- leetcode118 & 119. [Array]Pascal's Triangle
- 第三周 项目4 (2)换分币
- 关于Java和C#的内部类的小记
- HD_1241Oil Deposits(DFS)
- https原理及tomcat配置https方法