Coin Change (II)(完全背包)

                                                           Coin Change (II)
Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %lld & %llu

[]   [Go Back]   [Status]  

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

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <string.h>
 4 using namespace std;
 5 #define mod 100000007
 6 int a[11000],c[10000],dp[11000];
 7 int main()
 8 {
 9     int n,k,t,i,j,r,w;
10     scanf("%d",&t);
11     for(i=1; i<=t; i++)
12     {
13         memset(dp,0,sizeof(dp));
14         scanf("%d%d",&n,&k);
15         for(j=1; j<=n; j++)scanf("%d",&a[j]);
16         dp[0]=1;
17         for(j=1; j<=n; j++)
18         {
19             for(r=0; r<=k; r++)
20             {
21                 if(r+a[j]<=k)
22                 {
23                     dp[r+a[j]]+=dp[r],dp[r+a[j]]%=mod;
24                 }
25             }
26         }
27         cout<<"Case "<<i<<": ";
28         cout<<dp[k]<<endl;
29     }
30 }
View Code
原文地址:https://www.cnblogs.com/ERKE/p/3582216.html