Inmost professional sporting events, cheerleaders play a major role inentertaining the spectators. Their roles are substantial during breaks andprior to start of play. The world cup soccer is no exception. Usually thecheerleaders form a group and perform at the centre of the field. In additionto this group, some of them are placed outside the side line so they are closerto the spectators. The organizers would like to ensure that at least onecheerleader is located on each of the four sides. For this problem, we willmodel the playing ground as an M*Nrectangular grid. The constraints for placing cheerleaders are described below:
§ There should be at least one cheerleader oneach of the four sides. Note that, placing a cheerleader on a corner cell wouldcover two sides simultaneously.
§ There can be at most one cheerleader in acell.
§ All the cheerleaders available must beassigned to a cell. That is, none of them can be left out.
Theorganizers would like to know, how many ways they can place the cheerleaderswhile maintaining the above constraints. Two placements are different, if thereis at least one cell which contains a cheerleader in one of the placement butnot in the other.
Input
Thefirst line of input contains a positive integer T<=50, which denotes the number of test cases. T lines then follow each describing onetest case. Each case consists of three nonnegative integers, 2<=M, N<=20 and K<=500. Here M is the number of rows and Nis the number of columns in the grid. Kdenotes the number of cheerleaders that must be assigned to the cells in thegrid.
Output
For each case of input, there will be one line ofoutput. It will first contain the case number followed by the number of ways toplace the cheerleaders as described earlier. Look at the sample output forexact formatting. Note that, the numbers can be arbitrarily large. Thereforeyou must output the answers modulo 1000007.
Sample Input | Sample Output |
2 2 2 1 2 3 2 | Case 1: 0 Case 2: 2 |
M*N的矩形网格里放K个相同石子,第一行,第一列,最后一行,最后一列一定要有石子,问有多少种方法。
设第一行没有石子的方案集为A,最后一行没有石子的方案集为B,第一列没有石子的方案集为C,最后一列没有石子的方案集为D,全集为S(S对应空搭配),答案就是在S中但不在ABCD任何一个集合中的元素个数。
这就是容斥原理了,奇偶加减不一样。注意(a-b)%M=(a%M-b%M+M)%M。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<cstdlib>
#include<cmath>
#define INF 0x3f3f3f3f
#define MAXN 510
#define MAXM 20010
#define MAXNODE 4*MAXN
#define MOD 1000007
#define eps 1e-9
using namespace std;
int T,M,N,K;
long long C[MAXN][MAXN];
void init(){
for(int i=0;i<MAXN;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 main(){
freopen("in.txt","r",stdin);
init();
int cas=0;
scanf("%d",&T);
while(T--){
scanf("%d%d%d",&M,&N,&K);
int S=M*N;
long long sum=0;
for(int S=0;S<16;S++){
int cnt=0,r=M,c=N;
if(S&1){
r--;
cnt++;
}
if(S&2){
r--;
cnt++;
}
if(S&4){
c--;
cnt++;
}
if(S&8){
c--;
cnt++;
}
if(cnt&1) sum=(sum+MOD-C[r*c][K])%MOD;
else sum=(sum+C[r*c][K])%MOD;
}
printf("Case %d: %d\n",++cas,sum);
}
return 0;
}