bzoj 4710 [Jsoi2011]分特产 组合数学+容斥原理

题面

题目传送门

解法

考虑容斥原理

显然,我们可以枚举有多少个人没有收到

然后就转化成一个组合问题了

假设现在有\(x\)个物品,\(n\)个人,可以有人没有被分到,那么分给这\(n\)个人的方案数为\(n+x-1\choose n-1\)

然后就是分别计算一下就可以了

时间复杂度:\(O(nm)\)

代码

#include <bits/stdc++.h>
#define Mod 1000000007
#define N 2010
using namespace std;
template <typename node> void chkmax(node &x, node y) {x = max(x, y);}
template <typename node> void chkmin(node &x, node y) {x = min(x, y);}
template <typename node> void read(node &x) {
	x = 0; int f = 1; char c = getchar();
	while (!isdigit(c)) {if (c == '-') f = -1; c = getchar();}
	while (isdigit(c)) x = x * 10 + c - '0', c = getchar(); x *= f;
}
int a[N], c[N][N];
int main() {
	int n, m; read(n), read(m);
	for (int i = 1; i <= m; i++) read(a[i]);
	for (int i = 0; i <= 2000; i++) {
		c[i][0] = 1;
		for (int j = 1; j <= i; j++) 
			c[i][j] = (c[i - 1][j - 1] + c[i - 1][j]) % Mod;
	}
	int ans = 0;
	for (int i = 0, f = 1; i <= n; i++, f = -f) {
		int tmp = 1;
		for (int j = 1; j <= m; j++)
			tmp = 1ll * tmp * c[n + a[j] - i - 1][n - i - 1] % Mod;
		ans = ((long long)ans + 1ll * tmp * c[n][i] % Mod * f + Mod) % Mod;
	}
	cout << ans << "\n";
	return 0;
}

posted @ 2018-08-14 19:00  谜のNOIP  阅读(97)  评论(0编辑  收藏  举报