ZOJ 3687 The Review Plan I 容斥原理/禁位排列

题意:

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4970


题解:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

#define MOD 55566677
#define lint long long
#define MAXN 52

int fa[MAXN];
int n, m;
int a[MAXN][2];
bool ban[MAXN][MAXN];
bool vis1[MAXN], vis2[MAXN];
int ret;

void init()
{
    fa[0] = 1;
    for(int i = 1; i <= 50; i++)
        fa[i] = ((lint)fa[i-1] * i) % MOD;

   // for(int i = 1; i <= 50; i++)
   //     printf("%d\n", fa[i]);
}

void In_Exclusion(int i, int num) //i表示第i个条件,num表示条件数目
{
    if(i > m)
    {
        if(num & 1) ret = (ret - fa[n-num]) % MOD;
        else ret = (ret + fa[n-num]) % MOD;
        return;
    }

    In_Exclusion(i + 1, num); //第i个条件不用
    if(vis1[a[i][0]] == false && vis2[a[i][1]] == false)
    {
        vis1[a[i][0]] = vis2[a[i][1]] = true;
        In_Exclusion(i + 1, num + 1);
        vis1[a[i][0]] = vis2[a[i][1]] = false;
    }
}

int main()
{
    init();
    while(scanf("%d%d",&n,&m) != EOF)
    {
        memset(ban, 0, sizeof(ban));
        memset(vis1, 0, sizeof(vis1));
        memset(vis2, 0, sizeof(vis2));

        for(int i = 1; i <= m; i++)
        {
            scanf("%d%d", &a[i][0], &a[i][1]);
            if(ban[a[i][0]][a[i][1]] == false)
                ban[a[i][0]][a[i][1]] = true;
            else {m--, i--;}
        }

        ret = 0;
        In_Exclusion(1, 0);
        printf("%d\n", (ret % MOD + MOD) % MOD);
    }
    return 0;
}


你可能感兴趣的:(ZOJ 3687 The Review Plan I 容斥原理/禁位排列)