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 center 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 m\times n 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 first line of input contains a positive integer T ≤ 50 T ≤ 50 T50, which denotes the number of test cases.
   T T T lines then follow each describing one test case. Each case consists of three nonnegative integers, 2 ≤ m , n ≤ 20 2 ≤ m,n ≤ 20 2m,n20 and k ≤ 500 k ≤ 500 k500. Here m m m is the number of rows and n n n is the number of columns in the grid. Kdenotes 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 1000007 1000007.

Sample Input

2
2 2 1
2 3 2

Sample Output

Case 1: 0
Case 2: 2

Idea

  直接考虑四个边界的放置情况较难,采用正难则反的策略。考虑第一行不放棋子,最后一行不放棋子,第一列不放棋子,最后一列不放棋子的情况。
  设 A A A 为第一行不妨棋子的方案集合, B B B 为最后一行不放棋子的方案集合, C C C 为第一列不放棋子的方案集合, D D D 为最后一列不放棋子的方案集合。在 m × n m\times n m×n 棋盘中放置 k k k 个棋子的总方案数为 ( m n k ) \begin{pmatrix}mn\\k\end{pmatrix} (mnk),则满足题目要求的方案数为 ( m n k ) − ∣ A ∪ B ∪ C ∪ D ∣ \begin{pmatrix}mn\\k\end{pmatrix}-|A\cup B\cup C\cup D| (mnk)ABCD
  根据容斥原理, ∣ A ∪ B ∪ C ∪ D ∣ = ∣ A ∣ + ∣ B ∣ + ∣ C ∣ + ∣ D ∣ − ∣ A ∩ B ∣ − ∣ A ∩ C ∣ − ∣ A ∩ D ∣ − ∣ B ∩ C ∣ − ∣ B ∩ D ∣ − ∣ C ∩ D ∣ + ∣ A ∩ B ∩ C ∣ + ∣ A ∩ B ∩ D ∣ + ∣ A ∩ C ∩ D ∣ + ∣ B ∩ C ∩ D ∣ − ∣ A ∩ B ∩ C ∩ D ∣ |A\cup B\cup C\cup D|=|A|+|B|+|C|+|D|-|A\cap B|-|A\cap C|-|A\cap D|-|B\cap C|-|B\cap D|-|C\cap D|+|A\cap B\cap C|+|A\cap B\cap D|+|A\cap C\cap D|+|B\cap C\cap D|-|A\cap B\cap C\cap D| ABCD=A+B+C+DABACADBCBDCD+ABC+ABD+ACD+BCDABCD
   A A A:第一行不放棋子, ∣ A ∣ = ( ( m − 1 ) n k ) |A|=\begin{pmatrix}(m-1)n\\k\end{pmatrix} A=((m1)nk)
   B B B:最后一行不放棋子, ∣ B ∣ = ( ( m − 1 ) n k ) |B|=\begin{pmatrix}(m-1)n\\k\end{pmatrix} B=((m1)nk)
   C C C:第一列不放棋子, ∣ C ∣ = ( m ( n − 1 ) k ) |C|=\begin{pmatrix}m(n-1)\\k\end{pmatrix} C=(m(n1)k)
   D D D:最后一列不放棋子, ∣ D ∣ = ( m ( n − 1 ) k ) |D|=\begin{pmatrix}m(n-1)\\k\end{pmatrix} D=(m(n1)k)
   A ∩ B A\cap B AB:第一行和最后一行都不放棋子, ∣ A ∩ B ∣ = ( ( m − 2 ) n k ) |A\cap B|=\begin{pmatrix}(m-2)n\\k\end{pmatrix} AB=((m2)nk)
   A ∩ C A\cap C AC:第一行和第一列都不放棋子, ∣ A ∩ C ∣ = ( ( m − 1 ) ( n − 1 ) k ) |A\cap C|=\begin{pmatrix}(m-1)(n-1)\\k\end{pmatrix} AC=((m1)(n1)k)
   A ∩ D A\cap D AD:第一行和最后一列都不放棋子, ∣ A ∩ D ∣ = ( ( m − 1 ) ( n − 1 ) k ) |A\cap D|=\begin{pmatrix}(m-1)(n-1)\\k\end{pmatrix} AD=((m1)(n1)k)
   B ∩ C B\cap C BC:最后一行和第一列都不放棋子, ∣ B ∩ C ∣ = ( ( m − 1 ) ( n − 1 ) k ) |B\cap C|=\begin{pmatrix}(m-1)(n-1)\\k\end{pmatrix} BC=((m1)(n1)k)
   B ∩ D B\cap D BD:最后一行和最后一列都不放棋子, ∣ B ∩ D ∣ = ( ( m − 1 ) ( n − 1 ) k ) |B\cap D|=\begin{pmatrix}(m-1)(n-1)\\k\end{pmatrix} BD=((m1)(n1)k)
   C ∩ D C\cap D CD,第一列和最后一列都不放棋子, ∣ C ∩ D ∣ = ( m ( n − 2 ) k ) |C\cap D|=\begin{pmatrix}m(n-2)\\k\end{pmatrix} CD=(m(n2)k)
   A ∩ B ∩ C A\cap B\cap C ABC,第一行、最后一行、第一列都不放棋子, ∣ A ∩ B ∩ C ∣ = ( ( m − 2 ) ( n − 1 ) k ) |A\cap B\cap C|=\begin{pmatrix}(m-2)(n-1)\\k\end{pmatrix} ABC=((m2)(n1)k)
   A ∩ B ∩ D A\cap B\cap D ABD,第一行、最后一行、最后一列都不放棋子, ∣ A ∩ B ∩ D ∣ = ( ( m − 2 ) ( n − 1 ) k ) |A\cap B\cap D|=\begin{pmatrix}(m-2)(n-1)\\k\end{pmatrix} ABD=((m2)(n1)k)
   A ∩ C ∩ D A\cap C\cap D ACD,第一行、第一列、最后一列都不放棋子, ∣ A ∩ C ∩ D ∣ = ( ( m − 1 ) ( n − 2 ) k ) |A\cap C\cap D|=\begin{pmatrix}(m-1)(n-2)\\k\end{pmatrix} ACD=((m1)(n2)k)
   B ∩ C ∩ D B\cap C\cap D BCD,最后一行、第一列、最后一列都不放棋子, ∣ B ∩ C ∩ D ∣ = ( ( m − 1 ) ( n − 2 ) k ) |B\cap C\cap D|=\begin{pmatrix}(m-1)(n-2)\\k\end{pmatrix} BCD=((m1)(n2)k)
   A ∩ B ∩ C ∩ D A\cap B\cap C\cap D ABCD:第一行、最后一行、第一列、最后一列都不放棋子, ∣ A ∩ B ∩ C ∩ D ∣ = ( ( m − 2 ) ( n − 2 ) k ) |A\cap B\cap C\cap D|=\begin{pmatrix}(m-2)(n-2)\\k\end{pmatrix} ABCD=((m2)(n2)k)
  综上所述,答案为 ( m n k ) − 2 × ( ( m − 1 ) n k ) − 2 × ( m ( n − 1 ) k ) + ( ( m − 2 ) n k ) + ( m ( n − 2 ) k ) + 4 × ( ( m − 1 ) ( n − 1 ) k ) − 2 × ( ( m − 2 ) ( n − 1 ) k ) − 2 × ( ( m − 1 ) ( n − 2 ) k ) + ( ( m − 2 ) ( n − 2 ) k ) \begin{pmatrix}mn\\k\end{pmatrix}-2\times \begin{pmatrix}(m-1)n\\k\end{pmatrix}-2\times \begin{pmatrix}m(n-1)\\k\end{pmatrix}+\begin{pmatrix}(m-2)n\\k\end{pmatrix}+\begin{pmatrix}m(n-2)\\k\end{pmatrix}+4\times \begin{pmatrix}(m-1)(n-1)\\k\end{pmatrix}-2\times \begin{pmatrix}(m-2)(n-1)\\k\end{pmatrix}-2\times \begin{pmatrix}(m-1)(n-2)\\k\end{pmatrix}+\begin{pmatrix}(m-2)(n-2)\\k\end{pmatrix} (mnk)2×((m1)nk)2×(m(n1)k)+((m2)nk)+(m(n2)k)+4×((m1)(n1)k)2×((m2)(n1)k)2×((m1)(n2)k)+((m2)(n2)k)

