UVA - 11806(Cheerleaders)容斥原理

题意:

给定n、m、k三个数,n代表行数,m代表列数,k代表人数。

n*m的表格,一共有k个人,要求:

  1. 每个小格只能容纳一个人,所有人必须都在表格中。
  2. 第一行、第一列、最后一行、最后一列必须站人。
  3. 若夹角处站人,则代表此行和列都已站人。

题解:

利用容斥原理,设:

  • S为总数。(n*m中选k个位置,即\large C[n*m][k]
  • A为第一行没有站人。(少一行)
  • B为最后一行没有站人。(少一行)
  • C为第一列没有站人。(少一列)
  • D为最后一列没有站人。(少一列)

文氏图如下:

                         1

我们要计算的就是,S减去ABCD覆盖部分。

容斥原理公式表示为:

\large ans=S-(A+B+C+D)+(AB+AC+AD+BC+BD+CD)-(ABC+ABD+ACD+BCD)+(ABCD)

记:

\large \\s1=(A+B+C+D)\\s2=(AB+AC+AD+BC+BD+CD)\\s3=(ABC+ABD+ACD+BCD)\\s4=(ABCD)

 代码:

#include <set>
#include <map>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <cstdio>
#include <cstring>
#include <sstream>
#include <iomanip>
#include <iostream>
#include <algorithm>
#define clr(str,x) memset(str,x,sizeof(str))
#define FRER() freopen("in.txt","r",stdin);
#define FREW() freopen("out.txt","w",stdout);
#define MAX_INF 0x7fffffff
#define INF 0x3f3f3f3f
#define maxn

typedef long long int ll;
using namespace std;
const int mod = 1000007;
int C[405][405];
int n,m,k,ans=0;
void init()
{
    for(int i=1; i<=400; i++)
    {
        C[i][0]=C[i][i]=1;
        for(int j=1;j<i;j++)
            C[i][j]=(C[i-1][j-1]+C[i-1][j])%mod;
    }
}
int main()
{
//    FRER()
//    FREW()
    init();
    int T,kase=1;
    int s1,s2,s3,s4,s5;
    scanf("%d",&T);
    while(T--)
    {
        ans=0;
        scanf("%d%d%d",&n,&m,&k);
        if(k==0)
        {
            printf("Case %d: 0\n",kase++);
            continue;
        }
        
        s1=C[n*m][k];
        s2=2*(C[n*(m-1)][k]+C[(n-1)*m][k])%mod;
        s3=(C[(n-2)*m][k]+4*C[n*m-n-m+1][k]+C[n*(m-2)][k])%mod;
        s4=2*(C[(n-2)*(m-1)][k]+C[(n-1)*(m-2)][k])%mod;
        s5=C[(n-2)*(m-2)][k]%mod;
        
        ans=s1-s2+s3-s4+s5;
        ans=(ans+10*mod)%mod;//防止取余后出现负数
        printf("Case %d: %d\n",kase++,ans);
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值