uva11806(容斥原理)
11806 - Cheerleaders
Time limit: 2.000 seconds
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 field. 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 first 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 first 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 |
Sample Output |
2 2 2 1 2 3 2 |
Case 1: 0 Case 2: 2
|
直接分类讨论所有情况很麻烦,容易出错。那么正难则反,用容斥原理做就很简单了。
思路很好想,关键是写法。我觉得还是大白书上的用二进制来表示状态的写法最简明易懂。
我学习着写了一发,AC了。但是我觉得原书每次计算ans时没有考虑当少了几行或几列有可能不够把k个都放进去了,我加了个判断语句,当然对于uva上的数据,不加这个判断也是可以AC的。
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#include<stack>
#include<queue>
using namespace std;
#define INF 1000000000
#define eps 1e-8
#define pii pair<int,int>
#define LL long long int
int T,n,m,k,ans;
const int mod=;
int C[][];
int main()
{
//freopen("in6.txt","r",stdin);
//freopen("out.txt","w",stdout);
for(int i=;i<=;i++) C[i][]=C[i][i]=;
for(int i=;i<=;i++)
{
for(int j=;j<=i-;j++)
{
C[i][j]=(C[i-][j]+C[i-][j-])%mod;
}
}
scanf("%d",&T);
for(int cas=;cas<=T;cas++)
{
scanf("%d%d%d",&m,&n,&k);//m*n
ans=;
for(int i=;i<;i++)
{
int row=m,col=n,b=;
if(i&){row--;b++;}
if(i&){row--;b++;}
if(i&){col--;b++;}
if(i&){col--;b++;}
if(b&)//b is a odd.
{
if(col*row>=k)
ans=(ans-C[col*row][k]+mod)%mod;
}
else
{
if(col*row>=k)
ans=(ans+C[col*row][k])%mod;
}
}
printf("Case %d: %d\n",cas,ans);
}
//fclose(stdin);
//fclose(stdout);
return ;
}
uva11806(容斥原理)的更多相关文章
- 【UVA11806 Cheerleaders】 题解
题目链接:https://www.luogu.org/problemnew/show/UVA11806 容斥原理+组合数 正着找合♂fa的不好找,那就用总方案数-不合♂fa的 #include < ...
- UVa11806 Cheerleaders(容斥原理)
11806 - Cheerleaders Time limit: 2.000 seconds C Cheerleaders In most professional sporting events, ...
- UVa 11806 拉拉队(容斥原理)
https://vjudge.net/problem/UVA-11806 题意: 在一个m行n列的矩形网格里放k个相同的石子,有多少种方法?每个格子最多放一个石子,所有石子都要用完,并且第一行.最后一 ...
- hdu4059 The Boss on Mars(差分+容斥原理)
题意: 求小于n (1 ≤ n ≤ 10^8)的数中,与n互质的数的四次方和. 知识点: 差分: 一阶差分: 设 则 为一阶差分. 二阶差分: n阶差分: 且可推出 性质: 1. ...
- hdu2848 Visible Trees (容斥原理)
题意: 给n*m个点(1 ≤ m, n ≤ 1e5),左下角的点为(1,1),右上角的点(n,m),一个人站在(0,0)看这些点.在一条直线上,只能看到最前面的一个点,后面的被档住看不到,求这个人能看 ...
- BZOJ2301: [HAOI2011]Problem b[莫比乌斯反演 容斥原理]【学习笔记】
2301: [HAOI2011]Problem b Time Limit: 50 Sec Memory Limit: 256 MBSubmit: 4032 Solved: 1817[Submit] ...
- BZOJ 2440: [中山市选2011]完全平方数 [容斥原理 莫比乌斯函数]
2440: [中山市选2011]完全平方数 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 3028 Solved: 1460[Submit][Sta ...
- ACM/ICPC 之 中国剩余定理+容斥原理(HDU5768)
二进制枚举+容斥原理+中国剩余定理 #include<iostream> #include<cstring> #include<cstdio> #include&l ...
- HDU5838 Mountain(状压DP + 容斥原理)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5838 Description Zhu found a map which is a N∗M ...
随机推荐
- 免费的webservice接口
快递查询接口 http://webservice.36wu.com/ExpressService.asmx ip查询接口 http://webservice.36wu.com/ipService.as ...
- redis3.2.11多机多实例集群部署及测试连接情况
机器配置 redis3.2.11安装配置规划 机器 192.168.169.136(本机虚拟机1) 192.168.169.137(本机虚拟机2) 系统 Red Hat Enterprise Linu ...
- Python进阶(4)_进程与线程 (python并发编程之多进程)
一.python并发编程之多进程 1.1 multiprocessing模块介绍 由于GIL的存在,python中的多线程其实并不是真正的多线程,如果想要充分地使用多核CPU的资源,在python中大 ...
- 20160422 --Switch…case 总结; 递归算法
13 2016-04-22 11:01:00 Switch…case 总结(网摘) 例题: Console.WriteLine("1.汉堡包"); Console.WriteLi ...
- val() attr('value')
val() 只能更改输入框内的值,能更改value属性, 在浏览器中体现不出value被改变 attr('value') 都可以 谷歌浏览器 val,attr都能获取输入框最新的value值
- iOS Xcode 8 打包上架 配置文件问题 此构建版本无效 推送问题 攻克 刚刚 waiting for review
煎熬... 本来xcode 7 打包正在审核了 还要修改一处购买问题 此时 xcode已经升级到xcode 8 了 升级后 很神奇 应用没报错 很好的运行 (网上很多说xib storyboard ...
- iOS self 和 super 学习
有人问我 这个问题 回答错了,题干大概是说 [self class] 和 [super class]打印结果 是不是一样的. 我睁着眼睛说是不一样的 .因为我明明记得 几天前 做 DFS 获取反射基类 ...
- P3794 签到题IV
题目 P3794 签到题IV 来切道水题放松一下吧 做法 或是单调不下降的,\(gcd\)是单调不上升的 \(a_i≤5×10^5\)分成权值不同的块数应该很小,所以随便乱搞就出来了 My compl ...
- Raspberry 2B && Ubuntu mate 16.04 && *** 完美透明代理
Raspberry 2B && Ubuntu mate 16.04 && *** 完美透明代理 关键词:Raspberry 2B, Ubuntu mate 16.04 ...
- K8s + Flannel 网络架构图
这是Flannel官网给出的网络架构图 这是通过自己的理解画的逻辑结构图 查看bridge [root@node01 ~]# brctl show bridge name bridge id STP ...