首页 > 代码库 > UVA10105 - Polynomial Coefficients(排列组合)

UVA10105 - Polynomial Coefficients(排列组合)

UVA10105 - Polynomial Coefficients(排列组合)

题目链接

题目大意:给你k个数,然后求(x1 + x2 + x3 +.. + xk)^n的x1^n1x2^n2x3^n3...xk^nk这个数的系数。题目会给n和k,然后给出k个ni,并且保证n1+ n2 + ..+nk = n.

解题思路:根据二项式的系数的求法,类似的也用组合来求这个数的系数。对于这个x1^n1..xk^nk这个数的系数,就等于每个单独的xi^ni的系数的积。容易得出这个数的系数是
n!/(n1!?(n - n1)!) ? (n - n1)! / (n2! ? (n - n1 - n2)!) ? ... ? nk!/(nk!?(n - n1 - n2 -..-nk)!),化简后得n!/ (n1!?n2!?n3!?..?nk!).

代码:

#include <cstdio>
#include <cstring>

typedef long long ll;

const int maxn = 13;
ll c[maxn];

void init () {

    c[0] = 1;
    for (int i = 1; i < maxn; i++)
        c[i] = c[i - 1] * i;
}

int main () {

    int N, K, num;
    init();

    while (scanf ("%d%d", &N, &K) != EOF) {

        ll ans = c[N];
        for (int i = 0; i < K; i++) {
            scanf ("%d", &num);
            ans /= c[num];
            N -= num;
        }

        printf ("%lld\n", ans);
    }
    return 0;
}

UVA10105 - Polynomial Coefficients(排列组合)