题目描述
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 starts with an integer T (T≤20), denoting the number of test cases.
Each case starts with a line containing two integers n (1≤n≤100) and K (1≤K≤100 000). The next line contains n integers, denoting A1,A2,…,An (1≤Ai≤100 000). All Ai will be distinct.
输出格式
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 100 000 007.
样例
input
2 3 5 1 2 5 4 20 1 2 3 4output
Case 1: 4 Case 2: 108
思路:每种硬币最多可以用K个其实是个幌子,因为当硬币面值大于1的时候,每个硬币肯定用不到K个,因此转化为硬币无限的背包问题,用一维dp可以解决。
代码实现:
#include <iostream>
#include <cstring>
#define MAX 100000007
using namespace std;
int main()
{
int cas;
cin>>cas;
for(int t=1; t<=cas; ++t){
int n, K;
int coin[101];
int dp[100001] = {0};
dp[0] = 1;
cin>>n>>K;
for(int i=0; i<n; ++i)
cin>>coin[i];
for(int i=0; i<n; ++i)//每层循环表示使用前i种硬币凑出面额j
for(int j=coin[i]; j<=K; ++j)
dp[j] = (dp[j] + dp[j-coin[i]]) % MAX;//dp[j]累计了使用前i-1种硬币凑成面额j的方案数
printf("Case %d: %d\n", t, dp[K]);
}
return 0;
}