[HDU 1521] 排列组合 指数型母函数

http://acm.hdu.edu.cn/showproblem.php?pid=1521

Problem Description
有n种物品,并且知道每种物品的数量。要求从中选出m件物品的排列数。例如有两种物品A,B,并且数量都是1,从中选2件物品,则排列有”AB”,”BA”两种。

Input
每组输入数据有两行,第一行是二个数n,m(1<=m,n<=10),表示物品数,第二行有n个数,分别表示这n件物品的数量。

Output
对应每组数据输出排列数。(任何运算不会超出2^31的范围)

Sample Input
2 2
1 1

Sample Output
2

思路:排列问题可以用指数型的母函数求解

f(x)= ( 1 + x +x^2 / 2! + x^3 / 3! +…..+ x^n1 / n1! ) (1+ x +x^2/ 2!+ x^3 / 3! +…..+ x^n2 / n2! ) ……….( 1 + x +x^2 / 2! + x^3 / 3! +…..+ x^nk / nk! )

#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

const int fac[] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800};

int main()
{
    int n, m;
    int counts[12];
    double nex[12], temp[12];
    while(cin>>n>>m){
        for(int i = 0; i < n; i++){
            cin>>counts[i];
        }
        for(int i = 0; i <= 10; i++){
            nex[i] = 0.0;
            temp[i] = 0.0;
        }
        for(int i = 0; i <= counts[0]; i++){
            nex[i] = 1.0 / fac[i];
        }
        for(int i = 1; i < n; i++){
            for(int j = 0; j <= m; j++){
                for(int k = 0; k <= counts[i] && j+k <= m; k++){
                    temp[j+k] += nex[j]/fac[k];
                }
            }
            for(int j = 0; j <= m; j++){
                nex[j] = temp[j];
                temp[j] = 0;
            }
        }
        printf("%.0lf\n", nex[m]*fac[m]);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

achonor

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值