UVA 11806 Cheerleaders

原创 2016年08月29日 15:28:01

Question:
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
题意大意:一个方阵要四个边界都要有人站(即最上边最下边最左边最右边)
思路:利用容斥定理,求A,B,C,D四边没人站
(http://acm.hust.edu.cn/vjudge/contest/121560#problem/C)

#include <iostream>
#include <cstdio>
using namespace std;
const int MOD=1000007;
typedef long long LL;
LL f[405][405];
int main()
{
    for(int i=0;i<=400;i++)
    {
        f[i][i]=f[i][0]=1;
        for(int j=1;j<i;j++)
            f[i][j]=(f[i-1][j-1]+f[i-1][j])%MOD;  //打表,打印杨辉三角
    }
    int T,n,m,k;
    scanf("%d",&T);
    for(int i=1;i<=T;i++)
    {
        scanf("%d%d%d",&n,&m,&k);
         LL way=0;
        for(int j=0;j<=15;j++)
        {
            int t=0,l=n,c=m ;
            if(j&1)      //充分利用位运算求p(a)+p(b)+p(c)......-p(abcd)
            {t++;l--;}
            if(j&2)
            {t++;c--;}
            if(j&4)
            {t++;l--;}
            if(j&8)
            {t++;c--;}
            if(t%2==0)  //原本偶数应该被减,但现在是总数减去,所以偶数被加,奇数被减
                way=(way+f[c*l][k])%MOD; 
            else way=(way+MOD-f[c*l][k])%MOD; //减法求MOD
        }
        printf("Case %d: %lld\n",i,way);
    }
    return 0;
}

体会:遇见复杂问题不易直接求解的可以换个思路,利用容斥定理,同时要灵活运用位运算。

版权声明:本文为博主原创文章,未经博主允许不得转载。

UVa 11806 Cheerleaders 【容斥】【入门】

题目链接:UVa 11806 Cheerleaders Description: In most professional sporting events, cheerleaders play a...
  • Young_12138
  • Young_12138
  • 2017年07月29日 11:47
  • 189

Cheerleaders UVA - 11806(容斥原理)题解

Cheerleaders UVA - 11806(容斥原理)题解
  • hsj970319
  • hsj970319
  • 2017年03月11日 11:06
  • 120

uva 11806 Cheerleaders 容斥原理+组合数

题目传送门:点击打开链接 题目要求 矩形四个边都必须至少坐上一个人,并且 坐在矩形的一个角算相邻的两条边都有人坐,问有几种做法? 我们肯定答案为0的情况:1.当k 下面 我们发现 符合题意的坐法...
  • S1766434333
  • S1766434333
  • 2016年11月08日 23:15
  • 100

UVA 11806 Cheerleaders(容斥原理)(组合数)

参考了两篇博客,并摘抄了里面的部分解释 AOQNRMGYXLMV Yan_Bin     此题目是用的容斥原理,设第一行没有石子的方法数为A,最后一行没有石子的方法数为B,第一列没有石子的方法数...
  • huatian5
  • huatian5
  • 2016年08月27日 10:03
  • 578

UVA 11806(B)——Cheerleaders 容斥原理 离散数学列举所有情况

题目链接:点击打开链接 题目大意:输入m,n,k,在m行n列的矩阵中放k名啦啦队员,要求四条边框每一个至少都要有一名啦啦队员,问有多少中放法 解题思路:直接放的话会很麻烦,所以可以从它的反方向去考虑问...
  • WSS_ang
  • WSS_ang
  • 2017年07月28日 00:14
  • 169

UVa 11806 - Cheerleaders (组合数学 容斥原理)

Cheerleaders Time Limit: 2000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu ...
  • zhaosdfa
  • zhaosdfa
  • 2014年08月15日 13:39
  • 311

UVA 11806 Cheerleaders (容斥原理+二进制枚举)

UVA 11806 Cheerleaders (容斥原理+二进制枚举) :http://acm.hust.edu.cn/vjudge/contest/view.action?cid=110771#pr...
  • PNAN222
  • PNAN222
  • 2016年04月02日 21:48
  • 301

UVA 11806 Cheerleaders(容斥原理 + 组合数)

传送门 In most professional sporting events, cheerleaders play a major role in entertaining the specta...
  • qingshui23
  • qingshui23
  • 2016年10月31日 16:40
  • 668

UVa 11806 Cheerleaders (组合&逆向思维||容斥定理)

【题意】 在一个n*m的区域内放k个棋子,第一排,最后一排,第一列,最后一列一定要放,求一共有多少种方法。 【思路】 正着想重复的情况太多,不妨反着思考。 设ai表示有且仅有i条边上没有放的情况数,...
  • synapse7
  • synapse7
  • 2013年09月20日 16:26
  • 1093

容斥+二进制UVA 11806

问题是求四个边都有队员的排列组合,根据容斥原理可以转化为对立问题的求解: 全集为S=C(n*m,k), 该区域有k个队员的全部排列组合,|A|=最左边没有队员的全部种数,|B|=最右边没有,|C|=最...
  • oba_gaga
  • oba_gaga
  • 2016年04月24日 20:34
  • 140
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:UVA 11806 Cheerleaders
举报原因:
原因补充:

(最多只允许输入30个字)