LGV - 求多条不相交路径的方案数

推荐博客 : https://blog.csdn.net/qq_25576697/article/details/81138213

链接:https://www.nowcoder.com/acm/contest/139/A
来源:牛客网

题目描述
Count the number of n x m matrices A satisfying the following condition modulo (109+7).
* Ai, j ∈ {0, 1, 2} for all 1 ≤ i ≤ n, 1 ≤ j ≤ m.
* Ai, j ≤ Ai + 1, j for all 1 ≤ i < n, 1 ≤ j ≤ m.
* Ai, j ≤ Ai, j + 1 for all 1 ≤ i ≤ n, 1 ≤ j < m.
输入描述:

The input consists of several test cases and is terminated by end-of-file.
Each test case contains two integers n and m.

输出描述:

For each test case, print an integer which denotes the result.

示例1
输入

1 2
2 2
1000 1000

输出

6
20
540949876

备注:

* 1 ≤ n, m ≤ 103
* The number of test cases does not exceed 105.

题意 : 格点上的数字只能是 0,1,2 ,求在符合题意的前提下的路径数量

思路分析 : 将一对点向右下平移,得到两对新的点

作者:zzuzxy
链接:https://www.nowcoder.com/discuss/87452?type=101&order=0&pos=4&page=1
来源:牛客网

LGV 算法 (Lindström–Gessel–Viennot lemma)

图片说明
求以上矩阵的行列式,其中 e(a,b) 是从a到b的方法数,带入求行列式即可得到(a1,a2,...an) 到 (b1,b2,...bn) 的所有不相交路径的种数

思路

考虑01和12的分界线
是(n, 0)到(0,m)的两条不相交(可重合)路径
分界线以及分界线以上的点是一种,分界线下是一种
平移其中一条变成(n-1, -1)到(-1,m-1);
变成
图片说明

代码示例 :

using namespace std;
#define ll long long
const ll maxn = 1e6+5;
const ll mod = 1e9+7;
const double eps = 1e-9;
const double pi = acos(-1.0);
const ll inf = 0x3f3f3f3f;

ll n, m;
ll pp[2005];

ll qw(ll x, ll cnt){
    ll res = 1;
    
    while(cnt) {
        if(cnt&1) res *= x;
        res %= mod;
        x = x*x;
        x %= mod;
        cnt >>= 1;
    }
    return res;
}

void init(){
    pp[0] = 1;
    for(ll i = 1; i <= 2000; i++) {
        pp[i] = pp[i-1]*i;
        pp[i] %= mod;
    }     
}

int main() {
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    init();
    
    while(~scanf("%lld%lld", &n, &m)){
        ll a1 = (pp[n+m]*qw(pp[n], mod-2)%mod)*qw(pp[m], mod-2)%mod;
        ll a2 = (pp[n+m]*qw(pp[n+1], mod-2)%mod)*qw(pp[n-1], mod-2)%mod;
        ll a3 = (pp[n+m]*qw(pp[m-1], mod-2)%mod)*qw(pp[m+1], mod-2)%mod;
        
        ll ans = a1*a1-a2*a3;
        printf("%lld\n", (ans%mod+mod)%mod);
    }
    return 0;
}

 

posted @ 2018-08-01 21:34  楼主好菜啊  阅读(311)  评论(0编辑  收藏  举报