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


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值