码迷,mamicode.com
首页 > 其他好文 > 详细

uva11806(容斥原理)

时间:2014-08-26 00:26:15      阅读:265      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   os   io   strong   for   

11806 - Cheerleaders

Time limit: 2.000 seconds

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.

 bubuko.com,布布扣

 

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

Sample Output

2

2 2 1

2 3 2

Case 1: 0

Case 2: 2

 

 

直接分类讨论所有情况很麻烦,容易出错。那么正难则反,用容斥原理做就很简单了。

思路很好想,关键是写法。我觉得还是大白书上的用二进制来表示状态的写法最简明易懂。

我学习着写了一发,AC了。但是我觉得原书每次计算ans时没有考虑当少了几行或几列有可能不够把k个都放进去了,我加了个判断语句,当然对于uva上的数据,不加这个判断也是可以AC的。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#include<stack>
#include<queue>
using namespace std;
#define INF 1000000000
#define eps 1e-8
#define pii pair<int,int>
#define LL long long int
int T,n,m,k,ans;
const int mod=1000007;
int C[505][505];
int main()
{
    //freopen("in6.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    for(int i=0;i<=500;i++) C[i][0]=C[i][i]=1;
    for(int i=2;i<=500;i++)
    {
        for(int j=1;j<=i-1;j++)
        {
            C[i][j]=(C[i-1][j]+C[i-1][j-1])%mod;
        }
    }
    scanf("%d",&T);
    for(int cas=1;cas<=T;cas++)
    {
        scanf("%d%d%d",&m,&n,&k);//m*n
        ans=0;
        for(int i=0;i<16;i++)
        {
            int row=m,col=n,b=0;
            if(i&1){row--;b++;}
            if(i&2){row--;b++;}
            if(i&4){col--;b++;}
            if(i&8){col--;b++;}
            if(b&1)//b is a odd.
            {
                if(col*row>=k)
                ans=(ans-C[col*row][k]+mod)%mod;
            }
            else
            {
                if(col*row>=k)
                ans=(ans+C[col*row][k])%mod;
            }
        }
        printf("Case %d: %d\n",cas,ans);
    }
    //fclose(stdin);
    //fclose(stdout);
    return 0;
}

 

uva11806(容斥原理)

标签:des   style   blog   http   color   os   io   strong   for   

原文地址:http://www.cnblogs.com/zywscq/p/3936159.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!