题面
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.
题意
在一个m行
n 列的矩形网格里放k个石子,问有多少种放法?每个格子里最多放一个石子,所有石子都要用完,并且第一行,最后一行,第一列,最后一列都得有石子。
解法
组合数+容斥:
首先不考虑约束条件,那么我们很容易得出答案为:Ckn∗m
直接解决这个问题比较难,所以考虑用总方案-不合法的方案,现在重点就变为了求不合法的方案:
设A为第一排不放石子的方案集合,B 为第m排不放石子的方案集合,C 为第一列不放石子的方案集合,D为最后一列不放石子的方案集合,显然,A∩B∩C∩D≠∅ ,那么就需要使用容斥
因为有4种初始状态,所以可以得出共有16种情况,考虑使用一个四位二进制数(即0~15)来表示这些情况,所以有:
ans=Ckn∗m−(|A|+|B|+|C|+|D|)+(|A∩B|+|A∩C|+|A∩D|+|B∩C|+|B∩D|+|C∩D|)−(|A∩B∩C|+|A∩B∩D|+|A∩C∩D|+|B∩C∩D|+|A∩B∩C∩D|)
剩下的就请看代码
复杂度
O(10002+T∗16)
代码
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#define Lint long long int
using namespace std;
const int M=1000007;
const int MAXN=1010;
int C[MAXN][MAXN];
int T,n,m,k;
int ans;
void Prepare()
{
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-1]+C[i-1][j])%M;
}
}
int main()
{
int Case=0,r,c;
Prepare();
scanf("%d",&T);
while( T-- )
{
ans=0;
scanf("%d%d%d",&n,&m,&k);
for(int s=0,b;s<=15;s++)
{
r=n,c=m,b=0;
if( s&1 ) r--,b++;
if( s&2 ) r--,b++;
if( s&4 ) c--,b++;
if( s&8 ) c--,b++;
if( b&1 ) ans=(ans+M-C[r*c][k])%M;
else ans=(ans+C[r*c][k])%M;
}
printf("Case %d: %d\n",++Case,ans);
}
return 0;
}