牛客网第一场 A Monotonic Matrix

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

Count the number of n x m matrices A satisfying the following condition modulo (10 9+7).
* A i, j ∈ {0, 1, 2} for all 1 ≤ i ≤ n, 1 ≤ j ≤ m.
* A i, j ≤ A i + 1, j for all 1 ≤ i < n, 1 ≤ j ≤ m.
* A i, j ≤ A i, 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 ≤ 10

3


* The number of test cases does not exceed 10

5

考虑 0101 和 1212 的分界线,用 (n,0)(n,0) 到 (0,m)(0,m) 的两条不相交(可重合)路径,平移其中一条变成 (n−1,−1)(n−1,−1) 到 (−1,m−1)(−1,m−1) 变成起点 (n,0)(n,0) 和 (n−1,−1)(n−1,−1),终点 (0,m)(0,m) 和 (−1,m−1)(−1,m−1) 的严格不相交路径,套 Lindstrom-Gessel-Viennot lemma 定理。

lgv公式;

#include <iostream>
#include<stdio.h>
#define ll long long
#define mod 1000000007
using namespace std;
ll inv[3005];
ll jiecheng[3005];
ll pow (ll a,ll b)
{
    ll ans=1;
    while(b>0)
    {
        if(b%2==1)
        {
            ans=ans*a;
            ans=ans%mod;
        }
        b=b/2;
        a=(a*a)%mod;
    }
    return ans;

}
int main()
{

    jiecheng[0]=1;
    for(int i=1;i<=2000;i++)
    {
        jiecheng[i]=jiecheng[i-1]*i;
        jiecheng[i]=jiecheng[i]%mod;
    }
    for(int i=0;i<=2000;i++)
    {
        inv[i]=pow(jiecheng[i],mod-2);
    }

   int n,m;
   while(~scanf("%d%d",&n,&m))
   {
      ll p;
      p=(jiecheng[n+m]*inv[n])%mod*inv[m]%mod;
      p=p*p%mod;
      ll q,w;
      q= (jiecheng[n+m]*inv[n+1])%mod*inv[m-1]%mod;
      w=(jiecheng[n+m]*inv[n-1])%mod*inv[m+1]%mod;
      q=q*w%mod;

      cout<<(p+mod-q)%mod<<endl;
    }

    return 0;
}

猜你喜欢

转载自www.cnblogs.com/2014slx/p/9358705.html