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(容斥原理)的更多相关文章

  1. 【UVA11806 Cheerleaders】 题解

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

  2. UVa11806 Cheerleaders(容斥原理)

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

  3. UVa 11806 拉拉队(容斥原理)

    https://vjudge.net/problem/UVA-11806 题意: 在一个m行n列的矩形网格里放k个相同的石子,有多少种方法?每个格子最多放一个石子,所有石子都要用完,并且第一行.最后一 ...

  4. hdu4059 The Boss on Mars(差分+容斥原理)

    题意: 求小于n (1 ≤ n ≤ 10^8)的数中,与n互质的数的四次方和. 知识点: 差分: 一阶差分: 设  则    为一阶差分. 二阶差分: n阶差分:     且可推出    性质: 1. ...

  5. hdu2848 Visible Trees (容斥原理)

    题意: 给n*m个点(1 ≤ m, n ≤ 1e5),左下角的点为(1,1),右上角的点(n,m),一个人站在(0,0)看这些点.在一条直线上,只能看到最前面的一个点,后面的被档住看不到,求这个人能看 ...

  6. BZOJ2301: [HAOI2011]Problem b[莫比乌斯反演 容斥原理]【学习笔记】

    2301: [HAOI2011]Problem b Time Limit: 50 Sec  Memory Limit: 256 MBSubmit: 4032  Solved: 1817[Submit] ...

  7. BZOJ 2440: [中山市选2011]完全平方数 [容斥原理 莫比乌斯函数]

    2440: [中山市选2011]完全平方数 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 3028  Solved: 1460[Submit][Sta ...

  8. ACM/ICPC 之 中国剩余定理+容斥原理(HDU5768)

    二进制枚举+容斥原理+中国剩余定理 #include<iostream> #include<cstring> #include<cstdio> #include&l ...

  9. HDU5838 Mountain(状压DP + 容斥原理)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5838 Description Zhu found a map which is a N∗M ...

随机推荐

  1. 2.2 使用ARDUINO控制MC20发短信

    需要准备的硬件 MC20开发板 1个 https://item.taobao.com/item.htm?id=562661881042 GSM/GPRS天线 1根 https://item.taoba ...

  2. 0607pm克隆&引用类&加载类&面向对象串讲&函数重载

    克隆class Ren{ public $name; public $sex; function __construct($n,$s) { $this->name=$n; $this->s ...

  3. Github结合Eclipse出现的问题

    半年前因为学习Git花费了很长时间,半年过去了,因为不使用,基本全部忘记了,最近在公司需要使用Eclipse开发相关项目,用到前期的测试数据挖掘的小算法,又重拾Git,不过这次不再是命令行模式,而是结 ...

  4. pdoModel封装

    <?php /** * Created by PhpStorm. * User: Administrator * Date: 2017/7/24 * Time: 14:03 */ /** * 数 ...

  5. 【Flask】Sqlalchemy group_by having

    ### group_by:根据某个字段进行分组.比如想要根据性别进行分组,来统计每个分组分别有多少人 ### having: having是对查找结果进一步过滤.比如只想要看未成年人的数量,那么可以首 ...

  6. 快乐学习 Ionic Framework+PhoneGap 手册1-3 {面板切换}

    编程的快乐和乐趣,来自于能成功运行程序并运用到项目中,会在后面案例,实际运用到项目当中与数据更新一起说明 从面板切换开始,请看效果图和代码,这只是一个面板切换的效果 Index HTML Code & ...

  7. https网站无法加载http路径的js和css

    在https的网站中引用http路径的js或css会导致不起作用,其形如: <script src="http://code.jquery.com/jquery-1.11.0.min. ...

  8. ACM训练小结-2018年6月15日

    今天题目情况如下:A题:给出若干条边的边长,问这些边按顺序能否组成一个凸多边形,并求出这个多边形的最小包含圆.答题情况:无思路.正解(某种):第一问很简单.对第二问,如果R大于可行的最小R,那么按照放 ...

  9. (十三)linux文件系统详解(基于ext2文件系统)【转】

    本文转载自:https://blog.csdn.net/FadeFarAway/article/details/53959639 我们知道,一个磁盘可以划分成多个分区,每个分区必须先用格式化工具(例如 ...

  10. poj 1905 Expanding Rods(木杆的膨胀)【数学计算+二分枚举】

                                                                                                         ...