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 eld. 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 rst 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 rst 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
2
2 2 1
2 3 2
Sample Output
Case 1: 0
Case 2: 2
【题意】
n行m列网格放k个石子。有多少种方法?要求第一行,第一列,最后一行,最后一列必须有石子。
【题解】
利用容斥原理。可以转到求“第一行、第一列、最后一行、最后一列没有石子”的方案数。
枚举各个集合的组合时可以借助二进制进行枚举
1.第一种二进制枚举
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n,m,sec,k;
int C[][];
const int mod=;
void pre()
{
memset(C,,sizeof(C));
for(int i=;i<=;i++)
C[i][]=; for(int i=;i<=;i++)
for(int j=;j<=i;j++)
C[i][j]=(C[i-][j]+C[i-][j-])%mod;
}
int main()
{
pre();
scanf("%d",&sec);
for(int z=;z<=sec;z++)
{
scanf("%d%d%d",&n,&m,&k);
int ans=;
for(int i=;i<;i++)
{
int b=,r=n,c=m;
if(i&){r--;b++;}
if(i&){r--;b++;}
if(i&){c--;b++;}
if(i&){c--;b++;} if(b%==)ans=(ans+C[r*c][k])%mod;
else ans=(ans+mod-C[r*c][k])%mod;
} printf("Case %d: %d\n",z,ans);
}
return ;
}
2.第二种二进制枚举
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int mod=1e6+; //记得加等号
int c[][];
void get() //求组合数模板 注意细节问题
{
memset(c,,sizeof(c));
for(int i=;i<=;i++)
c[i][]=;
for(int i=;i<=;i++)
for(int j=;j<=i;j++)
c[i][j]=(c[i-][j]+c[i-][j-])%mod;
}
int main()
{
get();
int t,cas=;
cin>>t;
while(t--)
{
int n,m,k;
cin>>n>>m>>k;//输入别忘了
int sum=;
for(int i=;i<(<<);i++)
{
int flag=,r=n,h=m;
for(int j=;j<;j++)
{
if(i&(<<j))
{
flag++;
if(j==||j==)
r--;
else
h--;
}
}
if(flag&)
sum=(sum-c[r*h][k]+mod)%mod; //c用过了 用h
else
sum=(sum+c[r*h][k]+mod)%mod;
}
printf("Case %d: %d\n",cas++,sum);
}
}