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;
}