链接:https://www.nowcoder.com/acm/contest/139/A
来源:牛客网
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 ≤ 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; }
原文:https://www.cnblogs.com/2014slx/p/9358705.html