1. 程式人生 > >黑龍江省賽 A Path Plan(組合數學+Lindstrom-Gessel-Viennot Lemma定理)

黑龍江省賽 A Path Plan(組合數學+Lindstrom-Gessel-Viennot Lemma定理)

問題 C: A Path Plan

時間限制: 1 Sec  記憶體限制: 128 MB
提交: 55  解決: 26
[提交] [狀態] [討論版] [命題人:admin]

題目描述

WNJXYK hates Destinys so that he does not want to meet him at any time. Luckily, their classrooms and dormitories are at different places. The only chance for them to meet each other is on their way to classrooms from dormitories. 
To simple this question, we can assume that the map of school is a normal rectangular 2D net. WNJXYK’s dormitory located at (0,y_1) and his classroom located at (x_1,0). Destinys’s dormitory located at (0,y_2) and his classroom is located at (x_2,0). On their way to classrooms, they can do two kinds of movement : (x,y)→(x,y-1) and (x,y)→(x+1,y). 
WNJXYK does not want to meet Destinys so that he thinks that it is not safe to let his path to classroom from dormitory has any intersect point with Destinys ‘s. And then he wonders how many different paths for WNJXYK and Destinys arriving their classrooms from dormitories safely.

輸入

The input starts with one line contains exactly one positive integer T which is the number of test cases.
Each test case contains one line with four positive integers x1,x2,y1,y2 which has been explained above.

輸出

For each test case, output one line containing “y” where y is the number of different paths modulo 10^9+7.

樣例輸入

3
1 2 1 2
2 3 2 4
4 9 3 13

樣例輸出

3
60
16886100

提示


T≤1000
x1<x2,y1<y2
0 < x1,x2,y1,y2≤100000
For Test Case 1, there are following three different ways.

題意:兩個人分別從(0,y1)-(x1,0)和(0,y2)-(x2,0),求他們路徑不相交的方案數。

題解:比賽的時候用組合數學想了好久也沒想到,最後沒想到有這麼個定理,還是太渣了,定理如下

對於一張無邊權的DAG圖,給定n個起點和對應的n個終點,這n條不相交路徑的方案數為

det() (該矩陣的行列式)

其中e(a,b)為圖點a到點b的方案數,如此答案只需要求解這個行列式即可。

2.行列式計算

https://blog.csdn.net/zhoufenqin/article/details/7779707

 

ps:求階乘的逆元的小trick

inv[maxn] = qk_mod(fac[maxn],mod - 2);

for(int i = maxn - 1;i >= 0;i --) inv[i] = inv[i + 1] * (i + 1) % mod;

#include<bits/stdc++.h>
#define rep(i,s,t) for(int i=s;i<=t;i++)
#define SI(i) scanf("%lld",&i)
#define PI(i) printf("%lld\n",i)
using namespace std;
typedef long long ll;
const int mod=1e9+7;
const int MAX=1e6+7;
const int INF=0x3f3f3f3f;
const double eps=1e-8;
int dir[9][2]={0,1,0,-1,1,0,-1,0, -1,-1,-1,1,1,-1,1,1};
template<class T>bool gmax(T &a,T b){return a<b?a=b,1:0;}
template<class T>bool gmin(T &a,T b){return a>b?a=b,1:0;}
template<class T>void gmod(T &a,T b){a=((a+b)%mod+mod)%mod;}
typedef pair<ll,ll> PII;

ll qpow(ll a,ll b)
{
    a%=mod;
    ll ans=1;
    while(b)
    {
        if(b&1)
            ans=(ans*a)%mod;
        a=(a*a)%mod;
        b/=2;
    }
    return ans;
}

ll fac[MAX];
ll C(ll a,ll b)
{
    return fac[a]*qpow(fac[a-b],mod-2)%mod*qpow(fac[b],mod-2)%mod;
}

int main()
{
    fac[0]=1;
    for(int i=1;i<=200005;i++)
        fac[i]=fac[i-1]*i%mod;

	int T;
	scanf("%d",&T);
    while(T--)
    {
        ll x1,x2,y1,y2;
        scanf("%lld%lld%lld%lld",&x1,&x2,&y1,&y2);

        ll ans=C(x1+y1,x1)*C(x2+y2,x2)%mod-C(x1+y2,x1)*C(x2+y1,x2)%mod;
        gmod(ans,(ll)mod);

        printf("%lld\n",ans);
    }
}