您的位置:首页 > 其它

LightOJ - 1232 - Coin Change (II)

2014-03-04 23:41 344 查看
先上题目:

1232 - Coin Change (II)

PDF (English)StatisticsForum
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[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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: