UVA 11806 Cheerleaders dp+容斥

时间:2022-12-22 07:58:12

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&period;11806 Cheerleaders &lpar;组合数学 容斥原理 二进制枚举&rpar;

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

  3. uva 11806 Cheerleaders

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

  4. bzoj 3622 DP &plus; 容斥

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

  5. 【BZOJ 4665】 4665&colon; 小w的喜糖 (DP&plus;容斥)

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

  6. &lbrack;Luogu P1450&rsqb; &lbrack;HAOI2008&rsqb;硬币购物 背包DP&plus;容斥

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

  7. HDU 5838 (状压DP&plus;容斥)

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

  8. Codeforces 611C New Year and Domino DP&plus;容斥

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

  9. &lbrack;BZOJ 1042&rsqb; &lbrack;HAOI2008&rsqb; 硬币购物 【DP &plus; 容斥】

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

随机推荐

  1. PHP变量作用域(花括号、global、闭包)

    花括号 很多语言都以花括号作为作用域界限,PHP中只有函数的花括号才构成新的作用域. <?php if (True) { $a = 'var a'; } var_dump($a); for ($ ...

  2. 《BI那点儿事》Microsoft 聚类分析算法——三国人物身份划分

    什么是聚类分析? 聚类分析属于探索性的数据分析方法.通常,我们利用聚类分析将看似无序的对象进行分组.归类,以达到更好地理解研究对象的目的.聚类结果要求组内对象相似性较高,组间对象相似性较低.在三国数据 ...

  3. &lbrack;译&rsqb; Python 3&period;5 协程究竟是个啥

    转自:http://blog.rainy.im/2016/03/10/how-the-heck-does-async-await-work-in-python-3-5/ [译] Python 3.5 ...

  4. 支持MVC的代码生成运行效果 C&num; ASP&period;NET

    做技术的,你若还不懂MVC的话,你好像是外星球来的一样,或者还生活在远古社会里一样,这几天正好没什么事情干,可以静心学习学习MVC技术,顺便把原先的代码生成器修改了一下,只要数据库里设计好了数据结构, ...

  5. Quartz Scheduler&lpar;2&period;2&period;1&rpar; - Working with TriggerListeners and JobListeners

    TriggerListeners and JobListeners Listeners are objects that you create to perform actions based on ...

  6. Swift - 使用导航条和导航条控制器来进行页面切换

    通过使用导航条(UINavigationBar)与导航条控制器(UINavigationController)可以方便的在主页面和多层子页面之间切换.下面通过一个简单“组件效果演示”的小例子来说明如何 ...

  7. 第21章 策略模式(Strategy Pattern)

    原文 第21章 策略模式(Strategy Pattern) 策略模式 导读:策略模式看完之后,大多数人都会感觉有点混了,包括我,感觉策略模式是一种OO思想的体现(纯属个人拙见). 概述:       ...

  8. 升级R版本后,更新Package

    升级R版本后,若重新安装所有的package将非常麻烦,可以尝试运行一下程序: 1)在旧版本中的R中运行 #--run in the old version of R setwd("C:/T ...

  9. 提取多层嵌套Json数据

    在.net 2.0中提取这样的json {"name":"lily","age":23,"addr":{"ci ...

  10. 【一天一道LeetCode】&num;76&period; Minimum Window Substring

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...