UVA11806-Cheerleaders(容斥原理+二进制)
2016-04-03 16:45
260 查看
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.第一种二进制枚举
2.第二种二进制枚举
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 <cstring> #include <cstdio> using namespace std; const int mod=1e6+7; //记得加等号 int c[505][505]; void get() //求组合数模板 注意细节问题 { 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() { get(); int t,cas=1; cin>>t; while(t--) { int n,m,k; cin>>n>>m>>k;//输入别忘了 int sum=0; for(int i=0;i<(1<<4);i++) { int 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-c[r*h][k]+mod)%mod; //c用过了 用h else sum=(sum+c[r*h][k]+mod)%mod; } printf("Case %d: %d\n",cas++,sum); } }
相关文章推荐
- mybatis配置文件
- DLUTOJ 1209 字典序和r-子集
- java如何得到GET和POST请求URL和参数列表
- canvas 橡皮擦效果制作
- 排序算法杂记
- 【Java】豆机
- 最小的k个数
- iOS性能优化:Instruments使用实战
- 利用NSCache提升效率
- HDU1695 GCD (欧拉函数+容斥原理)
- Java回调机制解析
- Maven+Spring+SpringMVC+MyBatis框架的搭建(二)
- java 学习心得
- Sublime Text常用快捷键总结附上gif图 更加清晰
- linux下如何检测程序是否运行并重启
- NSoperation和GCD的使用场景
- VJ【背包】
- Fiddler 使用教程
- LightOJ 1138 - Trailing Zeroes (III) (求末尾0为x的最小N---二分)
- hdoj 小兔的棋盘 2067 (DP)