UVa 11806 Cheerleaders 【容斥】【入门】
来源:互联网 发布:淘宝店铺装修模块对齐 编辑:程序博客网 时间:2024/06/14 20:28
题目链接:UVa 11806 Cheerleaders
Description:
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 maintainingthe above constraints. Two placements are different, if there is at least one cell which contains acheerleader 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. Tlines 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 bythe number of ways to place the cheerleaders as described earlier. Look at the sample output for exactformatting. 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个人放置到这些格子上。要满足下列三个要求:
1.每个边界上的至少有一个人(处于两边界交点处<即四个角上>时,算是经过了两个边界)
2.每个格子上最多放一个人
3.这k个人必须都放置到格子上,即不允许有人空着
解题思路:
直接求不好求,那我们采取逆向思维。
首先如果没有限制条件的话,答案数就是从n*m个格子里选出k个来,答案就是C(n*m, k),然后减去边界行列不存在人的情况。这时如果记最上一行没人的情况为A,最下一行没人的情况为B,最左一列没人的情况为C,最右一列没人的情况为D。
则答案就是C(n*m, k) - A - B - C - D + AB + AC + AD + BC + BD + CD - ABC - ACD - ABD - BCD + ABCD
Mycode:
#include <bits/stdc++.h>using namespace std;typedef long long LL;const int MAX = 505;const int MOD = 1000007;const int INF = 0x3f3f3f3f;int t, n, m, k;LL C[MAX][MAX];void init(){ for(int i = 0; i < MAX; ++i) C[i][0] = C[i][i] = 1; for(int i = 1; i < MAX; ++i) { for(int j = 1; j < i; ++j) { C[i][j] = (C[i-1][j] + C[i-1][j-1]) % MOD; } }}int main(){ init(); int tot = 4; //共4组 scanf("%d",&t); for(int cas = 1; cas <= t; ++cas) { LL sum = 0; scanf("%d%d%d",&n,&m,&k); for(int i = 0; i < (1 << tot); ++i) { int row = m, col = n, bits = 0; for(int j = 0; j < tot; ++j) { if(i & (1 << j)) { bits++; if(j & 1) row--; else col--; } } if(bits & 1) sum -= C[row*col][k]; else sum += C[row*col][k]; sum = (sum + MOD) % MOD; //防止出现负数 } printf("Case %d: %lld\n",cas, sum); } return 0;}
- UVa 11806 Cheerleaders 【容斥】【入门】
- UVA 11806 - Cheerleaders (容斥原理)
- UVa 11806 Cheerleaders / 容斥原理
- uva 11806 - Cheerleaders(容斥原理)
- uva 11806 Cheerleaders(容斥)
- UVA 11806 Cheerleaders (容斥原理)
- uva 11806 Cheerleaders (容斥原理)
- uva 11806 Cheerleaders【容斥原理】
- uva 11806 - Cheerleaders(容斥原理+二进制)
- uva 11806 Cheerleaders 容斥原理
- UVA 11806 Cheerleaders(容斥原理)
- CheerLeaders Uva 11806(容斥原理)
- UVa 11806 Cheerleaders (组合&逆向思维||容斥定理)
- UVa 11806 Cheerleaders 解题报告(容斥原理)
- UVA 11806 - Cheerleaders(数论+容斥原理)
- UVa 11806 - Cheerleaders (组合数学 容斥原理)
- UVA 11806-Cheerleaders-容斥原理+组合数打表
- UVA 11806 Cheerleaders (容斥原理+二进制枚举)
- C++的单例模式
- 写Java代码的小技巧
- Session一致性配置---memcached
- C语言实现三字棋游戏
- Go语言的map以及sort
- UVa 11806 Cheerleaders 【容斥】【入门】
- UWP中Page的重用
- 解决UnicodeDecodeError: 'ascii' code can't decode byte 0xef in position
- 关联规则R语言实战(Apriori算法)
- ural1183&&poj1141 Brackets Sequence(区间DP+记录路径)
- Linux笔记汇总
- js实现ctrl+v粘贴上传图片(兼容chrome、firefox、ie11)
- 欢迎使用CSDN-markdown编辑器
- CF317D. Vessels(并查集)