UVa 11806 Cheerleaders(容斥定理)

原题连接:https://vjudge.net/problem/UVA-11806

Description: 
In most professional sporting events, cheerleaders play a major role in entertaining the spectators. Their 
roles are substantial during breaks and prior to start of play. The world cup soccer is no exception. 
Usually the cheerleaders form a group and perform at the centre of the field. In addition to this group, 
some of them are placed outside the side line so they are closer to the spectators. The organizers would 
like to ensure that at least one cheerleader is located on each of the four sides. For this problem, we 
will model the playing ground as an M × N rectangular grid. The constraints for placing cheerleaders 
are described below: 
• There should be at least one cheerleader on each of the four sides. Note that, placing a cheerleader 
on a corner cell would cover two sides simultaneously. 
• There can be at most one cheerleader in a cell. 
• All the cheerleaders available must be assigned to a cell. That is, none of them can be left out. 
 
The organizers would like to know, how many ways they can place the cheerleaders while maintainingthe above constraints. Two placements are different, if there is at least one cell which contains acheerleader in one of the placement but not in the other.

Input 
The first line of input contains a positive integer T ≤ 50, which denotes the number of test cases. Tlines then follow each describing one test case. Each case consists of three nonnegative integers, 2 ≤ M,N ≤ 20 and K ≤ 500. Here M is the number of rows and N is the number of columns in the grid. K 
denotes the number of cheerleaders that must be assigned to the cells in the grid.

Output 
For each case of input, there will be one line of output. It will first contain the case number followed bythe number of ways to place the cheerleaders as described earlier. Look at the sample output for exactformatting. Note that, the numbers can be arbitrarily large. Therefore you must output the answers modulo 1000007.

Sample Input 

2 2 1 
2 3 2

Sample Output 
Case 1: 0 
Case 2: 2

题目大意: 
在一个N*M的场地上,将k个人放置到这些格子上。要满足下列三个要求: 
1.每个边界上的至少有一个人(处于两边界交点处<即四个角上>时,算是经过了两个边界) 
2.每个格子上最多放一个人 
3.这k个人必须都放置到格子上,即不允许有人空着

解题思路: 
直接求不好求,那我们采取逆向思维。 
首先如果没有限制条件的话,答案数就是从n*m个格子里选出k个来,答案就是C(n*m, k),然后减去边界行列不存在人的情况。这时如果记最上一行没人的情况为A,最下一行没人的情况为B,最左一列没人的情况为C,最右一列没人的情况为D。 
则答案就是C(n*m, k) - A - B - C - D + AB + AC + AD + BC + BD + CD - ABC - ACD - ABD - BCD + ABCD。即

ans = C(nm,k)−2(C((n−1)m,k)+C((m−1)n,k))

+(C((n−2)m,k)+C((m−2)n,k)+4C((m−1)(n−1),k)

−2C(m−2)(n−1),k)−2C(C(m−1)(n−2),k)+C((m−2)(n−2),k);

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int mod = (int)1e6 + 7;
const int N = 420;

LL CC[N][N] = { 0 };
LL T, n, m, k;

void init() {
	CC[0][0] = 1;
	for (LL i = 1; i < N; i++) {
		CC[i][0] = 1;
		for (LL j = 1; j <= i; j++) {
			CC[i][j] = (CC[i - 1][j - 1] + CC[i - 1][j]) % mod;
		}
	}
}

LL C(LL n, LL m) {
	return CC[n][m];
}

int main(void) {
	init();
	scanf("%lld", &T);
	while (T--) {
		scanf("%lld%lld%lld", &n, &m, &k);
		LL ans = (C(n * m, k) - (2 * C((n - 1) * m, k) + 2 * (C(n * (m - 1), k)) - C((m - 2) * n, k) - C((n - 2) * m, k) - 4 * C((m - 1) * (n - 1), k) + 2 * C((m - 2) * (n - 1), k) + 2 * C((m - 1) * (n - 2), k) - C((m - 2) * (n - 2), k)));
		while (ans < 0) {
			ans += mod;
		}
		printf("%lld
", ans % mod);
	}
	return 0;
}
原文地址:https://www.cnblogs.com/shmilky/p/14089036.html