UVa11806 Cheerleaders(容斥原理)
11806 - Cheerleaders
Time limit: 2.000 seconds
C 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 Sample Output 2 2 2 1 2 3 2 Case 1: 0 Case 2: 2
//本题利用容斥原理:S为全集!!
A表示第一行没有石子;B表示第n行没有石子;C表示第一列没有石子;D表示第n列没有石子;
则A∪B∪C∪D=S;
则S=(A+B+C+D)-(_∩_)+(_∩__∩_)-(_∩__∩__∩_); //
1 #include<stdio.h> 2 #include<string.h> 3 #include<algorithm> 4 #include<math.h> 5 #include<queue> 6 #include<set> 7 #include<vector> 8 #include<bitset> 9 using namespace std; 10 typedef long long ll; 11 12 const int mo=1000007; 13 const int M=600; 14 15 int C[M][M]; 16 void yanghui() 17 { 18 int i,j; 19 C[0][0]=C[1][0]=C[1][1]=1; 20 for(i=2;i<M;i++) 21 { 22 C[i][0]=C[i][i]=1; 23 for(j=1;j<i;j++) 24 C[i][j]=(C[i-1][j-1]+C[i-1][j])%mo; 25 } 26 } 27 28 int main() 29 { 30 yanghui(); 31 int st,ca,k,m,n,T,i; 32 scanf("%d",&T); 33 for(ca=1;ca<=T;ca++) 34 { 35 scanf("%d%d%d",&n,&m,&k); 36 printf("Case %d: ",ca); 37 if(k>n*m){printf("0\n");continue;} 38 int s=C[n*m][k]; 39 for(st=1;st<16;st++)//枚举状态 40 { 41 int b=0,r=n,c=m;//b统计集合的个数,r和c统计可以防止的行列数!!! 42 if(st&1){b++;r--;}; 43 if(st&4){b++;r--;}; 44 if(st&2){b++;c--;}; 45 if(st&8){b++;c--;}; 46 if(b&1)s=(s-C[r*c][k]+mo)%mo; 47 else s=(s+C[r*c][k])%mo; 48 } 49 printf("%d\n",s); 50 } 51 return 0; 52 }