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 modulo1000007.
Sample Input | Sample Output |
2 2 2 1 2 3 2 | Case 1: 0 Case 2: 2 |
解决方案:有四条边,可枚举有一条边没放,有两条边没放,有三条边没放,有四条边没放a[1],a[2],[3],a[4].然后总的放法减去这个就是结果。但a[1],a[2],[3],a[4]比较难算,但A[1],A[2],A[3].A[4],这些规定多少条边不能放的可用,其他随便放的,可由组合数得出。仔细一推,
A[1]=a[1]+2*a[2]+3*a[3]+4*a[4],
A[2]=a[2]+3*a[3]+6*a[4],
A[3]=a[3]+4*a[3],
A[4]=a[4];
可得a[1]+a[2]+a[3]+a[4]=A[1]-A[2]+A[3]-A[4];
code:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int C[403][403];
int A[5];
void init()
{
memset(C,0,sizeof(C));
C[0][0]=1;
for(int i=1; i<=400; i++)
{
C[i][0]=1;
for(int j=1; j<=i; j++)C[i][j]=(C[i-1][j]+C[i-1][j-1])%1000007; ///利用组合数恒等式C(n,m)= C(n,n-m)= C(n-1,m-1)+C(n-1,m)
}
}
int main()
{
int t,k=0;
init();
scanf("%d",&t);
long long N,M,K;
while(t--)
{
memset(A,0,sizeof(A));
scanf("%lld%lld%lld",&N,&M,&K);
int ans=C[N*M][K];
if(K>N*M) {printf("Case %d: 0\n",++k);continue;}
for(int i=1; i<16; i++)
{
int cnt=0;
int m=M,n=N;
for(int j=0; j<4; j++)
{
if(i&(1<<j))
{
cnt++;
if(j<2) n--;
else m--;
}
}
if(K<=m*n)
A[cnt]=(A[cnt]+C[m*n][K])%1000007;
}
printf("Case %d: %d\n",++k,((ans-(A[1]-A[2]+A[3]-A[4]))%1000007+1000007)%1000007);
}
return 0;
}