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. python2 和 pyhton3 输入语句写法

    Python的输入语句类型 1 python2的输入语句 在python2中有两种常见的输入语句,input()和raw_input(). (1)input()函数 可以接收不同类型的参数,而且返回的 ...

  2. 剑指offer 面试9题

    面试9题: 题目:用两个栈实现队列 题目描述:用两个栈来实现一个队列,完成队列的Push和Pop操作. 队列中的元素为int类型. 解题思路:有两个栈stackA,stackB,A为入栈,B为出栈的. ...

  3. Something haunts me in Python

    @1: 在查看"The Python Library Reference"(https://docs.python.org/2/library/stdtypes.html#sequ ...

  4. form 表单<input type="button" value="登录" onclick="loginSubmit ()"/> 点击提示 Uncaught TypeError: loginSubmit is not a function

    在网上搜了一堆东东,仔细看了一下,再加上实验,发现原因出在<form>中. <form method="post"> <button type=&qu ...

  5. This version of the rendering library is more recent than your version of ADT plug-in. Please update ADT plug-in问题

    点击 Help > Install New Software. 在 Work with 的输入区域里, 输入: https://dl-ssl.google.com/android/eclipse ...

  6. 解决PowerDesigner 反向工程没有注释(备注)

    1. 列注释 原来代码: {OWNER, TABLE, S, COLUMN, DTTPCODE, LENGTH, SIZE, PREC, COMPUTE, NOTNULL, IDENTITY, DOM ...

  7. SourceTree的基本使用---团队开发/参与开源

    1.实践入门-团队开发 如果你看到第二部分关于“参与开源”的内容,而你的需求是团队开发,你会发现几个不方便的地方: 1.1.组长建项目,组员每次提交,都需要组长审查同意merge 如果你觉得麻烦,组长 ...

  8. PAT 天梯赛 L2-015. 互评成绩 【排序】

    题目链接 https://www.patest.cn/contests/gplt/L2-015 思路 在求和的过程中 标记一下 最大值和最小值,在最后求平均的时候 用总和减去最大值和最小值 去除 (总 ...

  9. hostname -f 失败解决办法

    $ hostname fzk $ uname -n fzk 当 hostname -f 时报错:未搜索到主机名 产生这个原因时因为  /etc/hosts和/etc/sysconfig/network ...

  10. MySQL索引操作命令详解

    创建索引: MySql创建索引的语法如下: CREATE [UNIQUE|FULLTEXT|SPATIAL] INDEX index_name [USING index_type] ON table_ ...