牛客網第一場 A Monotonic Matrix
阿新 • • 發佈:2018-07-24
chm png blank tro test case a* res isf www.
* 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.
定理。
鏈接:https://www.nowcoder.com/acm/contest/139/A
來源:牛客網
* 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 ≤ 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; }
牛客網第一場 A Monotonic Matrix