您的位置:首页 > 其它

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

2015-07-29 20:06 344 查看
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! )

[code]#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: