UVA 11806 Cheerleaders 容斥原理
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. Kdenotes 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 |
1 #include<iostream> 2 #include<string.h> 3 #include<stdio.h> 4 using namespace std; 5 6 const int maxn=401; 7 8 const int mod=1000007;///不要忘了取模 9 10 long long int c[maxn][maxn]; 11 12 int main() 13 { 14 c[0][0]=1;//注意这里 wrong的原因 c(0,0)结果为1 不为0 15 16 //首先对组合数进行打表 17 for(int i=1;i<maxn;i++) 18 { 19 for(int j=0;j<=i;j++) 20 { 21 if(j==0||j==i) 22 c[i][j]=1; 23 else 24 c[i][j]=c[i-1][j]+c[i-1][j-1]; 25 c[i][j]%=mod; 26 27 } 28 } 29 30 31 long long int n,m,k; 32 int t; 33 //freopen("aa.txt","r",stdin); 34 cin>>t; 35 for(int kase=1;kase<=t;kase++){ 36 cin>>n>>m>>k; 37 if(k>n*m){ 38 cout<<"Case "<<kase<<": "<<0<<endl; 39 continue; 40 } 41 42 ///容斥原理进行计算 43 long long int ans=c[n*m][k]; 44 long long int sum=0; 45 sum+=2*c[(n-1)*m][k]; 46 sum+=2*c[n*(m-1)][k]; 47 sum-=c[(n-2)*m][k]; 48 sum-=c[n*(m-2)][k]; 49 sum-=4*c[(n-1)*(m-1)][k]; 50 sum+=2*c[(n-1)*(m-2)][k]; 51 sum+=2*c[(n-2)*(m-1)][k]; 52 sum-=c[(n-2)*(m-2)][k]; 53 54 55 while(ans-sum<0) 56 ans+=mod; 57 ans-=sum; 58 ans%=mod; 59 cout<<"Case "<<kase<<": "<<ans<<endl; 60 } 61 return 0; 62 }