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.
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
2
2 2 1
2 3 2
Sample Output
Case 1: 0
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.
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
2
2 2 1
2 3 2
Sample Output
Case 1: 0
Case 2: 2
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
DP+容斥原理~
今天才意识到C[i][j]的递推求法是DP诶,好神奇啊~
先预处理出C[i][j],然后对于每次输入,用容斥原理去掉四边中有空的种类数,具体就是减去空一条边的,加上空两条边的,减去空三条边的,加上空四条边的。
完全没必要用二进制啊。
WA了无数次,还以为又是被取模坑的,后来发现原来是因为输出的Case后面没有#……为什么偏偏就你没有#啊!!!
(所以应该是没必要取那么多次模的。)
(写完才发现我的取模全写成了取膜……我已经走火入膜了……)
#include<cstdio>
#define modd 1000007
int t,n,m,num,f[501][501],totnumans;
int main()
{
for(int i=0;i<=500;i++) f[i][0]=f[i][i]=1;
for(int i=1;i<=500;i++)
for(int j=1;j<i;j++) f[i][j]=(f[i-1][j]+f[i-1][j-1])%modd;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d",&n,&m,&num);
int now=(((f[n*m][num]-f[(n-1)*m][num]*2+modd*2)%modd)-f[n*(m-1)][num]*2+modd*2)%modd;
now=(now+f[(n-1)*(m-1)][num]*4+f[(n-2)*m][num]+f[n*(m-2)][num])%modd;
now=(((now-f[(n-1)*(m-2)][num]*2+modd*2)%modd)-f[(n-2)*(m-1)][num]*2+modd*2)%modd;
now=(now+f[(n-2)*(m-2)][num])%modd;
printf("Case %d: %d\n",++totnumans,now);
}
return 0;
}