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

2

2 3 1

2 3 2

Sample Output

Case 1: 0

Case 2: 2

题目大意:在一个n*m的网格放k个想同的石子,每个格子最多放一块,都要放玩,求第一行、最后一行、第一列、最后一列、都得有石子的种数

直接求太麻烦了,运用容斥原理设集合S、A(第一行没有石子)、B(第一列没有石子)、C(最后一行没有石子)、D(最后一列没有石子)

设题目要求的结合为E(第一行、最后一行、第一列、最后一列、都得有石子)

设A'为A的补集,设A&B为A跟B的交集(数学符号懒得找,就用这种表示了)

那么根据容斥原理

E=A'&B'&C'&D'=S-(A+B+C+D)+(A&B+A&C+A&D+B&C+B&D+C&D)-(A&B&C+A&B&D+A&C&D+B&C&D)+A&B&C&D

用dfs做容斥原理

AC代码:

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#define Max 550
#define MOD 1000007
int c[Max][Max];
int map[]={,,,,};
int vis[];
int n,m,k,num,temp; void Init()
{
memset(c,,sizeof(c));
int i,j;
for(i=;i<Max;i++)
{
for(j=;j<=i;j++)
{
if(j==)
c[i][j]=;
else
c[i][j]=(c[i-][j]+c[i-][j-])%MOD;
}
}
} void dfs(int now,int top,int start,int s)
{
int a,b;
a=n;
b=m;
if(now==top)
{
while(s%)
{
if((s%)%)
a--;
else
b--;
s/=;
}
num=(num+MOD+temp*c[a*b][k])%MOD;
return ;
}
for(int j=start;j<=;j++)
{
if(!vis[j])
{
vis[j]=true;
int s1=s*+j;
dfs(now+,top,j+,s1);
vis[j]=false;
}
}
return ;
}
int main()
{
Init();
int t,sum,i,Case;
cin>>t;
Case=;
while(t--)
{
Case++;
cin>>n>>m>>k;
sum=c[n*m][k];
memset(vis,false,sizeof(vis));
for(i=;i<=;i++)
{
num=;
temp=(i%?-:);
dfs(,i,,);
sum=(sum+MOD+num)%MOD;
}
printf("Case %d: %d\n",Case,sum);
}
return ;
}
 

uva 11806 容斥原理+dfs的更多相关文章

  1. UVA.11806 Cheerleaders (组合数学 容斥原理 二进制枚举)

    UVA.11806 Cheerleaders (组合数学 容斥原理 二进制枚举) 题意分析 给出n*m的矩形格子,给出k个点,每个格子里面可以放一个点.现在要求格子的最外围一圈的每行每列,至少要放一个 ...

  2. uva 11806 Cheerleaders

    // uva 11806 Cheerleaders // // 题目大意: // // 给你n * m的矩形格子,要求放k个相同的石子,使得矩形的第一行 // 第一列,最后一行,最后一列都必须有石子. ...

  3. UVA 11806 Cheerleaders (组合+容斥原理)

    自己写的代码: #include <iostream> #include <stdio.h> #include <string.h> /* 题意:相当于在一个m*n ...

  4. UVA 11806 Cheerleaders (容斥原理)

    题意 一个n*m的区域内,放k个啦啦队员,第一行,最后一行,第一列,最后一列一定要放,一共有多少种方法. 思路 设A1表示第一行放,A2表示最后一行放,A3表示第一列放,A4表示最后一列放,则要求|A ...

  5. UVA 11806 Cheerleaders (容斥原理

    1.题意描述 本题大致意思是讲:给定一个广场,把它分为M行N列的正方形小框.现在给定有K个拉拉队员,每一个拉拉队员需要站在小框内进行表演.但是表演过程中有如下要求: (1)每一个小框只能站立一个拉拉队 ...

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

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

  7. UVa 11806 Cheerleaders (数论容斥原理)

    题意:给定一个n*m的棋盘,要放k个石子,要求第一行,最后一行,第一列,最后一列都有石子,问有多少种放法. 析:容斥原理,集合A是第一行没有石子,集合B是最后一行没有石子,集合C是第一列没有石子,集合 ...

  8. UVa 11806 Cheerleaders (容斥原理+二进制表示状态)

    In most professional sporting events, cheerleaders play a major role in entertaining the spectators. ...

  9. UVA - 11806 Cheerleaders (容斥原理)

    题意:在N*M个方格中放K个点,要求第一行,第一列,最后一行,最后一列必须放,问有多少种方法. 分析: 1.集合A,B,C,D分别代表第一行,第一列,最后一行,最后一列放. 则这四行必须放=随便放C[ ...

随机推荐

  1. 使用Kubernetes里的job计算圆周率后2000位

    使用Kubernetes里的job(作业),我们可以很方便地执行一些比较耗时的操作. 新建一个job.ymal文件: 定义了一个Kubernetes job,名称为pi,类型为job,容器名称为pi, ...

  2. vue实现微信分享朋友圈和朋友功能

    vue实现微信分享朋友圈和朋友功能 A-A+ haibao  2018-10-25  11  21  6.2 k  百度已收录  前端开发 温馨提示:本文共3536个字,读完预计9分钟. 这两天在开发 ...

  3. R文件报错的原因

    一般R文件报错,无非是资源文件错误,图片命名错误,但是编译都会报错,可以很快解决.但是前几天,引入一个第三方aar包后,项目编译正确,但是就是R文件报错,找不到R文件,整个项目一片报红. 1)首先编译 ...

  4. 移动端:active伪类无效的解决方法

    :active伪类常用于设定点击状态下或其他被激活状态下一个链接的样式.最常用于锚点<a href="#">这种情况,一般主流浏览器下也支持其他元素,如button等. ...

  5. 关于jQuery中的$发生冲突及解决方案

    问题描述: 在Jquery库中,$是JQuery的别名,所有使用$的地方也都可以使用JQuery来替换,如$('#msg')等同于JQuery('#msg')的写法. 当引入多个js库后,其它的js库 ...

  6. iOS UI 设计

    优设 http://www.uisdc.com Sketch http://www.sketchcn.com

  7. Ruby设计模式-观察者模式学习笔记

    observer.rb #!/bin/env ruby # encoding: utf-8 require 'observer' class CriminalMovement include Obse ...

  8. proto_ops和proto区别

    **proto_ops:**该结构体包含了某个特定的"protocol family"的一系列functions. 其包含的functions和struct proto很类似,但是 ...

  9. JavaScript内建对象-String

    JavaScript中通过双引号或单引号界定一个字符串. String对象只有一个属性:length属性,得到字符串的长度. 处理字符串本身的方法 charAt(index) 返回字符串中index指 ...

  10. URI URL URN 关系

    我们一起来看下面这个虚构的例子.这是一个URI: http://bitpoetry.io/posts/hello.html#intro 我们开始分析 http:// 是定义如何访问资源的方式.另外 b ...