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. 【转】jQuery插件之ajaxFileUpload

    转自:http://www.cnblogs.com/kissdodog/archive/2012/12/15/2819025.html 说在前头,本文出自上面的作者,只是以前存的一些网址不见了,怕以后 ...

  2. docker devise相关错误

    rake aborted!Devise.secret_key was not set. Please add the following to your Devise initializer: con ...

  3. ajax使用formdata 提交excel文件表单到rails解析

    .modal-body .container-fluid .row .col-md-12 1.下载模板文件 = link_to '模板文件' .row .col-md-12 = form_tag '' ...

  4. java 断言工具类

    1.断言工具类 package com.sze.redis.util; import java.util.Collection; import java.util.Map; import com.sz ...

  5. 【leetcode刷题笔记】Restore IP Addresses

    Given a string containing only digits, restore it by returning all possible valid IP address combina ...

  6. github使用——如何恢复被删去文件。

    首先git删除文件包括以下几种情况 删除本地文件,但是未添加到暂存区: 删除本地文件,并且把删除操作添加到了暂存区: 把暂存区的操作提交到了本地git库: 把本地git库的删除记录推送到了远程服务器g ...

  7. Django---Blog系统开发之注册页面(验证码&ajax发送文件)

    前端页面及渲染: 静态文件的配置:setting.py: static 文件放在app下 STATIC_URL = '/static/' STATIC_ROOT = ( os.path.join(BA ...

  8. 关于pycharm中的requirements.txt文件

    作用:记录所有所依赖的第三方模块,方便迁移到不同的环境中后,防止缺少模块,或因为所依赖的第三方模块版本不同引起不必要的问题 生成命令:pip freeze > requirements.txt ...

  9. Tooltip表单验证的注册表单

    在线演示 本地下载

  10. java string 细节原理分析(2016.5)

    看到了以前2016.5月学习java写的笔记,这里放在一起. String实现的细节原理分析 一.jdk源码中String 的实现 public final class String implemen ...