1232 - Coin Change (II)

来源:互联网 发布:淘宝任务发布平台 编辑:程序博客网 时间:2024/06/05 18:03
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 valueA1, A2 ... An. You have to find thenumber of ways you can makeK 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) andK (1 ≤ K ≤ 10000). The nextline contains n integers, denotingA1, A2 ... An(1 ≤ Ai ≤ 500). AllAi 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 modulo100000007.

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        :xdloveCreated 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;}



0 0