清北学堂模拟赛day7 错排问题

/*
考虑一下已经放回m本书的情况,已经有书的格子不要管他,考虑没有书的格子,不考虑错排有(n-m)!种,在逐步考虑有放回原来位置的情况,已经放出去和已经被占好的格子,不用考虑,剩下全都考虑,设t=x∩y,把除t以外的搞一下容斥就行了
*/
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#define ll long long
#define fo(i,l,r) for(int i = l;i <= r;i++)
#define fd(i,l,r) for(int i = r;i >= l;i--)
using namespace std;
const int maxn = 100050;
const ll mod = 1000000007LL;
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;
}
ll n,m,x[maxn],y[maxn],rec[maxn],c[maxn];
bool usd[maxn];
ll ans,fac[maxn];
ll q_pow(ll a,ll b){
    ll ret = 1;
    while(b){
        if(b&1) ret = (ret*a) % mod;
        a = (a*a) % mod;
        b >>= 1;
    }
    return ret;
}
inline ll inv(ll x){
    return q_pow(x,mod-2);
}
int main(){
    freopen("problem.in","r",stdin);
    freopen("problem.out","w",stdout);
    n = read();m = read();
    fo(i,1,m) x[i] =read(),y[i]=read();
    fo(i,1,m){
        usd[x[i]] = true;
        usd[y[i]] = true;
    }
    int t = n;
    fo(i,1,n) if(usd[i]) t--;
    fac[0] = fac[1] = 1;
    fo(i,2,n) fac[i] = (fac[i-1]*i) % mod;
    c[0] = c[t] = 1;
    if(t > 0) c[1] = c[t-1] = t;
    fo(i,2,t-2)c[i] = ((c[i-1]*(t-i+1)%mod)*inv(i))%mod;
    ans = fac[n-m];
    fo(i,1,t){
        if(i&1) ans = (ans + 100*mod - (c[i]*fac[n-m-i]) % mod) % mod;
        else ans = (ans + (c[i]*fac[n-m-i]) % mod) % mod;
    }
    cout<<ans;
    return 0;
} 

 

posted @ 2016-11-06 14:48  ACforever  阅读(228)  评论(0编辑  收藏  举报