• hdu1521 排列组合 指数型母函数模板题


    排列组合

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 4891    Accepted Submission(s): 2122


    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
     
    Author
    xhd
     
    Recommend
    xhd   |   We have carefully selected several similar problems for you:  1085 1171 1398 2152 2110 
     
    从题目中可以看出来,取m件物品可能取到重复的物品,所以这是一个指数型母函数问题
    接下来就是套用母函数模板了
    #include <map>
    #include <set>
    #include <stack>
    #include <cmath>
    #include <queue>
    #include <cstdio>
    #include <vector>
    #include <string>
    #include <cstring>
    #include <iterator>
    #include <iostream>
    #include <algorithm>
    #define debug(a) cout << #a << " " << a << endl
    using namespace std;
    const int maxn = 2*1e2 + 10;
    const int mod = 10000;
    typedef long long ll;
    ll f( ll x ) {
        ll sum = 1;
        for( ll i = 1; i <= x; i ++ ) {
            sum *= i;
        }
        return sum;
    }
    int main() {
        ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
        ll n, m, num[maxn];
        double a[maxn], b[maxn];
        while( cin >> n >> m ) {
            memset( a, 0, sizeof(a) );
            memset( b, 0, sizeof(b) );
            for( ll i = 0; i < n; i ++ ) {
                cin >> num[i];
            }
            for( ll i = 0; i <= num[0]; i ++ ) {
                a[i] = 1.0/f(i);
            }
            for( ll i = 1; i < n; i ++ ) {
                for( ll j = 0; j <= n; j ++ ) {
                    for( ll k = 0; k <= num[i] && k+j <= n; k ++ ) {
                        b[k+j] += a[j]/f(k);
                    }
                }
                for( ll j = 0; j <= n; j ++ ) {
                    a[j] = b[j], b[j] = 0;
                }
            }
            printf("%.lf
    ",a[m]*f(m) );
        }
        return 0;
    }
    

      

    彼时当年少,莫负好时光。
  • 相关阅读:
    sourceinsight问题
    mysql函数调用过程
    visual studio 中sstrcpy报错的问题
    mysql基本操作
    c/c++程序连接mysql
    mysql 在visual studio中的配置
    va_start
    c do{}while(0)
    .NET 通用权限设计
    https://zhidao.baidu.com/question/362784520674844572.html
  • 原文地址:https://www.cnblogs.com/l609929321/p/9323701.html
Copyright © 2020-2023  润新知