24h購物| | PChome| 登入
2013-12-05 16:27:14| 人氣2,793| 回應0 | 上一篇 | 下一篇

[UVA][排容原理] 11806 - Cheerleaders

推薦 0 收藏 0 轉貼0 訂閱站台

C

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 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

Sample Output

2

2 2 1

2 3 2

Case 1: 0

Case 2: 2



題目描述:


在一個 n*m 個格子中,排放啦啦隊的隊形,總共有 k 個人可以放置。

規定第一行第一列、最後一行最後一列中,至少要存在 1 個人。

題目解法:


排容原理

(總數) -(不排第一行)
-(不排第一列)-(不排第最後一行)-(不排第最後一列)
+(不排第一行和第一列) ...

數學真是奧妙啊,寫起來如此短。

#include <stdio.h>
#include <string.h>
using namespace std;
int C[505][505] = {};
int main() {
    int i, j;
    C[0][0] = 1;
    for(i = 1; i < 505; i++) {
        C[i][0] = 1;
        for(j = 1; j <= i; j++) {
            C[i][j] = (C[i-1][j] + C[i-1][j-1])%1000007;
        }
    }
    int testcase, cases = 0;
    int n, m, k;
    scanf("%d", &testcase);
    while(testcase--) {
        scanf("%d %d %d", &n, &m, &k);
        int ret = 0;
        for(i = 0; i < 16; i++) {
            int r = n, c = m, bits = 0;
            if(i&1)
                r--, bits++;
            if(i&2)
                r--, bits++;
            if(i&4)
                c--, bits++;
            if(i&8)
                c--, bits++;
            if(bits&1)
                ret = (ret - C[r*c][k])%1000007;
            else
                ret = (ret + C[r*c][k])%1000007;
        }
        printf("Case %d: %d\n", ++cases, (ret+1000007)%1000007);
    }
    return 0;
}

台長: Morris
人氣(2,793) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 教育學習(進修、留學、學術研究、教育概況) | 個人分類: UVA |
此分類下一篇:[UVA][Easy] 12658 - Character Recognition
此分類上一篇:[UVA][dp] 11654 - Arithmetic Subsequence

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文