1232 - Coin Change (II)

1232 - Coin Change (II)
Time Limit: 1 second(s)Memory Limit: 32 MB

In a strange shop there are n types of coins of valueA1, A2 ... An. You have to find thenumber of ways you can make K using the coins. You can use any coin atmost 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 nextline contains n integers, denoting A1, A2 ... An(1 ≤ Ai ≤ 500). All Ai will bedistinct.

Output

For each case, print the case number and the number of ways Kcan 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

Output for Sample Input

Case 1: 4

Case 2: 108


题意: 给出n种硬币的币值,每种硬币最多有k个,问用这n种硬币组成k的方案

题解: 咋一看,多重背包计数问题,但是稍微想下就会明白,其实它是一个完全背包. 所以接下来就是裸的完全背包计数问题了

         对于完全背包问题,我们可以转化为01背包问题求解,首先对于某一种硬币来说,最多可以用k/a[i]个硬币,当我们把这看成

         是不同的物品时,就自然转化为01背包问题,但是这样的转化复杂度略高,于是可以利用二进制的性质,枚举.

AC代码:

/* ***********************************************
Author        :xdlove
Created Time  :2015年10月31日 星期六 18时25分02秒
File Name     :DP/1232_Coin_Change_II.cpp
************************************************ */

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <memory.h>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>

using namespace std;


#define REP_ab(i,a,b) for(int i = a; i <= b; i++)
#define REP(i, n) for(int i = 0; i < n; i++)
#define REP_1(i,n) for(int i = 1; i <= n; i++)
#define DEP(i,n) for(int i = n - 1; i >= 0; i--)
#define DEP_N(i,n) for(int i = n; i >= 1; i--)
#define CPY(A,B) memcpy(A,B,sizeof(B))
#define MEM(A) memset(A,0,sizeof(A))
#define MEM_1(A) memset(A,-1,sizeof(A))
#define MEM_INF(A) memset(A,0x3f,sizeof(A))
#define MEM_INFLL(A) memset(A,0x3f3f,sizeof(A))
#define mid (((l + r) >> 1))
#define lson l, mid, u << 1
#define rson mid + 1, r, u << 1 | 1
#define ls (u << 1)
#define rs (u << 1 | 1)


typedef long long ll;
typedef unsigned long long ull;
const int INF = 0x3f3f3f3f;
const ll INFLL = 0x3f3f3f3f3f3f3f3f;
const int MAXN = 1e4 + 5;
const int MAXM = MAXN;
const int mod = 1e8 + 7;

int dp[MAXN];
int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int T,n,x,k,cnt = 0;
    cin>>T;
    while(T--)
    {
        MEM(dp);
        scanf("%d %d",&n,&k);
        dp[0] = 1;
        for(int o = 0; o < n; o++)
        {
            scanf("%d",&x);
            for(int i = x; i <= k; i++)
                dp[i] = (dp[i] + dp[i - x]) % mod;
        }
        printf("Case %d: %d\n",++cnt,dp[k]);
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值