uva 11806 Cheerleaders (容斥)

时间:2024-01-05 13:16:14

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

  容斥原理,从反面去想。统计边界上都没有石子的情况。这时候就要用到容斥原理了。

代码如下:

 #include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio> using namespace std; typedef long long LL;
const LL MOD = ;
const int N = ;
const int M = N * N;
LL C[M][M]; void PRE() {
C[][] = ;
for (int i = ; i < M; i++) {
C[i][] = ;
for (int j = ; j <= i; j++) {
C[i][j] = (C[i - ][j - ] + C[i - ][j]) % MOD;
}
}
} int main() {
int n, m, k, T, cas = ;
PRE();
cin >> T;
for (int cas = ; cas <= T; cas++) {
cin >> n >> m >> k;
LL ans = ;
for (int i = ; i < ; i++) {
int r = n, c = m, odd = false;
if (i & ) r--, odd = !odd;
if (i & ) r--, odd = !odd;
if (i & ) c--, odd = !odd;
if (i & ) c--, odd = !odd;
if (r < || c < ) ;
else if (odd) ans -= C[r * c][k];
else ans += C[r * c][k];
while (ans < ) ans += MOD;
while (ans >= MOD) ans -= MOD;
}
printf("Case %d: ", cas);
cout << ans << endl;
}
return ;
}

——written by Lyon