A - Monotonic Matrix(求x个起点到y个终点的严格不相交路径)

Count the number of n×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.

Input
The input consists of several test cases and is terminated by end-of-file.

Each test case contains two integers n and m.

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

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

Example
Input
1 2
2 2
1000 1000
Output
6
20
540949876
在这里插入图片描述
如图,有两条分界线,设起点分别是A,C终点B,D

Lindstrom-Gessel-Viennot lemma 定理:
对于一张无边权的DAG图,给定n个起点和对应的n个终点,这n条不相交路径的方案数为
在这里插入图片描述
(行列式) 其中e(a,b)为图上a到b的方案数
原理不理解,直接套用/(ㄒoㄒ)/~~
AB,CD:对于单独的轮廓线,共需要往上走n步,往右走m步。在这n+m步中选择n步向上走,即有C(n+m,n)种方式。
假设将其中一条轮廓线向左上平移一个单位,有原先的(n,0)->(0,m)变成了(n-1,-1)->(-1,m-1)
相交的情况可以理解成从A到D,从C到B。情况数是C(n+m,n-1)*C(n+m,m-1)
该图片来自lhy同学的友情提供:根据公式:C(n+m,n)*C(n+m,n)-C(n+m,n-1)*C(n+m,m-1)
在这里插入图片描述

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const ll mod=1e9+7;
ll c[2010][2010]; 
int main()
{
    
    
	int n,m;
    c[1][0]=c[1][1]=1;
    for(int i=2;i<=2000;i++)
	{
    
    
        c[i][0]=1;
        for(int j=1;j<=2000;j++)
         c[i][j]=(c[i-1][j]+c[i-1][j-1])%mod;
    }
    while(~scanf("%d%d",&n,&m))
	{
    
    
     ll ans=(c[n+m][n]*c[n+m][n]%mod-c[n+m][n-1]*c[n+m][m-1]%mod+mod)%mod;
	 printf("%lld\n",ans);	
    }
 
}

猜你喜欢

转载自blog.csdn.net/weixin_43540515/article/details/113100022