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.

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

题意:你有n种不同面值的硬币,要用这些硬币来组合成K,每种硬币不得使用超过k次(怎么可能超过k次)

解析:先来看第一种方法:

  假设我们用a[i]来表示第i种硬币的面额,用dp[i][j]来表示,在选用前i种硬币的情况下,有多少种组成j的方案。

  那么可以有递推式:dp[i][j]=dp[i-1][j-a[i]]+dp[i-1][j-2*a[i]].......dp[i-1][j-k*a[i]];

  那么我们可以尝试着去简化一下这个式子,虽然为了做出这道题,没有必要去简化,但是为了把我的代码贴出来,还是需要简化一下的。假如我们使用dp[i]来表示用当前的硬币来组成i有多少种方案,那么我们知道,dp[i]=dp[i-a[j]](j=1~n)但是,这样写递推式,会导致112和121被视为两种情况,那么我们应该如何解决这样的问题呢,答案是,我们需要将硬币一个个地引入,然后每引入一个新的硬币,就去刷新一遍dp数组,下面的话看一下代码应该就可以理解了。

  

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
int n,k;
int a[10000];
int dp[200000];
int main(){
	int T;
	cin>>T;
	int cnt=0;
	while(T--){
		memset(a,0,sizeof(a));
		memset(dp,0,sizeof(dp));
		cin>>n>>k;
		for(int i=1;i<=n;i++){
			scanf("%d",&a[i]);
		}
		dp[0]=1;
		sort(a+1,a+1+n);
		for(int cass=1;cass<=n;cass++){
			int curr=a[cass];
			if(a[cass]!=a[cass-1]){
				for(int i=curr;i<=k;i++){
					if(curr<=i){
						dp[i]+=dp[i-curr];
						dp[i]%=100000007;
					}
				}
			}
		}
		printf("Case %d: %d\n",++cnt,dp[k]);
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值