UVa 11806 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 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:
 1. 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.
 2.There can be at most one cheerleader in a cell.
 3.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 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
题目大意:在一个m行n列的矩形网格里放k个相同的棋子,问有多少种方法?每个格子最多放一个石子,所有石子都要用完,并且第一行、最后一行。第一列、最后一列都得有石子.(每个测试点有T组数据).
题解:比较裸的容斥原理,设总方案数为S,第一行不放的方案数为A,最后一行不放的方案数为B,第一列不放的方案数为C,最后一列不放的方案数为D;将S,A,B,C,D即他们的并集加加减减即可得到答案。
可以发现搭配方式(即A,B,C,D那些存在)可以用一个四位二进制串来表示,存在为1不存在为0,则S为0000;若A,B存在,则相当与少了一行,若B,C存在,则相当于少了一列,假如最后剩了r行s列此种搭配下的方法数就是c(r*s)(k)。根据容斥原理的规律,若串中1的个数为奇数个,则要减掉方案数,偶数个则要加上方案数.(预处理出组合数,注意边界.)`

#include<iostream>
using namespace std;
int n,m,t,c[1001][1001],sum,r,s,b,k;
int MD(1000007);
int main()
{
   c[0][0]=1;
   for (int i=1;i<=500;i++)
     {
 c[i][0]=c[i][i]=1;
 for (int j=1;j<i;j++)
   c[i][j]=(c[i-1][j]+c[i-1][j-1])%MD;
     }
   cin>>t;
   for (int i=1;i<=t;i++)
     {
   cin>>m>>n>>k;
   sum=0;
   for (int j=0;j<16;j++)
{
  r=m,s=n,b=0;
  if (j&1) {r--;b++;}
  if (j&2) {r--;b++;}
  if (j&4) {s--;b++;}
  if (j&8) {s--;b++;}
  if (b&1) sum=(sum+MD-c[r*s][k])%MD;
   else sum=(sum+c[r*s][k])%MD;
} 
cout<<"Case "<<i<<": "<<sum<<endl;
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值