uva11806 Cheerleaders
来源:互联网 发布:mac无鼠标右键 编辑:程序博客网 时间:2024/04/29 20:30
Inmost professional sporting events, cheerleaders play a major role inentertaining the spectators. Their roles are substantial during breaks andprior to start of play. The world cup soccer is no exception. Usually thecheerleaders form a group and perform at the centre of the field. In additionto this group, some of them are placed outside the side line so they are closerto the spectators. The organizers would like to ensure that at least onecheerleader is located on each of the four sides. For this problem, we willmodel the playing ground as an M*Nrectangular grid. The constraints for placing cheerleaders are described below:
§ There should be at least one cheerleader oneach of the four sides. Note that, placing a cheerleader on a corner cell wouldcover two sides simultaneously.
§ There can be at most one cheerleader in acell.
§ All the cheerleaders available must beassigned to a cell. That is, none of them can be left out.
Theorganizers would like to know, how many ways they can place the cheerleaderswhile maintaining the above constraints. Two placements are different, if thereis at least one cell which contains a cheerleader in one of the placement butnot in the other.
Input
Thefirst line of input contains a positive integer T<=50, which denotes the number of test cases.T lines then follow each describing onetest case. Each case consists of three nonnegative integers,2<=M, N<=20 and K<=500. Here M is the number of rows andNis the number of columns in the grid.Kdenotes the number of cheerleaders that must be assigned to the cells in thegrid.
Output
For each case of input, there will be one line ofoutput. It will first contain the case number followed by the number of ways toplace the cheerleaders as described earlier. Look at the sample output forexact formatting. Note that, the numbers can be arbitrarily large. Thereforeyou must output the answers modulo 1000007.
Sample Input
Sample Output
2
2 2 1
2 3 2
Case 1: 0
Case 2: 2
解析
数学,用容斥原理。(参阅大白书2.1基本计数方法)每一项前面的正负号——奇数个集合为正,偶数个集合为负。
组合数可以先预处理:
Comb[0][0]=1;for(int i=1;i<=MAXK;i++){Comb[i][0]=Comb[i][i]=1;for(int j=1;j<i;j++) Comb[i][j]=(Comb[i-1][j-1]+Comb[i-1][j])%MOD;}代码
#include<cstdio>using namespace std;#define LOCALconst int MOD=1000007,MAXK=500;int Comb[MAXK+10][MAXK+10];void prework(){Comb[0][0]=1;for(int i=1;i<=MAXK;i++){Comb[i][0]=Comb[i][i]=1;for(int j=1;j<i;j++) Comb[i][j]=(Comb[i-1][j-1]+Comb[i-1][j])%MOD;}#ifdef LOCALfor(int i=1;i<=10;i++)for(int j=0;j<=i;j++)printf("%d%c",Comb[i][j],j!=i ? ' ' : '\n');#endif}void work(){int N,M,K,sum=0; scanf("%d%d%d",&N,&M,&K);for(int i=0;i< 1<<4;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&1) sum-=Comb[r*c][K];//奇数个集合为正else sum+=Comb[r*c][K];//偶数个集合为负sum=sum%MOD;/*if(b&1) sum = (sum+MOD-Comb[r*c][K])%MOD; else sum = (sum+Comb[r*c][K])%MOD;*/}printf("%d",(sum+MOD)%MOD);}int main(){#ifdef LOCALfreopen("uva11806.in","r",stdin);#endifprework();int T; scanf("%d",&T);for(int i=1;i<=T;i++){printf("Case %d: ",i);work();printf("\n");}#ifdef LOCALwhile(1);#endifreturn 0;}
- Cheerleaders UVA11806
- uva11806 Cheerleaders
- uva11806 - Cheerleaders
- uva11806 Cheerleaders
- 【Uva11806】Cheerleaders
- uva11806 - Cheerleaders 容斥原理
- UVa11806 Cheerleaders(容斥原理)
- UVA11806-Cheerleaders(容斥原理+二进制)
- 【UVA11806】Cheerleaders——容斥
- UVA11806-Cheerleaders(容斥原理+二进制)
- 容斥原理——Uva11806-Cheerleaders
- 20160331 UVA11806 Cheerleaders(组合数公式+容斥原理)
- uva11806
- UVa11806
- uva11806(数论)
- Cheerleaders UVA
- Cheerleaders UVA
- 例题2.3 拉拉队 UVa11806
- button点击切换背景图片的问题
- 开源爬虫Labin,Nutch,Neritrix介绍和对比
- MySQL分页的实现
- 用delphi实现类似[MPLAB里ICD3烧写操控界面]的调用外部cmd命令方式
- appfuse 部署到tomcat报错
- uva11806 Cheerleaders
- Android Studio 官网下载地址
- javascript实现修改微信分享的标题内容等
- IOS 如何选择delegate、notification、KVO?
- 常用的linux命令
- MySQL之外键约束
- kettle入门(四) 之kettle取昨天时间&设置任意时间变量案例
- Umeng的QQ登陆设置
- python 网络抓包