UVa 11806 - Cheerleaders (组合数学 容斥原理)

来源:互联网 发布:淘宝宝贝标题违规规则 编辑:程序博客网 时间:2024/05/16 08:17

Cheerleaders

Time Limit: 2000MSMemory Limit: Unknown64bit IO Format: %lld & %llu

[Submit]   [Go Back]   [Status]  

Description

Download as PDF

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


[Submit]   [Go Back]   [Status]  




题意:

n行m列的矩形格子里面放k个石子,每个格子只能放一个,第一行 最后一行 第一列 最后一列 都必须有石子,问方案数有多少


正面不好求可以通过求反面来求

全集S=C(n*m, k) 

A表示第一行不放的方案集合,B表示最后一行不放的方案集合,C表示第一列不放的方案集合,D表示最后一列不放的方案集合

那么答案就是 { S - { A U B U C U D } }

就是求 A∪B∪C∪D = |A|+|B|+|C|+|D| - |A∩B| - |B∩C| - |C∩A|- |A∩D| - |B∩D| - |C∩D|+|A∩B∩C|+|A∩B∩D| +|A∩C∩D| +|B∩C∩D| -|A∩B∩C∩D|

写出来就能明白大白上面的代码了 加减和奇偶有关


#include <cstdio>#include <iostream>#include <vector>#include <algorithm>#include <cstring>#include <string>#include <map>#include <cmath>#include <queue>#include <set>using namespace std;//#define WIN#ifdef WINtypedef __int64 LL;#define iform "%I64d"#define oform "%I64d\n"#define oform1 "%I64d"#elsetypedef long long LL;#define iform "%lld"#define oform "%lld\n"#define oform1 "%lld"#endif#define S64I(a) scanf(iform, &(a))#define P64I(a) printf(oform, (a))#define P64I1(a) printf(oform1, (a))#define REP(i, n) for(int (i)=0; (i)<n; (i)++)#define REP1(i, n) for(int (i)=1; (i)<=(n); (i)++)#define FOR(i, s, t) for(int (i)=(s); (i)<=(t); (i)++)const int INF = 0x3f3f3f3f;const double eps = 10e-9;const double PI = (4.0*atan(1.0));const int maxk = 500 + 20;const int MOD = 1000007;int C[maxk][maxk];void init() {    memset(C, 0, sizeof(C));    C[0][0] = 1;    for(int i=1; i<maxk; 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;        }    }}int main() {    int T;    init();    scanf("%d", &T);    for(int kase=1; kase<=T; kase++) {        int n, m, k;        scanf("%d%d%d", &n, &m, &k);        int sum = 0;        for(int i=1; i<16; i++) {            int r = n, c = m, b = 0;            if(i&1) { r--; b++; }            if(i&2) { r--; b++; }            if(i&4) { c--; b++; }            if(i&8) { c--; b++; }            if(b&1) sum = (sum + C[r*c][k]) % MOD;            else sum = (sum - C[r*c][k] + MOD) % MOD;        }        int ans = (C[n*m][k] - sum + MOD) % MOD;        printf("Case %d: %d\n", kase, ans);    }    return 0;}







0 0
原创粉丝点击