首页 > 代码库 > UVA - 11806 Cheerleaders (容斥原理)

UVA - 11806 Cheerleaders (容斥原理)

题意:在N*M个方格中放K个点,要求第一行,第一列,最后一行,最后一列必须放,问有多少种方法。

分析:

1、集合A,B,C,D分别代表第一行,第一列,最后一行,最后一列放。

则这四行必须放=随便放C[N * M][K] - 至少有一行没放,即ABCD=随便放-A的补集 ∪ B的补集 ∪ C的补集 ∪ D的补集。

2、A的补集 ∪ B的补集 ∪ C的补集 ∪ D的补集,可用容斥原理计算,二进制枚举即可。

#include<cstdio>#include<cstring>#include<cstdlib>#include<cctype>#include<cmath>#include<iostream>#include<sstream>#include<iterator>#include<algorithm>#include<string>#include<vector>#include<set>#include<map>#include<stack>#include<deque>#include<queue>#include<list>#define lowbit(x) (x & (-x))const double eps = 1e-9;inline int dcmp(double a, double b){    if(fabs(a - b) < eps) return 0;    return a > b ? 1 : -1;}typedef long long LL;typedef unsigned long long ULL;const int INT_INF = 0x3f3f3f3f;const int INT_M_INF = 0x7f7f7f7f;const LL LL_INF = 0x3f3f3f3f3f3f3f3f;const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};const int MOD = 1000007;const double pi = acos(-1.0);const int MAXN = 400 + 10;const int MAXT = 1000 + 10;using namespace std;int C[MAXN][MAXN];void init(){    for(int i = 0; i < MAXN; ++i){        C[i][0] = C[i][i] = 1;        for(int j = 1; j < i; ++j){            C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % MOD;        }    }}int main(){    int T;    scanf("%d", &T);    int kase = 0;    init();    while(T--){        int N, M, K;        scanf("%d%d%d", &N, &M, &K);        printf("Case %d: ", ++kase);        if(K > N * M){            printf("0\n");            continue;        }        int n = 4;        int ans = C[N * M][K];        for(int i = 1; i < (1 << n); ++i){            int cnt = 0, tmpn = N, tmpm = M;            for(int j = 0; j < n; ++j){                if(i & (1 << j)){                    ++cnt;                    if(j & 1) --tmpm;                    else --tmpn;                }            }            if(cnt & 1){                ans = (ans - C[tmpm * tmpn][K] + MOD) % MOD;            }            else{                (ans += C[tmpm * tmpn][K]) %= MOD;            }        }        printf("%d\n", ans);    }    return 0;}

  

UVA - 11806 Cheerleaders (容斥原理)