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 ...
随机推荐
- 友盟iOS推送配置(从真机调试到推送)
下面我来讲解一下友盟iOS的推送配置,其实友盟只是一个示例,换做其余的第三方推送服务也会适用,只是第三方的后面服务变了而已. iOS推送(包括真机调试)所需要的步骤和文件如下: 备注:这里我将省略掉一 ...
- make xconfig时,出现“Unable to find any QT installation"错误
在make menuconfig的时候,提示是找不到libncurses,最后其实是找不到libncurses-dev,又因为debian系统装的是libncurses5,所以装了libncurses ...
- 查看apk包名package和入口activity名称的方法
ctrl+r 打开CMD窗口 进入sdk-aapt目录 执行命令:aapt dump badging xx.apk 内容太多?不好看,没关系,全部拷出来,ctrl+f,so easy! package ...
- Mac OS X 10.9 Mavericks安装后,Xcode调试时模拟器黑屏的处理方法
请耐心的等下去吧,少年! 装了Mac OS X 10.9 Mavericks的同学,如果碰到Xcode调试App时,模拟器黑屏(重置也无效),请耐心的等下去吧,大约10来分钟左右黑屏就会消失,App启 ...
- mq安装参考
CentOS 6.2 64bit 安装erlang及RabbitMQ Server 1.操作系统环境(CentOS 6.2 64bit) [root@leekwen ~]# cat /etc/issu ...
- Protocol Buffer技术详解(Java实例)
Protocol Buffer技术详解(Java实例) 该篇Blog和上一篇(C++实例)基本相同,只是面向于我们团队中的Java工程师,毕竟我们项目的前端部分是基于Android开发的,而且我们研发 ...
- 应用PHPCMS V9轻松完成WAP手机网站搭建全教程
用PHPCMS最新发布的V9搭建了PHPCMS研究中心网站(http://phpcms.org.cn)完成后,有用户提出手机访问的问题,于是着手搭建WAP无线站(wap.phpcms.org.cn). ...
- JS(截取字符串,显示当前系统时间yyyy-MM-dd,从文本框得到的数值计算)
截取字符串: var str = "1234567890"; var a = str.substring(0,8); //==str.substring(8)---结果:12 ...
- 使用ymPrompt弹框
使用弹框 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8&q ...
- 对称加密和分组加密中的四种模式(ECB、CBC、CFB、OFB)
一. AES对称加密: AES加密 分组 二. 分组密码的填充 分组密码的填充 e.g.: PKCS#5填充方式 三. 流密码: 四. 分组密码加密中的四种模式: 3.1 ECB模式 优点: 1. ...