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:
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.
Input
The rst 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

【题意】

n行m列网格放k个石子。有多少种方法?要求第一行,第一列,最后一行,最后一列必须有石子。

【题解】

利用容斥原理。可以转到求“第一行、第一列、最后一行、最后一列没有石子”的方案数。

枚举各个集合的组合时可以借助二进制进行枚举

1.第一种二进制枚举

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n,m,sec,k;
int C[][];
const int mod=;
void pre()
{
memset(C,,sizeof(C));
for(int i=;i<=;i++)
C[i][]=; for(int i=;i<=;i++)
for(int j=;j<=i;j++)
C[i][j]=(C[i-][j]+C[i-][j-])%mod;
}
int main()
{
pre();
scanf("%d",&sec);
for(int z=;z<=sec;z++)
{
scanf("%d%d%d",&n,&m,&k);
int ans=;
for(int i=;i<;i++)
{
int b=,r=n,c=m;
if(i&){r--;b++;}
if(i&){r--;b++;}
if(i&){c--;b++;}
if(i&){c--;b++;} if(b%==)ans=(ans+C[r*c][k])%mod;
else ans=(ans+mod-C[r*c][k])%mod;
} printf("Case %d: %d\n",z,ans);
}
return ;
}

2.第二种二进制枚举

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int mod=1e6+; //记得加等号
int c[][];
void get() //求组合数模板 注意细节问题
{
memset(c,,sizeof(c));
for(int i=;i<=;i++)
c[i][]=;
for(int i=;i<=;i++)
for(int j=;j<=i;j++)
c[i][j]=(c[i-][j]+c[i-][j-])%mod;
}
int main()
{
get();
int t,cas=;
cin>>t;
while(t--)
{
int n,m,k;
cin>>n>>m>>k;//输入别忘了
int sum=;
for(int i=;i<(<<);i++)
{
int flag=,r=n,h=m;
for(int j=;j<;j++)
{
if(i&(<<j))
{
flag++;
if(j==||j==)
r--;
else
h--;
}
}
if(flag&)
sum=(sum-c[r*h][k]+mod)%mod; //c用过了 用h
else
sum=(sum+c[r*h][k]+mod)%mod;
}
printf("Case %d: %d\n",cas++,sum);
}
}

UVA11806-Cheerleaders(容斥原理+二进制)的更多相关文章

  1. UVA.11806 Cheerleaders (组合数学 容斥原理 二进制枚举)

    UVA.11806 Cheerleaders (组合数学 容斥原理 二进制枚举) 题意分析 给出n*m的矩形格子,给出k个点,每个格子里面可以放一个点.现在要求格子的最外围一圈的每行每列,至少要放一个 ...

  2. HDU.1796 How many integers can you find ( 组合数学 容斥原理 二进制枚举)

    HDU.1796 How many integers can you find ( 组合数学 容斥原理 二进制枚举) 题意分析 求在[1,n-1]中,m个整数的倍数共有多少个 与 UVA.10325 ...

  3. UVa 11806 Cheerleaders (容斥原理+二进制表示状态)

    In most professional sporting events, cheerleaders play a major role in entertaining the spectators. ...

  4. UVa11806 Cheerleaders(容斥原理)

    11806 - Cheerleaders Time limit: 2.000 seconds C Cheerleaders In most professional sporting events, ...

  5. 【UVA11806 Cheerleaders】 题解

    题目链接:https://www.luogu.org/problemnew/show/UVA11806 容斥原理+组合数 正着找合♂fa的不好找,那就用总方案数-不合♂fa的 #include < ...

  6. HDU 1796 How many integers can you find(容斥原理+二进制/DFS)

    How many integers can you find Time Limit: 12000/5000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  7. UVA 11806 Cheerleaders (容斥原理)

    题意 一个n*m的区域内,放k个啦啦队员,第一行,最后一行,第一列,最后一列一定要放,一共有多少种方法. 思路 设A1表示第一行放,A2表示最后一行放,A3表示第一列放,A4表示最后一列放,则要求|A ...

  8. UVA.10325 The Lottery (组合数学 容斥原理 二进制枚举)

    UVA.10325 The Lottery (组合数学 容斥原理) 题意分析 首先给出一个数n,然后给出m个数字(m<=15),在[1-n]之间,依次删除给出m个数字的倍数,求最后在[1-n]之 ...

  9. UVA11806 Cheerleaders

    题意 PDF 分析 如果要求是某行某列没有石子很好算,就一个组合数. 然后要求某行某列有,就用容斥原理就行了. 时间复杂度\(O(k^2 + 16T)\) 代码 #include<iostrea ...

随机推荐

  1. NOI题库

    07:机器翻译 总时间限制: 1000ms 内存限制: 65536kB 描述 小晨的电脑上安装了一个机器翻译软件,他经常用这个软件来翻译英语文章. 这个翻译软件的原理很简单,它只是从头到尾,依次将每个 ...

  2. Bzoj 1336&1337 Alien最小圆覆盖

    1336: [Balkan2002]Alien最小圆覆盖 Time Limit: 1 Sec  Memory Limit: 162 MBSec  Special Judge Submit: 1473  ...

  3. 腾讯云ubuntu下mysqli服务的开启

    腾讯云ubuntu下mysqli服务的开启 今天晚上搞了好久,在本地操作系统deepin下操作完全无需开启mysqli模块,自动就开启了.这次介绍一下服务器ubuntu下mysqli模块的开启. 首先 ...

  4. HTTPS 协议降级攻击原理

    0x00 HTTPS 在传统流行的web服务中,由于http协议没有对数据包进行加密,导致http协议下的网络包是明文传输,所以只要攻击者拦截到http协议下的数据包,就能直接窥探这些网络包的数据. ...

  5. laravel框架中的session问题

    这两天一直在鼓捣服务器,配置环境,在搭建laravel的过程之中,发现了laravel中的session的一些问题,这里总结一下: (1):我在服务器上搭建了多个sever,为了测试学习,分别使用不同 ...

  6. POJ2823Sliding Window

    Sliding Window Time Limit: 12000MS   Memory Limit: 65536K Total Submissions: 49919   Accepted: 14391 ...

  7. ASP.NET MVC 过滤器详解

    http://www.fwqtg.net/asp-net-mvc-%E8%BF%87%E6%BB%A4%E5%99%A8%E8%AF%A6%E8%A7%A3.html 我经历了过滤器的苦难,我想到了还 ...

  8. ECSHOP手机号码或邮箱用户名都可以登录方法

    ECSHOP手机号码或邮箱用户名都可以登录方法 ECSHOP教程/ ecshop教程网(www.ecshop119.com) 2013-06-30   有不少人都在找支持ECShop用户名.邮箱或手号 ...

  9. Writing the first draft of your science paper — some dos and don’ts

    Writing the first draft of your science paper — some dos and don’ts 如何起草一篇科学论文?经验丰富的Angel Borja教授告诉你 ...

  10. hibernate criteria中Restrictions的用法

    方法说明 方法 说明 Restrictions.eq = Restrictions.allEq 利用Map来进行多个等于的限制 Restrictions.gt > Restrictions.ge ...