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 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(容斥原理+二进制)的更多相关文章
- UVA.11806 Cheerleaders (组合数学 容斥原理 二进制枚举)
UVA.11806 Cheerleaders (组合数学 容斥原理 二进制枚举) 题意分析 给出n*m的矩形格子,给出k个点,每个格子里面可以放一个点.现在要求格子的最外围一圈的每行每列,至少要放一个 ...
- HDU.1796 How many integers can you find ( 组合数学 容斥原理 二进制枚举)
HDU.1796 How many integers can you find ( 组合数学 容斥原理 二进制枚举) 题意分析 求在[1,n-1]中,m个整数的倍数共有多少个 与 UVA.10325 ...
- UVa 11806 Cheerleaders (容斥原理+二进制表示状态)
In most professional sporting events, cheerleaders play a major role in entertaining the spectators. ...
- UVa11806 Cheerleaders(容斥原理)
11806 - Cheerleaders Time limit: 2.000 seconds C Cheerleaders In most professional sporting events, ...
- 【UVA11806 Cheerleaders】 题解
题目链接:https://www.luogu.org/problemnew/show/UVA11806 容斥原理+组合数 正着找合♂fa的不好找,那就用总方案数-不合♂fa的 #include < ...
- 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 ...
- UVA 11806 Cheerleaders (容斥原理)
题意 一个n*m的区域内,放k个啦啦队员,第一行,最后一行,第一列,最后一列一定要放,一共有多少种方法. 思路 设A1表示第一行放,A2表示最后一行放,A3表示第一列放,A4表示最后一列放,则要求|A ...
- UVA.10325 The Lottery (组合数学 容斥原理 二进制枚举)
UVA.10325 The Lottery (组合数学 容斥原理) 题意分析 首先给出一个数n,然后给出m个数字(m<=15),在[1-n]之间,依次删除给出m个数字的倍数,求最后在[1-n]之 ...
- UVA11806 Cheerleaders
题意 PDF 分析 如果要求是某行某列没有石子很好算,就一个组合数. 然后要求某行某列有,就用容斥原理就行了. 时间复杂度\(O(k^2 + 16T)\) 代码 #include<iostrea ...
随机推荐
- 【SDOI2009】解题汇总
又开了波专题,感觉就和炉石开冒险一样...(说的好像我有金币开冒险似的) /---------------------------------------------/ BZOJ-1226 [SDOI ...
- sqlserver 还原数据库
1.解决什么问题? a.还原数据库的时候老是提示 不能独占 2.解决方案 ALTER DATABASE [ datebase] SET OFFLINE WITH ROLLBACK IMMEDIATE ...
- Linux System Reinforcement、Intrusion Detection Based On syslog
目录 .文件系统及访问权限 . Linux Syslog . Linux日志审计 . 帐号安全管理 . 基础物理安全 . 系统编译环境安全 . 系统病毒.后门.rootkit安全 . 系统端口.服务安 ...
- MS-sqlserver数据库2008如何转换成2000
http://bbs.csdn.net/topics/390438560?page=1#post-394316973 MS-sqlserver数据库2008如何转换成2000 回你这个贴等于我写个博客 ...
- 慎用 Enum.GetHashCode()
公司里遗留下了相当多的 Enum.GetHashCode()来获取枚举值的代码 但是这会产生装箱行为的!!因为Enum是值类型,GetHashCode()是Object的方法,调用GetHashCod ...
- jquery------捕获异常处理
web.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE web-app PUBLIC ...
- ppa安装php版本
如果你想安装PHP的特定版本,那么这篇文章可以帮助你.这篇文章将帮助您安装PHP 5.4和PHP 5.5 PHP 5.6,通过使用PPA在Ubuntu 15.10 LTS,14.04或12.04 LT ...
- IIS6.0文件解析漏洞小结
今天搞站,本来这个站是aspx的,webserver是IIS6.0的,进入后台之后,发现有一个上传图片的地方,于是,我就上传了一张asp/aspx的一句话图片木马,但是用菜刀连接的时候,没有成功get ...
- JAVA Socket 实现HTTP与HTTPS客户端发送POST与GET方式请求
JAVA Socket 实现HTTP与HTTPS客户端发送POST与GET方式请求 哇,一看标题怎么这么长啊,其实意思很简单,哥讨厌用HTTP Client做POST与GET提交 觉得那个毕竟是别人写 ...
- seajs+spm之再研究
好久没有用seajs了,之前对spm也只是一知半解,这些天再次拿起来研究.谈谈我的认识与理解. 声明:本文不适合对seajs完全不了解的同学阅读.对于想知道seajs来龙去脉以及spm相关的同学&qu ...