读书人

UVa 11806 Cheerleaders (结合amp;逆向思

发布时间: 2013-09-25 11:02:58 作者: rapoo

UVa 11806 Cheerleaders (组合&逆向思维)

11806 - Cheerleaders

Time limit: 2.000 seconds

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2906

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.

UVa 11806 Cheerleaders (结合&逆向思维)

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

Sample Output

2

2 2 1

2 3 2

Case 1: 0

Case 2: 2


【题意】

在一个n*m的区域内放k个棋子,第一排,最后一排,第一列,最后一列一定要放,求一共有多少种方法。


【思路】

正着想重复的情况太多,不妨反着思考。

设ai表示有且仅有i条边上没有放的情况数,我们想要的显然是a1+a2+a3+a4,这就是所有不符合要求的情况,但是同样不好直接计算。

但是有一个方便计算的:设si表示i条边上不能放,而其他的地方随便放的情况数,

则有

UVa 11806 Cheerleaders (结合&逆向思维)

同时

UVa 11806 Cheerleaders (结合&逆向思维)

解得

a1+a2+a3+a4 = s1-s2+s3-s4


【完整代码】

/*0.012s*/#include<cstdio>const int mod = 1000007;int c[405][405];int main(void){///生成组合数c[0][0] = 1;for (int i = 1; i < 405; i++){c[i][0] = c[i][i] = 1;for (int j = 1; j < i; j++)c[i][j] = (c[i - 1][j - 1] + c[i - 1][j]) % mod;}///endint t, cas = 0, n, m, k, ans, s1, s2, s3, s4;scanf("%d", &t);while (t--){scanf("%d%d%d", &n, &m, &k);if (n * m < k) ans = 0;else{s1 = (c[n * m - m][k] + c[n * m - n][k]) << 1;s2 = c[n * m - m - m][k] + c[n * m - n - n][k] + (c[n * m - n - m + 1][k] << 2);s3 = (c[n * m - n - m - m + 2][k]  + c[n * m - m - n - n + 2][k]) << 1;s4 = c[n * m - m - m - n - n + 4][k];ans = ((c[n * m][k] - s1 + s2 - s3 + s4) % mod + mod) % mod;}printf("Case %d: %d\n", ++cas, ans);}return 0;}

读书人网 >编程

热点推荐