UVA - 10105 Polynomial Coefficients

题目大意:给出n和m,再该出m个数值n1 ~ nm, 保证n1 + ... + nm = n,现在有算式(a1 + a2 + ... + am) ^ n, 求展开项中a1^n1 + a2^n2 +...+ am^nm这项的系数。


解题思路:(a + b)^n的系数为C(i, n), 那么对于算式(a1 + a2 + ... + am)^n可以理解成(X + am)^n,类似于递归的操作。

#include <cstdio>

int main() {
	int n, k, C[20][20] = {0};
	for (int i = 0; i < 15; i++)
		C[i][0] = 1;

	for (int i = 1; i < 15; i++)
		for (int j = 1; j <= i; j++)
			C[i][j] = C[i-1][j] + C[i-1][j-1];

	while (scanf("%d%d", &n, &k) != EOF) {
		int ans = 1, temp;
		for (int i = 0; i < k; i++) {
			scanf("%d", &temp);
			ans *= C[n][temp];
			n -= temp;
		}
		printf("%d\n", ans);
	}
	return 0;
}





你可能感兴趣的:(UVA - 10105 Polynomial Coefficients)