Monotonic Matrix (Lindström–Gessel–Viennot lemma 定理)

链接: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.

 

 

扫描二维码关注公众号,回复: 2436461 查看本文章
#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
const int maxn=1010;
const int mod=1e9+7;

ll f[maxn*2],rev[maxn*2];

ll  pow_(ll base,int n){
    ll ans=1;
    while(n){
        if(n&1)ans=(ans*base)%mod;
        base=(base*base)%mod;
        n>>=1;
    }
    return ans;
}
/*
ll exgcd(ll a,ll b,ll &x,ll &y){
    if(a==0&&b==0) return -1;
    if(b==0){
        x=1,y=0;return a;
    }
    ll d=exgcd(b,a%b,y,x);
    y-=a/b*x;
    return d;
}

ll NY(ll a,ll mod){
    ll x,y;
    ll d=exgcd(a,mod,x,y);
    if(d==1) return (x%mod+mod)%mod;
    else return -1;

    //return pow_(a,mod-2);
}
*/
void init(){
    f[0]=1;
    int m=maxn*2;
    for(ll i=1;i<m;i++){
        f[i]=(f[i-1]*i)%mod;
    }

    for(ll i=1;i<m;i++){
        rev[i]=pow_(f[i],mod-2);
    }
    rev[0]=rev[1];
    /*
    for(ll i=1;i<m;i++){
        rev[i]=NY(f[i],mod);
    }
    rev[0]=rev[1];
    */
}

int main(){
    init();
    int n,m;
    while(scanf("%d %d",&n,&m)==2){
        ll ans1=((f[n+m]*rev[n])%mod*rev[m])%mod;
           ans1=(ans1*ans1)%mod;
        ll ans2=((f[n+m]*rev[n-1])%mod*rev[m+1])%mod;
        ll ans3=((f[n+m]*rev[m-1])%mod*rev[n+1])%mod;

       // printf("f[n+m]:%lld rev[n]:%lld rev[m]:%lld\n",f[n+m],rev[n],rev[m]);
      //  printf("ans1:%lld ans2:%lld ans3:%lld\n",ans1,ans2,ans3);

        ll ans=(ans1+mod-(ans2*ans3)%mod)%mod;
        printf("%lld\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_36424540/article/details/81141988
今日推荐