UVA11806-Cheerleaders(容斥原理+二进制)

来源:互联网 发布:看high类似软件 编辑:程序博客网 时间:2024/05/16 12:09
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 eld. 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 rst 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 rst 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个石子。有多少种方法?要求第一行,第一列,最后一行,最后一列必须有石子。

【题解】

利用容斥原理。可以转到求“第一行、第一列、最后一行、最后一列没有石子”的方案数。

枚举各个集合的组合时可以借助二进制进行枚举

1.第一种二进制枚举

#include<iostream>  #include<cstdio>  #include<cstring>  using namespace std;  int n,m,sec,k;  int C[510][510];  const int mod=1000007;  void pre()  {      memset(C,0,sizeof(C));      for(int i=0;i<=500;i++)      C[i][0]=1;        for(int i=1;i<=500;i++)       for(int j=1;j<=i;j++)       C[i][j]=(C[i-1][j]+C[i-1][j-1])%mod;  }  int main()  {      pre();      scanf("%d",&sec);      for(int z=1;z<=sec;z++)      {          scanf("%d%d%d",&n,&m,&k);          int ans=0;          for(int i=0;i<16;i++)          {              int b=0,r=n,c=m;              if(i&1){r--;b++;}              if(i&2){r--;b++;}              if(i&4){c--;b++;}              if(i&8){c--;b++;}                if(b%2==0)ans=(ans+C[r*c][k])%mod;              else ans=(ans+mod-C[r*c][k])%mod;          }            printf("Case %d: %d\n",z,ans);      }      return 0;  }  

 2.第二种二进制枚举

#include <iostream>#include <algorithm>#include <stdio.h>#include <string.h>using namespace std;typedef long long ll;const int mod=1000007;  //记得加等号int c[505][505];int main(){    memset(c,0,sizeof(c));    c[0][0]=1;    for(int i=0;i<=500;i++)    c[i][0]=c[i][i]=1;    for(int i=0;i<=500;i++)    for(int j=1;j<i;j++)   //不是j=0;j<=500;j++    c[i][j]=(c[i-1][j]+c[i-1][j-1])%mod;    int t,n,m,k;    scanf("%d",&t);    for(int cas=1;cas<=t;cas++)    {        printf("Case %d: ",cas);        scanf("%d%d%d",&n,&m,&k);  //输入别忘了        int sum=c[n*m][k];        for(int i=1;i<(1<<4);i++)        {            int tmp=1,flag=0,r=n,h=m;            for(int j=0;j<4;j++)            {                if(i&(1<<j))                {                    flag++;                    if(j==0||j==1)                    r--;                    else                    h--;                }            }            if(flag&1)            sum=(sum+mod-c[r*h][k])%mod; //c用过了            else            sum=(sum+mod+c[r*h][k])%mod;        }        printf("%d\n",sum);    }    return 0;}

 

0 0