UVA - 11806 Cheerleaders (容斥原理)

题意:一个N*M的场地,有K个人要站到场地上去,遵循以下条件:

  1. 一个格子只能站一个人
  2. 首行,末行以及首列,末列都必须有人站
    求有多少种方案(人可以看做无区别).
    分析:直接统计符合的方案数不太容易,可以反过来计算不符合的方案数,再用全部方案数减去.
    不符合的方案数用容斥的方法计算.设二进制的第1位代表首行无人,第2位代表末行无人,第3位代表首列无人,第4位代表末列无人.二进制枚举时,每次的答案通过组合数计算即可,在\(h*w\)的格点阵中放置k个人的情况为\(C(h*w,k)\)种.先预处理出组合数.
    最后用无限制的全部方案数为:\(C(N*M,k)\),减去容斥计算出的结果.
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int mod = 1000007;
const int MAXN = 600;
LL fac[MAXN+5], inv[MAXN+5];
LL Comb[MAXN][MAXN];

void pre()
{
    memset(Comb,0,sizeof(Comb));
    Comb[0][0] = 1;
    Comb[1][0] = Comb[1][1] = 1;
    for(int i=2;i<MAXN;++i){
        Comb[i][0] = 1;
        for(int j=1;j<=i;++j){
            Comb[i][j] = (Comb[i-1][j-1] + Comb[i-1][j])%mod;
        }
    }
}

int main()
{
    #ifndef ONLINE_JUDGE
        freopen("in.txt","r",stdin);
        freopen("out.txt","w",stdout);
    #endif
    pre();
    int T,cas=1; scanf("%d",&T);
    while(T--){
        int N,M,k;
        scanf("%d %d %d",&N, &M, &k);
        LL res=0;
        for(int i =0;i<16;++i){
            int cnt = __builtin_popcount(i);
            int h = N, w = M;
            if(i&1) h--;
            if(i&2) h--;
            if(i&4) w--;
            if(i&8) w--;
            if(cnt&1) res = (res+ mod-Comb[h*w][k])%mod;
            else res = (res + Comb[h*w][k])%mod;
        }
        printf("Case %d: %lld\n",cas++,res);
    }
    return 0;
}
posted @ 2018-09-27 19:51  xiuwenL  阅读(129)  评论(0编辑  收藏  举报