题意:1到N的数组成一个排列,有些数不能放到指定的位置上,问这种排列有多少种。
枚举反面,用容斥原理求出所有的不符合条件的序列,即求出所有的序列,其中之至少有一个数放到了条件中所指定的不能放的位置。
#include <iostream>
#include <cstdio>
#include <cstring>
#define lng long long
using namespace std;
const int mod = 55566677;
const int maxn = 100;
lng fact[maxn], ans;
int x[maxn], y[maxn];
int n, m, check1[maxn], check2[maxn];
int stop[maxn][maxn];
void dfs(int pos, int num)
{
if(num > n) return;
ans += (num % 2) ? -fact[n - num] : fact[n - num];
ans = (ans + mod) % mod;
for(int i = pos; i < m; ++i)
{
if(!check1[x[i]] && !check2[y[i]])
{
check1[x[i]] = check2[y[i]] = 1;
dfs(i, num + 1);
check1[x[i]] = check2[y[i]] = 0;
}
}
}
int main()
{
freopen("in", "r", stdin);
fact[0] = 1;
for(int i = 1; i < 60; ++i) fact[i] = (fact[i - 1] * i) % mod;
while(~scanf("%d %d", &n, &m))
{
memset(stop, 0, sizeof(stop));
int cnt = 0;
for(int i = 0; i < m; ++i)
{
int a, b; scanf("%d %d", &a, &b);
if(!stop[a][b])
{
stop[a][b] = 1;
x[cnt] = a; y[cnt] = b;
cnt++;
}
}
m = cnt;
ans = fact[n];
for(int i = 0; i < m; ++i)
{
check1[x[i]] = check2[y[i]] = 1;
dfs(i, 1);
check1[x[i]] = check2[y[i]] = 0;
}
cout << ans << "\n";
}
return 0;
}
/*
4 3
1 2
4 3
2 1
6 5
1 1
2 6
3 5
4 4
3 4
11
284
*/