Code

#include <iostream>
using namespace std;
const int mod=1000007;
const int N=502;
int C[N][N];
int m,n,k;
void init()
{
	int i,j;
	for(i=0;i<N;i++)
	{
		C[i][0]=C[i][i]=1;
		for(j=0;j<i;j++)
		{
			C[i][j]=(C[i-1][j]+C[i-1][j-1])%mod;
		}
	}
}
int main()
{
	init();
	int _,CASE=0;
	for(cin>>_;_;_--)
	{
		scanf("%d%d%d",&m,&n,&k);
		printf("Case %d: %lld\n",++CASE,
		((C[m*n][k]
		-2*C[(m-1)*n][k]-2*C[m*(n-1)][k]
		+C[(m-2)*n][k]+C[m*(n-2)][k]+4*C[(m-1)*(n-1)][k]
		-2*C[(m-2)*(n-1)][k]-2*C[(m-1)*(n-2)][k]
		+C[(m-2)*(n-2)][k])%mod+mod)%mod);
	}
	return 0;
}

PostScript

  注意! 1000007 = 29 × 34483 1000007=29\times 34483 1000007=29×34483 1000007 1000007 1000007 不是质数。求 x x x 的逆元时,万万不可直接利用 i n v ( x ) ≡ x p − 2 ( m o d p ) \mathrm{inv}(x)\equiv x^{p-2}\pmod p inv(x)xp2(modp)。这题数据范围不大,老老实实打表吧。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值