HDU5852:Intersection is not allowed!(Lindström–Gessel–Viennot引理)

xiaoxiao2021-02-28  9

传送门

题解: 利用有向图的Lindström–Gessel–Viennot引理来模拟容斥操作,只需要求行列式即可。

#include <bits/stdc++.h> using namespace std; typedef long long LL; const int RLEN=1<<18|1; inline char nc() { static char ibuf[RLEN],*ib,*ob; (ib==ob) && (ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin)); return (ib==ob) ? -1 : *ib++; } inline int rd() { char ch=nc(); int i=0,f=1; while(!isdigit(ch)) {if(ch=='-')f=-1; ch=nc();} while(isdigit(ch)) {i=(i<<1)+(i<<3)+ch-'0'; ch=nc();} return i*f; } const int N=2e5+50,K=150,mod=1e9+7; inline int add(int x,int y) {return (x+y>=mod) ? (x+y-mod) : (x+y);} inline int dec(int x,int y) {return (x-y<0) ? (x-y+mod) : (x-y);} inline int mul(int x,int y) {return (LL)x*y%mod;} inline int power(int a,int b,int rs=1) {for(;b;b>>=1,a=mul(a,a)) if(b&1) rs=mul(rs,a); return rs;} int n,k,a[K],b[K],A[K][K],fac[N],ifac[N]; inline int C(int x,int y) {return mul(fac[x],mul(ifac[y],ifac[x-y]));} inline int det() { int sgn=1; for(int i=1;i<=n;i++) { int l; for(l=i;l<=n && !A[l][i];++l) if(l>n) return 0; if(l!=i) { sgn=mod-sgn; for(int j=i;j<=n;j++) swap(A[i][j],A[l][j]); } const int inv=power(A[i][i],mod-2); for(int j=i+1;j<=n;j++) { int t=mul(A[j][i],inv); for(int k=i;k<=n;k++) A[j][k]=dec(A[j][k],mul(t,A[i][k])); } } for(int i=1;i<=n;i++) sgn=mul(sgn,A[i][i]); return sgn; } int main() { fac[0]=1; for(int i=1;i<N;i++) fac[i]=mul(fac[i-1],i); ifac[N-1]=power(fac[N-1],mod-2); for(int i=N-2;~i;i--) ifac[i]=mul(ifac[i+1],i+1); for(int T=rd();T;T--) { n=rd(), k=rd(); fac[0]=1; for(int i=1;i<=k;i++) a[i]=rd(); for(int i=1;i<=k;i++) b[i]=rd(); for(int i=1;i<=k;i++) for(int j=1;j<=k;j++) if(a[i]<=b[j]) A[i][j]=C((n-1)+b[j]-a[i],n-1); else A[i][j]=0; n=k; cout<<det()<<endl; } }
转载请注明原文地址: https://www.6miu.com/read-1600280.html

最新回复(0)