UVA 11806 Cheerleaders dp+容斥

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
2
2 2 1
2 3 2
Sample Output
Case 1: 0
Case 2: 2

题意:给定n*m的棋盘和k个一样的女孩,最上面和最下面一行,最左边和最右边一列至少有一个女孩,问有多少中方案数。

题解:设最上面一行不放石头的方案为集合A,最下面一行不放的方案为集合B,最左边不放的方案为集合C,最右边放的方案为集合D,全集为S。那么答案就是|S|-|A∪B∪C∪D|,求|A∪B∪C∪D|直接用容斥原理就好。

      至于求ABCD,  我们设定dp[i][j]表示  i个格子放了j个女孩的方案数   dp[i][j] = dp[i-1][j-1]+dp[i-1][j];

//meek
///#include<bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <sstream>
#include <queue>
using namespace std ;
typedef long long ll;
#define mem(a) memset(a,0,sizeof(a))
#define pb push_back
#define fi first
#define se second
#define MP make_pair
inline ll read()
{
    ll x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9')
    {
        if(ch=='-')f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')
    {
        x=x*10+ch-'0';
        ch=getchar();
    }
    return x*f;
}
//****************************************

const int N=400+100;
const ll INF = 1ll<<61;
const int inf = 1000000007;
const int MOD= 1000007;


int n,k,m;
ll dp[N][N];
int solve() {
    int ans = dp[n*m][k]%MOD;
    for(int i=1;i<(1<<4);i++) {
        int  a = n, b = m, cnt = 0;
        if(i&(1<<0)) cnt++,a--;
        if(i&(1<<1)) cnt++,a--;
        if(i&(1<<2)) cnt++,b--;
        if(i&(1<<3)) cnt++,b--;
        if(cnt&1) ans = (ans - dp[a*b][k]+MOD)%MOD;
        else ans = (ans + dp[a*b][k]+MOD)%MOD;
    }
    return ans%MOD;
}
void init() {
    for(int i=0;i<=400;i++) dp[i][i] = 1, dp[i][0] = 1;
    for(int i=2;i<=400;i++) {
        for(int j=1;j<i;j++) {
            dp[i][j] = dp[i-1][j] + dp[i-1][j-1];
            dp[i][j] %= MOD;
        }
    }
}
int main() {
   int T, cas = 1;
   init();
   scanf("%d",&T);
   while(T--) {
    scanf("%d%d%d",&n,&m,&k);
    printf("Case %d: %d
",cas++,solve());
   }
    return 0;
}
代码
原文地址:https://www.cnblogs.com/zxhl/p/5083155.html