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. leetcode_1052. Grumpy Bookstore Owner

    1052. Grumpy Bookstore Owner https://leetcode.com/problems/grumpy-bookstore-owner/ 题意:每个时刻i会有custome ...

  2. python基础一 day14 生成器函数进阶

    def generator(): print(123) content = yield 1 print('=======',content) print(456) arg = yield 2 '''' ...

  3. 【Codeforces #228】Solutions

    http://codeforces.com/contest/389 重新把号刷到Div 1 准备ACM?(我这么菜还是玩玩算了……) 官方题解出的很快 Div2 A: 怎么做都行……随便找俩数减就可以 ...

  4. 打印两个有序链表的公共部分 【题目】 给定两个有序链表的头指针head1和head2,打印两个 链表的公共部分

    简单题 package my_basic.class_3; public class Code_10_PrintCommonPart { public static class Node{ int v ...

  5. windows10 下安装、配置、启动mysql

    下载mysql 可以自行去百度 或者 https://dev.mysql.com/downloads/mysql/5.7.html#downloads 解压mysql-5.7.26-winx64.zi ...

  6. glassfish配置中数据库密码加密方法

    glassfish配置中数据库密码加密方法 Glassfish中的数据库连接池需要使用密文保存数据库密码.如果不是,则可按如下方法可配置 通过Glassfish中的Alias实现,配置方法如下: 1. ...

  7. 什么是二维数组?二维遍历?Java二维数组制作图片迷宫 使用如鹏游戏引擎制作窗口界面 附带压缩包下载,解压后双击start.bat启动

    什么是二维数组? 数组当中放的还是数组 int [][] arr=new int[3][2]; 有3个小箱子,每个箱子2个格子. 看结果? int [][] arr=new int[3][2]; Sy ...

  8. Linux系统状态检测

    基于Red Hat Enterprise Linux 7.5 1.ifconfig ifconfig用于获取和配置网络接口的网络参数,格式为“ifconfig [网络设备] [参数]” 参数: add ...

  9. 【转发】【composer】composer 命令行介绍

    首页 入门 下载 安装包列表 中国镜像 命令行 你已经学会了如何使用命令行界面做一些事情.本章将向你介绍所有可用的命令. 为了从命令行获得帮助信息,请运行 composer 或者 composer l ...

  10. POJ:2406-Power Strings(寻找字符串循环节)

    Power Strings Time Limit: 3000MS Memory Limit: 65536K Description Given two strings a and b we defin ...