LightOJ - 1232 - Coin Change (II)
2014-03-04 23:41
344 查看
先上题目:
1232 - Coin Change (II)
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.
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.
题意:有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代表背包体积)。
上代码:
1232
1232 - Coin Change (II)
PDF (English) | Statistics | Forum |
Time Limit: 1 second(s) | Memory Limit: 32 MB |
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 |
根据题意分析,可以发现这是一条完全背包。
分析:对于每一种的硬币可以用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[102]; int dp[MAX]; int main() { int t,n,k; //freopen("data.txt","r",stdin); scanf("%d",&t); for(int cal=1;cal<=t;cal++){ memset(dp,0,sizeof(dp)); scanf("%d %d",&n,&k); for(int i=1;i<=n;i++){ scanf("%d",&a[i]); } dp[0]=1; for(int i=1;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 0; }
1232
相关文章推荐
- 1232 - Coin Change (II) Lightoj 转移为等差
- lightoj 1232 - Coin Change (II) 完全背包
- LightOJ 1232 Coin Change (II)(完全背包)
- LightOJ 1232 - Coin Change (II) 【完全背包】
- LightOj 1231 Coin Change (II)(完全背包)
- 1232 - Coin Change (II)
- Lightoj 1235 - Coin Change (IV) 【二分】
- LightOJ-1395 A Dangerous Maze (II) (期望dp)
- lightoj 1235 Coin Change (IV)(折半枚举)
- A Dangerous Maze (II) LightOJ - 1395(概率dp)
- lightoj 1363 - Binary Matrix (II) 贪心
- lightoj 1257 - Farthest Nodes in a Tree (II)(树的直径升级版)
- LightOJ1245 Harmonic Number (II)
- lightoj 1429 - Assassin`s Creed (II) 【BFS预处理传递闭包 + SCC缩点 + DAG最小路径覆盖】
- LightOJ - 1217 Neighbor House (II)(dp)
- LightOJ 1245 Harmonic Number (II) <思维---数学>
- Lightoj 1235 - Coin Change (IV) 【二分】
- LightOJ 1245 Harmonic Number (II)
- lightoj 1235 Coin Change (IV)
- LightOJ 1245 - Harmonic Number (II) (求n/1+n/2+n/2+....+n/n)