uva 10105 - Polynomial Coefficients(多项式系数)

题目连接: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 <stdio.h>
#include <string.h>

const int N = 15;
int c[N][N];

void init() {
	for (int i = 0; i < N; i++) c[i][0] = c[i][i] = 1;

	for (int i = 1; i < N; i++)
		for (int j = 1; j < i; j++)
			c[i][j] = c[i - 1][j] + c[i - 1][j - 1];
}

int main () {
	init();
	int n, m, x;
	while (scanf("%d%d", &n, &m) == 2) {
		int ans = 1;
		for (int i = 0; i < m; i++) {
			scanf("%d", &x);
			ans *= c[n][x];
			n -= x;
		}
		printf("%d\n", ans);
	}
	return 0;
}


你可能感兴趣的:(uva 10105 - Polynomial Coefficients(多项式系数))