【Uva11806】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.

题意

  在一个 m n列的矩形网格里放 k 个石子,问有多少种放法?每个格子里最多放一个石子,所有石子都要用完,并且第一行,最后一行,第一列,最后一列都得有石子。

解法

组合数+容斥:
  首先不考虑约束条件,那么我们很容易得出答案为:Cknm
  直接解决这个问题比较难,所以考虑用总方案-不合法的方案,现在重点就变为了求不合法的方案:
  设 A 为第一排不放石子的方案集合,B为第 m 排不放石子的方案集合,C为第一列不放石子的方案集合, D 为最后一列不放石子的方案集合,显然,ABCD,那么就需要使用容斥
因为有4种初始状态,所以可以得出共有16种情况,考虑使用一个四位二进制数(即0~15)来表示这些情况,所以有:
ans=Cknm|A|+|B|+|C|+|D|+|AB|+|AC|+|AD|+|BC|+|BD|+|CD||ABC|+|ABD|+|ACD|+|BCD|+|ABCD|
  剩下的就请看代码

复杂度

O( 10002+T16

代码

#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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值