您的位置:首页 > 其它

Cheerleaders UVA - 11806(容斥原理)题解

2017-03-11 11:06 330 查看
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 maintaining

the above constraints. Two placements are different, if there is at least one cell which contains a

cheerleader 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. T

lines 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 by

the number of ways to place the cheerleaders as described earlier. Look at the sample output for exact

formatting. Note that, the numbers can be arbitrarily large. Therefore you must output the answers

modulo 1000007.

Sample Input

2

2 2 1

2 3 2

Sample Output

Case 1: 0

Case 2: 2

白书上的题,一个矩形方阵上放置k个物品,求四条边上都有东西的方案总数。因为在四个角分别会让两条边都满足条件,比较难求解,所以转换为求四条边中某几条边没有东西的方案数,然后用容斥原理求答案。用1234表示第1234条边没有东西的情况,枚举1-15,用二进制为的0,1表示用没有发生1234中的某种情况,依次就会得到所有情况都没出现,一条边没东西,两条边没东西…..然后对偶数情况加,计数情况减(容斥原理)就能得到答案。

#include<iostream>
#include<algorithm>
#include<string.h>
#include<stdio.h>
#include<vector>
#include<queue>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
const int maxe = 30005;
const int maxn = 1000005;
const int maxk = 505;
const int mod = 1000007;
int n,m,k;
int c[maxk][maxk];

int main(){
for (int i = 0; i < maxk; i++){
c[i][0] = c[i][i] = 1;
for (int j = 1; j < i; j++){
c[i][j] = (c[i - 1][j] + c[i - 1][j - 1]) % mod;
}
}
int t;
scanf("%d", &t);
for (int cas = 1; cas <= t; cas++){
scanf("%d%d%d", &n, &m,&k);
int sum = 0;
for (int i = 0; i < 16; i++){

bde2
int nn = n, mm = m, b = 0;
if (i & 1){ nn--; b++; }
if (i & 2){ nn--; b++; }
if (i & 4){ mm--; b++; }
if (i & 8){ mm--; b++; }
if (b & 1){ sum = (sum + mod - c[nn*mm][k])%mod; }
else{ sum = (sum + c[nn*mm][k]) % mod; }
}
printf("Case %d: %d\n", cas, sum);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: