UVA 11806 Cheerleaders dp+容斥
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+容斥的更多相关文章
- uva 11806 Cheerleaders (容斥)
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- UVA.11806 Cheerleaders (组合数学 容斥原理 二进制枚举)
UVA.11806 Cheerleaders (组合数学 容斥原理 二进制枚举) 题意分析 给出n*m的矩形格子,给出k个点,每个格子里面可以放一个点.现在要求格子的最外围一圈的每行每列,至少要放一个 ...
- uva 11806 Cheerleaders
// uva 11806 Cheerleaders // // 题目大意: // // 给你n * m的矩形格子,要求放k个相同的石子,使得矩形的第一行 // 第一列,最后一行,最后一列都必须有石子. ...
- bzoj 3622 DP + 容斥
LINK 题意:给出n,k,有a,b两种值,a和b间互相配对,求$a>b$的配对组数-b>a的配对组数恰好等于k的情况有多少种. 思路:粗看会想这是道容斥组合题,但关键在于如何得到每个a[ ...
- 【BZOJ 4665】 4665: 小w的喜糖 (DP+容斥)
4665: 小w的喜糖 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 94 Solved: 53 Description 废话不多说,反正小w要发喜 ...
- [Luogu P1450] [HAOI2008]硬币购物 背包DP+容斥
题面 传送门:https://www.luogu.org/problemnew/show/P1450 Solution 这是一道很有意思的在背包里面做容斥的题目. 首先,我们可以很轻松地想到暴力做背包 ...
- HDU 5838 (状压DP+容斥)
Problem Mountain 题目大意 给定一张n*m的地图,由 . 和 X 组成.要求给每个点一个1~n*m的数字(每个点不同),使得编号为X的点小于其周围的点,编号为.的点至少大于一个其周围的 ...
- Codeforces 611C New Year and Domino DP+容斥
"#"代表不能放骨牌的地方,"."是可以放 500*500的矩阵,q次询问 开两个dp数组,a,b,a统计横着放的方案数,b表示竖着放,然后询问时O(1)的,容 ...
- [BZOJ 1042] [HAOI2008] 硬币购物 【DP + 容斥】
题目链接:BZOJ - 1042 题目分析 首先 Orz Hzwer ,代码题解都是看的他的 blog. 这道题首先使用DP预处理,先求出,在不考虑每种硬币个数的限制的情况下,每个钱数有多少种拼凑方案 ...
随机推荐
- iOS代码实践总结
转载地址:http://mobile.51cto.com/hot-492236.htm 最近一个月除了专门抽时间和精力重构之外,还有就是遇到需要添加功能的模块的时候,由于项目中的代码历史因素比较多,第 ...
- 魔兽世界私服Trinity,从源码开始
缘起因由 在一个无所事事的周末下午,突然想起魔兽世界,官方的账号很久没有上了,里面的大小号现在连满级都不是.以前曾经搭过传奇和星际争霸战网的私服自娱自乐,也听说过魔兽世界有开源的服务端模拟,既然兴致来 ...
- 史上最佳 Mac+PhpStorm+XAMPP+Xdebug 集成开发和断点调试环境的配置
在上一篇 PHP 系列的文章<PHP 集成开发环境比较>中,我根据自己的亲身体验,非常简略的介绍和对比了几款常用的集成开发环境,就我个人而言,比较推崇 Zend Studio 和 PhpS ...
- exception -----> Functions
/* current_exception */ exception_ptr current_exception() noexcept; 返回指向当前异常(或其副本)的智能指针[具体返回对象本身还是副本 ...
- Mysql的相关命令
1.登录服务器 mysql -h host -u user -p mysql -h host -u user -p 数据库 2.使用SHOW语句找出服务器上当前存在什么数据库:mysql> SH ...
- Lua利用cjson读写json示例分享
本文结合本人的实际使用经验和代码示例,介绍如何在Lua中对json进行encode和decode,需要的朋友可以参考下 我这里采用的是Lua CJson库,是一个高性能的JSON解析器和编码器,其性能 ...
- linux查看有哪些shell可用
1.命令cat /etc/shells [tansheng@localhost ~]$ cat /etc/shells /bin/sh /bin/bash /sbin/nologin /bin/das ...
- USB硬件远程共享解决iphone已停用
悲剧的在iphone拆过电池之后,再开机显示iphone已停用,请在23000000分钟后再试一次 算算这得45年了,可以留给孙子用了... 网上除了刷机和有同步过的电脑貌似没有别的办法了 因是旧系统 ...
- 大作业NABC分析结果
大作业NABC分析结果 这次的大作业计划制作一款关于七巧板的游戏软件.关于编写的APP的NABC需求分析: N:需求 ,本款软件主要面向一些在校的大学生,他们在校空闲时间比较多,而且热衷于一些益智类游 ...
- PSP记录表
学生 崔乐乐 日期 2015/3/15 教师 王建民 课程 软件工程 周活动总结表 任务 日期 听课 写程序 ...