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 2 1
2 3 2
Sample Output
Case 1: 0
Case 2: 2

题意:给定n*m的棋盘和k个一样的女孩,最上面和最下面一行,最左边和最右边一列至少有一个女孩,问有多少中方案数。

题解:设最上面一行不放石头的方案为集合A,最下面一行不放的方案为集合B,最左边不放的方案为集合C,最右边放的方案为集合D,全集为S。那么答案就是|S|-|A∪B∪C∪D|,求|A∪B∪C∪D|直接用容斥原理就好。

至于求ABCD,  我们设定dp[i][j]表示  i个格子放了j个女孩的方案数   dp[i][j] = dp[i-1][j-1]+dp[i-1][j];

//meek
///#include<bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <sstream>
#include <queue>
using namespace std ;
typedef long long ll;
#define mem(a) memset(a,0,sizeof(a))
#define pb push_back
#define fi first
#define se second
#define MP make_pair
inline ll read()
{
ll x=,f=;
char ch=getchar();
while(ch<''||ch>'')
{
if(ch=='-')f=-;
ch=getchar();
}
while(ch>=''&&ch<='')
{
x=x*+ch-'';
ch=getchar();
}
return x*f;
}
//**************************************** const int N=+;
const ll INF = 1ll<<;
const int inf = ;
const int MOD= ; int n,k,m;
ll dp[N][N];
int solve() {
int ans = dp[n*m][k]%MOD;
for(int i=;i<(<<);i++) {
int a = n, b = m, cnt = ;
if(i&(<<)) cnt++,a--;
if(i&(<<)) cnt++,a--;
if(i&(<<)) cnt++,b--;
if(i&(<<)) cnt++,b--;
if(cnt&) ans = (ans - dp[a*b][k]+MOD)%MOD;
else ans = (ans + dp[a*b][k]+MOD)%MOD;
}
return ans%MOD;
}
void init() {
for(int i=;i<=;i++) dp[i][i] = , dp[i][] = ;
for(int i=;i<=;i++) {
for(int j=;j<i;j++) {
dp[i][j] = dp[i-][j] + dp[i-][j-];
dp[i][j] %= MOD;
}
}
}
int main() {
int T, cas = ;
init();
scanf("%d",&T);
while(T--) {
scanf("%d%d%d",&n,&m,&k);
printf("Case %d: %d\n",cas++,solve());
}
return ;
}

代码

UVA 11806 Cheerleaders dp+容斥的更多相关文章

  1. uva 11806 Cheerleaders (容斥)

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

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

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

  3. uva 11806 Cheerleaders

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

  4. bzoj 3622 DP + 容斥

    LINK 题意:给出n,k,有a,b两种值,a和b间互相配对,求$a>b$的配对组数-b>a的配对组数恰好等于k的情况有多少种. 思路:粗看会想这是道容斥组合题,但关键在于如何得到每个a[ ...

  5. 【BZOJ 4665】 4665: 小w的喜糖 (DP+容斥)

    4665: 小w的喜糖 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 94  Solved: 53 Description 废话不多说,反正小w要发喜 ...

  6. [Luogu P1450] [HAOI2008]硬币购物 背包DP+容斥

    题面 传送门:https://www.luogu.org/problemnew/show/P1450 Solution 这是一道很有意思的在背包里面做容斥的题目. 首先,我们可以很轻松地想到暴力做背包 ...

  7. HDU 5838 (状压DP+容斥)

    Problem Mountain 题目大意 给定一张n*m的地图,由 . 和 X 组成.要求给每个点一个1~n*m的数字(每个点不同),使得编号为X的点小于其周围的点,编号为.的点至少大于一个其周围的 ...

  8. Codeforces 611C New Year and Domino DP+容斥

    "#"代表不能放骨牌的地方,"."是可以放 500*500的矩阵,q次询问 开两个dp数组,a,b,a统计横着放的方案数,b表示竖着放,然后询问时O(1)的,容 ...

  9. [BZOJ 1042] [HAOI2008] 硬币购物 【DP + 容斥】

    题目链接:BZOJ - 1042 题目分析 首先 Orz Hzwer ,代码题解都是看的他的 blog. 这道题首先使用DP预处理,先求出,在不考虑每种硬币个数的限制的情况下,每个钱数有多少种拼凑方案 ...

随机推荐

  1. 关于asp.net和iis的进程/线程问题,假如网站有1000个人访问,会产生多少个进程/线程啊

    详解 ASP.NET异步   超好的文章

  2. iOS学习之C语言函数指针

    通过函数名调用函数: int max = maxValue(4, 5); printf("max = %d\n", max);     函数类型:int (int, int) 1. ...

  3. python 安装scrapy

    1. 首先你先得安装PYTHON...还是推荐2.7吧,之前装了3.3似乎和这个世界格格不入...先装个2.7. 并将python加入系统的环境变量. 2. 去scrapy 官网下载最新版本的scra ...

  4. Linux 删除mysql数据库失败的解决方法

    使用命令:drop database xxx:删除本数据库时却删除失败,系统提示出现了错误,错误代码为: ERROR 1010 (HY000): Error dropping database(can ...

  5. iTween基础之CameraFade(摄像机淡入淡出)

    一.基础介绍:二.基础属性 原文地址: http://blog.csdn.net/dingkun520wy/article/details/50896420 一.基础介绍 CameraTexture: ...

  6. [网络配置相关]——ifconfig命令、ip命令、route命令

    ifconfig命令 1. 查看已被激活的网卡的详细信息 # ifconfig eth0 Link encap:Ethernet HWaddr 00:30:67:F2:10:CF inet addr: ...

  7. 20145129 《Java程序设计》第5周学习总结

    20145129 <Java程序设计>第5周学习总结 教材学习内容总结 语法与继承架构 使用try.catch Java中所有错误都会被打包为对象,可以尝试(try)捕捉(catch)代表 ...

  8. SQL Server 中使用 convert 转换 datetime 格式示例

    Select CONVERT(varchar(100), GETDATE(), 0): 05 16 2006 10:57AMSelect CONVERT(varchar(100), GETDATE() ...

  9. 低功耗(LPS)VLSI 电路设计中的一些注意事项

    1. Non-Retention RAM/Register: 如果使用的RAM/Register本身在Power-Down状态下不可以保存自身的状态,那么一定要保证它们在power-up的时候会进行r ...

  10. selenium--嵌套frame定位

    网页源码: 案例1 :iframe有id.name属性 网页上有3个frame:header.menu.main,分别代码顶部.左侧.右侧(其中menu.main在另外一个frameset中) 如何定 ...