m行n列矩形网格放k个【相同】石子，四条边界都要放，问有多少种放法。

|ABCD|=S|ABCD|

//  Created by wander on 16/6/1.
//  UVa 11806 Cheerleaders
//  类型：组合数学-容斥原理

#include "bits/stdc++.h"
using namespace std;

const int MOD = 1000007;
int kase, C[505][505];

void Init() { // 计算组合数{ C_i^j }，杨辉三角递推
for (int i = 0; i < 505; i += 1) {
C[i][0] = C[i][i] = 1;
for (int j = 1; j < i; j += 1)
C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % MOD;
}
}

int main() {
#ifdef DEBUG
freopen("in", "r", stdin);
freopen("out", "w", stdout);
#endif
Init();
scanf("%d", &kase);
for (int nCase = 1; nCase <= kase; nCase += 1) {
int n, m, k, res = 0;
scanf("%d%d%d", &n, &m, &k);
for (int state = 0; state < 16; state += 1) {
int cnt = 0, r = n, c = m;
if (state & 1) { cnt += 1; r -= 1; }
if (state & 2) { cnt += 1; r -= 1; }
if (state & 4) { cnt += 1; c -= 1; }
if (state & 8) { cnt += 1; c -= 1; }
if (cnt & 1) res = (res + MOD - C[r * c][k]) % MOD;
else res = (res + C[r * c][k]) % MOD;
}
printf("Case %d: %d\n", nCase, res);
}
return 0;
}

• 本文已收录于以下专栏：

DP+容斥原理

UVA - 11806 - Cheerleaders （递推）

UVA - 11806 Cheerleaders Time Limit: 2000MS Memory Limit: Unknown 64bit IO Format: %ll...

Cheerleaders UVA - 11806（容斥原理）题解

Cheerleaders UVA - 11806（容斥原理）题解