UVA - 11806 Cheerleaders 【容斥定理+排列组合打表】

题目链接:https://cn.vjudge.net/problem/UVA-11806

题意:(蓝书 P107)

思路:

正向想问题会有点复杂,反过来想,假设 A为第一行一个都不放,B为最后一行一个都不放,C为第一列一个都不放,D为最后一列一个都不放,那么答案一定是不包括这些集合的,n*m放k个石头,有c(n*m,k)种方法,那么答案就是c(n*m,k)减去所有包含ABCD某个或者某几个的个数;ABCD这四个集合会有16种组合,根据容斥定理得出一个公式 sum = sum1-(A+B+C+D)+(AB+AC+AD+BC+BD+CD)-(ABC+ABD+ACD+BCD)+ABCD;   sum1就是 c(n*m,k);

如果用  n!/((n-k)!*k!) 计算c(n,k)会超时;所以需要用  c(n,k) = c(n-1,k)+c(n-1,k-1) 把我们需要用到的所有排序组合的答案预先打表;

枚举 0000到1111能够写出所有ABCD可能的搭配,比如 0001 就是 A 0011 就是 AB,0111就是ABC;如果搭配是 AD (第一行一个都不放,最后一列一个都不放),那么说明能放的格子有(n-1)*(m-1)个,所以AD这种搭配的方案数是 c((n-1)*(m-1),k);我们只需要知道某种搭配能放的格子有几行几列就行;

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<queue>
#include<map>
#include<stack>
#include<sstream>
#include<vector>
#include<string>
#include<set>

using namespace std;

#define IOS ios::sync_with_stdio(false); cin.tie(0);
#define REP(i,n) for(int i=0;i<n;++i)

int read(){

    int r=0,f=1;char p=getchar();
    while(p>'9'||p<'0'){if(p=='-')f=-1;p=getchar();}
    while(p>='0'&&p<='9'){r=r*10+p-48;p=getchar();}return r*f;
}

typedef long long ll;
typedef unsigned long long ull;
const int Maxn = 500;
const long long LINF = 1e18;
const int INF = 0x3f3f3f3f;
const int Mod = 1e6+7;
const double PI = acos(-1.0);

int n,m,k,c[Maxn+10][Maxn+10];

int main (void)
{    // 所有组合排列打表
    // c(n,m) = c(n-1,m) + c(n-1,m-1) 排列组合的递推公式
    memset(c,0,sizeof(c));  
    c[0][0] = 1;
    for (int i = 0; i <= Maxn; ++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])%Mod;
        }
    }
    // A第一行一个都不放,B最后一行一个都不放,以此类推
    // sum = sum1-(A+B+C+D)+(AB+AC+AD+BC+BD+CD)-(ABC+ABD+ACD+BCD)+ABCD; (容斥定理,蓝书)

    int t;
    scanf("%d",&t);
    for (int cas = 1; cas <= t; ++cas) {
        scanf("%d%d%d",&n,&m,&k);
        int r,cn,b,ans = 0; // r ->行,cn ->列,b为ABCD中选取的个数
        for (int i = 0; i < 16; ++i) {  // 0000 到 1111,由上面的公式,ABCD四种集合的搭配有16种
            r = n; cn = m; b = 0;           // sum1是所有集合的总和,全集减去包含ABCD所有情况的方案数就是答案
            if(i&1) { r--; b++; } // 0001
            if(i&2) { r--; b++; }  // 0010
            if(i&4) { cn--; b++; } // 0100
            if(i&8) { cn--; b++; } // 1000
            if(b&1) ans = (ans+Mod-c[r*cn][k])%Mod; // b&1 == 1 为奇数
            else ans = (ans+c[r*cn][k])%Mod;
        }
        printf("Case %d: %d\n",cas,ans);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值