• UVa11806


    Cheerleaders

    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.

    Sample Input

    Sample Output

    2

    2 2 1

    2 3 2

    Case 1: 0

    Case 2: 2


    Source


    Root :: AOAPC I: Beginning Algorithm Contests -- Training Guide (Rujia Liu) :: Chapter 2. Mathematics :: Counting :: Examples
    Root :: Prominent Problemsetters :: Shamim Hafiz

    题意:

           在m行n列的矩形网格中放置k个相同的石子,要求第一行、最后一行、第一列、最后一列都必须有石子,问一共有多少种放置的方法。

    输入:

           T组数据,每组都输入n、m、k(n、m的顺序在这道题中显得不重要)。

    输出:

           方法数除以1000007的余数。

    分析:

           由容斥原理,设第一行没有石子的方法数为A,最后一行没有石子的方法数为B,第一列没有石子的方法数为C,最后一列没有石子的方法数为D,不加任何限制而放置k个石子的总方法数为S。那么答案应该为S - (A U B U C U D)。用二进制的方式表示集合A、B、C、D的组合方式。0001表示在A中、0010表示在B中、0100表示在C中、1000表示在D中、0101表示在A和C中、0111表示在A、B和C中、…

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 using namespace std;
     5 #define MAX_K 500
     6 #define MAX_INDEX 20
     7 int n,m,k;
     8 int C[MAX_K + 1][MAX_K + 1];
     9 const int mod = 1000007;
    10 void pre(){ // 计算组合数
    11     memset(C,0,sizeof(C));
    12     for(int i = 0 ; i <= MAX_K ; i++) C[i][0] = 1;
    13     for(int i = 1 ; i <= MAX_K ; i++)for(int j = 1 ; j <= i ; j++)
    14         C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % mod;
    15 }
    16 int main(){
    17     pre();
    18     int T; scanf("%d",&T);
    19     for(int cnt = 1 ; cnt <= T ; cnt++){
    20         scanf("%d%d%d",&n,&m,&k);
    21         int ans = 0;
    22         for(int i = 0 ; i < 16 ; i++){ // 枚举16种搭配方式
    23             int b = 0,r = n,c = m; // b用来统计集合的个数,r、c是目前可以放置石子的行列数
    24             if(i & 1) r--,b++;
    25             if(i & 2) r--,b++;
    26             if(i & 4) c--,b++;
    27             if(i & 8) c--,b++;
    28             // 容斥原理,此时是减去这些情况,于是偶数加、奇数减。
    29             if(!(b & 1))ans = (ans + C[r * c][k]) % mod;
    30             else ans = (ans + mod - C[r * c][k]) % mod;
    31         }
    32         printf("Case %d: %d
    ",cnt,ans);
    33     }
    34     return 0;
    35 }
    View Code
  • 相关阅读:
    写代码容易,编程并不容易
    微信小程序教学第四章第二节(含视频):小程序中级实战教程:详情-视图渲染...
    微信小程序教学第四章第二节(含视频):小程序中级实战教程:详情-视图渲染...
    微信小程序教学第四章第二节(含视频):小程序中级实战教程:详情-视图渲染...
    微信小程序教学第四章第二节(含视频):小程序中级实战教程:详情-视图渲染...
    Git经典学习指南
    Git经典学习指南
    Git经典学习指南
    Git经典学习指南
    合伙做生意长久的原则
  • 原文地址:https://www.cnblogs.com/cyb123456/p/5803173.html
Copyright © 2020-2023  润新知