10105 - Polynomial Coefficients

Time limit: 3.000 seconds

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=115&page=show_problem&problem=1046

The Problem

The problem is to calculate the coefficients in expansion of polynomial(x1+x2+...+xk)n.

The Input

The input will consist of a set of pairs of lines. The first line of the pair consists of two integers n and k separated with space (0<K,N<13). This integers define the power of the polynomial and the amount of the variables. The second line in each pair consists of k non-negative integers n1, ..., nk, where n1+...+nk=n.

The Output

For each input pair of lines the output line should consist one integer, the coefficient by the monomial x1n1x2n2...xknk in expansion of the polynomial(x1+x2+...+xk)n.

Sample Input


2 2
1 1
2 12
1 0 0 0 0 0 0 0 0 0 1 0


Sample Output


2 2



题意&思路:参考Richard A.Brualdi的《组合数学(第5版)》P88 (5.4 多项式定理)


完整代码:

非打表:

/*0.016s*/

#include <cstdio>
using namespace std;

int main()
{
	int n, k, ni;
	while (~scanf("%d%d", &n, &k))
	{
		long long sum = 1;
		for (int i = 2; i <= n; i++)
			sum *= i;
		while (k--)
		{
			scanf("%d", &ni);
			for (int i = 2; i <= ni; i++)
				sum /= i;
		}
		printf("%lld\n", sum);
	}
	return 0;
}


打表:

/*0.015s*/

#include <cstdio>
using namespace std;
const long long f[11] = {6, 24, 120, 720, 5040, 40320, 362880, 3628800, 39916800, 479001600, 6227020800};

int main()
{
	int n, k, ni;
	while (~scanf("%d%d", &n, &k))
	{
		long long sum = 1;
		if (n == 2)
			sum <<= 1;
		else if (n > 2)
			sum *= f[n - 3];
		while (k--)
		{
			scanf("%d", &ni);
			if (ni == 2)
				sum >>= 1;
			else if (ni > 2)
				sum /= f[ni - 3];
		}
		printf("%lld\n", sum);
	}
	return 0;
}