[LOJ#6160][美团 CodeM 初赛 Round A][容斥][递推]二分图染色

题解

#include 
#include 
#include 

using namespace std;

typedef long long ll;

const int N=1e7+10,P=1e9+7;

int n,f[N],fac[N],inv[N];

inline ll C(int x,int y){
  return 1LL*fac[x]*inv[y]%P*inv[x-y]%P;
}

inline ll A(int x,int y){
  return 1LL*fac[x]*inv[x-y]%P;
}

int main(){
  scanf("%d",&n);
  f[0]=1; f[1]=2;
  for(int i=2;i<=n;i++)
    f[i]=(2LL*i*f[i-1]%P+P-1LL*(i-1)*(i-1)%P*f[i-2]%P)%P;
  fac[0]=inv[0]=inv[1]=1;
  for(int i=1;i<=n;i++) fac[i]=1LL*fac[i-1]*i%P;
  for(int i=2;i<=n;i++) inv[i]=1LL*(P-P/i)*inv[P%i]%P;
  for(int i=1;i<=n;i++) inv[i]=1LL*inv[i-1]*inv[i]%P;
  int ans=1LL*f[n]*f[n]%P;
  for(int i=1,p=-1;i<=n;i++,p=-p)
    ans=(ans+(P+1LL*p*f[n-i]*f[n-i]%P*C(n,i)%P*A(n,i)%P)%P)%P;
  printf("%d\n",ans);
  return 0;
}

你可能感兴趣的:(容斥原理,二分图)