LIST has a list (母函数)

LIST has a list

Problem Description

A famous ACMer named LIST.He is very rich,so he has infinite coins of some par value(面值).One day he has a list of things he want

to buy.And he is so rich,he can buy a things for many ways.for example,if he want to spend 5 yuan to buy a things,he will give the seller five coins (1,1,1,1,1) or three coins (2,2,1) or two coins (3,2) and so on.Now he wants to ask you to help him calculate how many ways he can pay m yuan.

Input

In the first line is a number T means the amount of cases(T<=50).And in each case,the first line has two numbers ,n(the amount of the par value, n<=30) and m(the sum of money LIST has to spend, m<=150).

In the next line has n numbers mean the different par value(each par value is no more than 35).

Output

Each case output one line,like"Case #i: Ai"(i is the number of case and Ai is the number of ways in this case).

Ai might be big,but it can't be very big.

Sample Input

1

5 13

1 2 3 5 10

Sample Output

Case #1: 37

//题意:输入n,m,接着输入n个数

表示给你n张钞票,每张钞票的面值不同,问现在要用这n张钞票组合成m,问总共有几种组合方法

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long LL;
const int MAXN = 300;
LL a[MAXN], b[MAXN];
int num[MAXN];
int main(){
	int T, n, m, kase = 0;
	scanf("%d", &T);
	while(T--){
		scanf("%d%d", &n, &m);
		for(int i = 0; i < n; i++){
			scanf("%d", num + i);
		}
		memset(a, 0, sizeof(a));
		memset(b, 0, sizeof(b));
		for(int i = 0; i <= m; i += num[0])
			a[i] = 1, b[i] = 0;
		for(int i = 1; i < n; i++){
			for(int j = 0; j <= m; j++){
				for(int k = 0; j + k * num[i] <= m; k++){
					if(j + k * num[i] <= m){
						b[j + k * num[i]] += a[j];
					}
				}
			}
			for(int j = 0; j <= m; j++)a[j] = b[j], b[j] = 0;
		}
		printf("Case #%d: %lld\n", ++kase, a[m]);
	}
	return 0;
}


 

你可能感兴趣的:(LIST has a list (母函数))