[HDU-6482]A Path Plan

题目

传送门

题解

这道题可以转化为一个模型,即在网格上求解不相交的路径数,解决这个问题的方法有一个经典的 \(\text{LGV}\) 引理,此题用到了引理的最简单的版本。

首先我们需要明白此题应该用总方案数减去相交路径的方案数,接下来要问的就是如何求解有多少路径相交。

我们假设左右分别有 \(A,B\) 两点,他们分别要到左右的 \(A',B'\) 两点去,现在我们求解他们不相交的路径的方案数,下面引用证明:(原文见这里

如果路径相交,那么交点一定只会有偶数个(包含 \(0\))。

在交叉点处的路径是一个经典问题:由于两个点的性质相同,既可以看成两个点穿过了彼此,也可以看成两个点碰了一下头又彼此走开了。

左边的 \(A\) 最终要在左边的 \(A'\) 结束,右边的 \(B\) 最终要在右边的 \(B'\) 结束,那么这两个点发生交换的次数应该为偶数(也可以交换 \(0\) 次)。

反过来,当我们交换了终点,左边的 \(A\) 要走到右边的 \(B'\) 去,那么交换的次数就应该为奇数。

考虑 \(n\) 的交点所有的 \(2^n\) 种情况,其中交换次数为奇数和偶数的情况各占一半,作差过后,相交的情况就被抵消掉了。

代码

#include<cstdio>

#define rep(i,__l,__r) for(signed i=(__l),i##_end_=(__r);i<=i##_end_;++i)
#define fep(i,__l,__r) for(signed i=(__l),i##_end_=(__r);i>=i##_end_;--i)
#define erep(i,u) for(signed i=tail[u],v=e[i].to;i;i=e[i].nxt,v=e[i].to)
#define writc(a,b) fwrit(a),putchar(b)
#define mp(a,b) make_pair(a,b)
#define fi first
#define se second
typedef long long LL;
// typedef pair<int,int> pii;
typedef unsigned long long ull;
typedef unsigned uint;
#define Endl putchar('\n')
// #define int long long
// #define int unsigned
// #define int unsigned long long

#define cg (c=getchar())
template<class T>inline void read(T& x){
    char c;bool f=0;
    while(cg<'0'||'9'<c)f|=(c=='-');
    for(x=(c^48);'0'<=cg&&c<='9';x=(x<<1)+(x<<3)+(c^48));
    if(f)x=-x;
}
template<class T>inline T read(const T sample){
    T x=0;char c;bool f=0;
    while(cg<'0'||'9'<c)f|=(c=='-');
    for(x=(c^48);'0'<=cg&&c<='9';x=(x<<1)+(x<<3)+(c^48));
    return f?-x:x;
}
template<class T>void fwrit(const T x){//just short,int and long long
    if(x<0)return (void)(putchar('-'),fwrit(-x));
    if(x>9)fwrit(x/10);
    putchar(x%10^48);
}
template<class T>inline T Max(const T x,const T y){return x<y?y:x;}
template<class T>inline T Min(const T x,const T y){return x<y?x:y;}
template<class T>inline T fab(const T x){return x>0?x:-x;}
inline int gcd(const int a,const int b){return b?gcd(b,a%b):a;}
inline LL mulMod(const LL a,const LL b,const LL mod){//long long multiplie_mod
    return ((a*b-(LL)((long double)a/mod*b+1e-8)*mod)%mod+mod)%mod;
}

const int mod=1e9+7;
const int maxsize=200000;

int fac[maxsize+5];
int facinv[maxsize+5];

inline int qkpow(int a,int n){
    int ret=1;
    for(;n>0;n>>=1,a=1ll*a*a%mod)if(n&1)ret=1ll*ret*a%mod;
    return ret;
}

inline void Init(){
    fac[0]=1;
    rep(i,1,maxsize)fac[i]=1ll*fac[i-1]*i%mod;
    facinv[maxsize]=qkpow(fac[maxsize],mod-2);
    fep(i,maxsize-1,1)facinv[i]=1ll*facinv[i+1]*(i+1)%mod;
    facinv[0]=1;
}

inline int C(const int n,const int m){
    if(n<m)return 0;
    if(m==0 || n==m)return 1;
    return 1ll*fac[n]*facinv[m]%mod*facinv[n-m]%mod;
}

int x1,x2,y1,y2;

inline int getmod(const int x){
    return (x%mod+mod)%mod;
}

inline void solve(){
    x1=read(1),x2=read(1),y1=read(1),y2=read(1);
    int C1=C(x1+y1,x1),C2=C(x2+y2,x2);
    int C3=C(x1+y2,x1),C4=C(x2+y1,x2);
    writc(getmod(1ll*C1*C2%mod-1ll*C3*C4%mod),'\n');
}

signed main(){
    Init();
    rep(i,1,read(1))solve();
    return 0;
}
posted @ 2020-09-07 19:32  Arextre  阅读(156)  评论(0编辑  收藏  举报