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

題目鏈接


 Michael takes the Discrete Mathematics course in this semester. Now it's close to the final exam, and he wants to take a complete review of this course.

The whole book he needs to review has N chapter, because of the knowledge system of the course is kinds of discrete as its name, and due to his perfectionism, he wants to arrange exactly N days to take his review, and one chapter by each day.

But at the same time, he has other courses to review and he also has to take time to hang out with his girlfriend or do some other things. So the free time he has in each day is different, he can not finish a big chapter in some particular busy days.

To make his perfect review plan, he needs you to help him.

Input

There are multiple test cases. For each test case:

The first line contains two integers N(1≤N≤50), M(0≤M≤25), N is the number of the days and also the number of the chapters in the book.

Then followed by M lines. Each line contains two integers D(1≤D≤N) and C(1≤C≤N), means at the Dth day he can not finish the review of the Cth chapter.

There is a blank line between every two cases.

Process to the end of input.

Output

One line for each case. The number of the different appropriate plans module 55566677.

Sample Input

4 3
1 2
4 3
2 1

6 5
1 1
2 6
3 5
4 4
3 4

Sample Output

11
284

題意:

給你1 - n ,n個數和一些限制條件, 限制條件爲某一位數不能排在某一個位置.

思路:

當時看到這題的第一想法是容斥原理(並不知道禁位排列公式), 於是寫, 寫出來後發現wa了, 但是仔細看並沒有發現錯誤,後來才發現原理要去重,居然有同一個條件出現多次的情況,真是噁心,

方法一: 容斥原理:

其實很簡單就是一個奇加偶減, 只是需要注意的是某一個位置或者某一個數的限制條件只能用一次.

方法二:禁位排列:

其實就是暴力搜索所有情況.

方法一代碼:

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

typedef long long LL;
const int mod = 55566677;
LL f[101];
int  cnt, n, m;
LL ans;
int a[100];
int b[100];
bool vis1[101];
bool vis2[101];
bool stop[51][51];

void init(){
    f[0] = 1;
    for(int i = 1; i <= 55; ++i)
        f[i] = f[i-1] * i % mod;
}

void dfs(int i, int num){
    if(num > n) return ;
    if(num&1)
        ans -= f[n - num];
    else ans += f[n - num];
    ans = (ans % mod + mod) % mod;
    for(int j = i + 1; j <= m; ++j)
        if(!vis1[a[j]] && !vis2[b[j]]){
            vis1[a[j]] = true;
            vis2[b[j]] = true;
            dfs(j,num + 1);
            vis1[a[j]] = false;
            vis2[b[j]] = false;
    }
}

int main(){
    init();
    while(scanf("%d %d", &n, &m) != EOF){
        memset(vis1, false, sizeof(vis1));
        memset(stop, 0, sizeof(stop));
        memset(vis2, false, sizeof(vis2));
        int cnt = 0;
       for(int i = 1; i <= m; ++i){
            int x, y;
            scanf("%d %d", &x, &y);
            if(!stop[x][y]){
                stop[x][y] = 1;
                a[++cnt] = x;
                b[cnt] = y;
            }
        }ans = f[n];
        m = cnt;
        for(int i = 1; i <= m; ++i){
                vis1[a[i]] = true;
                vis2[b[i]] = true;
                dfs(i,1);
                vis1[a[i]] = false;
                vis2[b[i]] = false;
        }printf("%lld\n",ans);
    }return 0;
}

方法二代碼:

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

const int mod = 55566677;
typedef long long LL;

bool vis1[55], vis2[55], vis[55][55];
int a[55], b[55];
LL f[55];
int num, n;
LL c[55][55];
LL ans;

void init(){
    f[0] = 1;
    for(int i = 1; i <= 50; ++i)
        c[i][i] = c[i][0] = 1,f[i] = f[i-1] * i % mod;
    for(int i = 1; i <= 50; ++i)
        for(int j = 1; j < i; ++j)
            c[i][j] = (c[i-1][j-1] + c[i-1][j]) % mod;
}

void dfs(int i, int cnt){
    if(i > num){
        if(cnt&1) ans = ((ans - f[n - cnt]) % mod + mod ) % mod;
        else ans = ((ans + f[n - cnt]) % mod + mod ) % mod;
        return ;
    }
    dfs(i+1,cnt);//不選取第 i 個點
    int j = i;
    if(!vis1[a[j]] && !vis2[b[j]]){
        vis1[a[j]] = vis2[b[j]] = true;
        dfs(j, cnt+1);//選取第 i 個點
        vis1[a[j]] = vis2[b[j]] = false;
    }
}

int main(){
    int m;
    init();
    while(scanf("%d %d", &n, &m) != EOF){
        num = 0;
        memset(vis1,false, sizeof(vis1));
        memset(vis1,false, sizeof(vis1));
        memset(vis, false, sizeof(vis));
        while(m--){
            int x, y;
            scanf("%d %d", &x, &y);
            if(!vis[x][y]){
                vis[x][y] = true;
                ++num;
                a[num] = x;
                b[num] = y;
            }
        }ans = 0;
       dfs(1,0);
        printf("%lld\n",ans);
    }return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章