UVA-11806 - Cheerleaders(dp+容斥)

                           

11806 - Cheerleaders

       

  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

题解:题意就是::n行m列网格放k个女孩。有多少种方法?要求第一行,第一列,最后一行,最后一列必须有女孩。

先求出在i个网格中站k个女孩子的方法数;状态转移方程dp[i][k]=dp[i-1][k]+dp[i-1][k-1];初始化是dp[i][0]=1;

然后用容斥解决,四条边中取0条-1条+2条-3条。。。。。

代码:

 

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
using namespace std;
#define mem(x,y) memset(x,y,sizeof(x))
const int INF=0x3f3f3f3f;
typedef long long LL;
const int MOD=1000007;
const int MAXN=510;
int M,N,K;
int dp[MAXN][MAXN];//dp[i][j]i=网格,j=人数; 
void getdp(){
	for(int i=0;i<MAXN;i++)dp[i][0]=1;
	for(int i=1;i<MAXN;i++)
		for(int k=1;k<MAXN;k++)
		dp[i][k]=(dp[i-1][k]+dp[i-1][k-1])%MOD;
}
int rc(){
	getdp();
	int sum=0,r,c,num;
	int *p[4]={&r,&c,&r,&c};
	for(int i=0;i<(1<<4);i++){//0开始代表没选一条的时候 
		r=M;c=N;num=0;
		for(int j=0;j<4;j++){
			if(i&(1<<j)){
				(*p[j])--;
				num++;
			}
		}
	//	for(int j=0;j<4;j++)printf("%d ",*p[j]);puts("");
		if(num&1)sum=(sum-dp[r*c][K]+MOD)%MOD;
		else sum=(sum+dp[r*c][K]+MOD)%MOD;
	}
//	printf("%d %d\n",M,N);
//	return (M*N-sum+MOD)%MOD;
	return (sum+MOD)%MOD;
}
int main(){
	int T,flot=0;
	scanf("%d",&T);
	while(T--){
		scanf("%d%d%d",&M,&N,&K);
		printf("Case %d: %d\n",++flot,rc());
	}
	return 0;
}

  

posted @ 2015-11-23 11:31  handsomecui  阅读(1033)  评论(0编辑  收藏  举报