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. django目录下的路由系统和视图函数

    一.Django路由系统(url) 1.什么是路由系统 路由系统的本质是URL模式以及要为该URL模式调用的视图函数之间的一个映射表即不同的url路径对应的不同的函数,该路由系统是存放在全局配置文件u ...

  2. Yii2 注册表单验证规则 手机注册时候使用短信验证码

    public function rules() { return [ ['username', 'filter', 'filter' => 'trim'], ['username', 'requ ...

  3. IBM的SOA方法论之一——五个切入点和八个场景

    一.什么是SOA: 面向服务的体系结构(Service-Oriented Architecture,SOA)是一种 IT 体系结构风格,支持将您的业务转换为一组相互链接的服务或可重复业务任务,可在需要 ...

  4. $python正则表达式系列(5)——零宽断言

    本文主要总结了python正则零宽断言(zero-length-assertion)的一些常用用法. 1. 什么是零宽断言 有时候在使用正则表达式做匹配的时候,我们希望匹配一个字符串,这个字符串的前面 ...

  5. 纯代码编写qt登录界面(转)

    1. 新建Qt Widgets Application,项目名称为login1,在类信息页面保持类名和基类为MainWindow和QMainWindow不变,取消选择创建界面选项,如下图所示.     ...

  6. C++学习 之pair

    Pair类型概述 pair是一种模板类型,其中包含两个数据值,两个数据的类型可以不同,基本的定义如下: pair<int, string> a; 表示a中有两个类型,第一个元素是int型的 ...

  7. 根据Django后台的ajax大全

    一.什么是ajax 1.1 什么是JSON? AJAX(Asynchronous Javascript And XML)翻译成中文就是“异步Javascript和XML”.即使用Javascript语 ...

  8. 20145230《Java程序设计》第3周学习总结

    20145230 <Java程序设计> 第3周学习总结 教材学习内容总结 String s=new String();第四章我首先了解了CPU与内存的关系,栈与堆的关系.要产生对象必须先定 ...

  9. groupby和agg的使用

    先来看一段代码: 分析下groupby和agg的联合使用: reset_index()表示重新设置索引 agg传进来的统计特征: 按照A这一列作聚合,C这一列作统计 注意:df = df.groupb ...

  10. myeclipse下搭建hadoop2.7.3开发环境

    需要下载的文件:链接:http://pan.baidu.com/s/1i5yRyuh 密码:ms91 一  下载并编译  hadoop-eclipse-plugin-2.7.3.jar 二  将had ...