Monotonic Matrix Gym - 247727A
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
一个矩阵,每一个位置可以填0,1,而且每个位置右面和下面的数字都要大于等于它的数字,求矩阵个数
- 可以画出0,1和1,2的分界线,发现是从矩阵左下角到右上角的不相交的两条线(可以重合)
- 这个问题就转化成了:计算从矩阵左下角到右上角的不相交的两条线(可以重合)的个数
- LGV定理求的是从n个起点到n个终点的不相交也不重合路径的方案数
- 对于这题两条线的起点是(n,0) (n,0),终点是(0,m),(0,m),求不相交可重合的情况数;可以转化成两条线的起点是(n,0) (n-1,-1),终点是(0,m),(-1,m-1),求不相交不重合的情况数
- 根据LGV定理,就是求行列式的值
∣ ( a 1 , b 1 ) ( a 1 , b 2 ) ( a 2 , b 1 ) ( a 2 , b 2 ) ∣ \left| \begin{matrix} (a1,b1)&(a1,b2) \\ (a2,b1)&(a2,b2) \end{matrix} \right| ∣∣∣∣(a1,b1)(a2,b1)(a1,b2)(a2,b2)∣∣∣∣ - (a,b)代表从a点到b点的方案数,a1 = (n,0),a2 =(n-1,-1),b1 = (0,m),b2 = (-1,m-1)
- 结果为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;
const int maxn = 2e3+9;
const int mod =1e9+7;
long long dp[maxn][maxn];
void init()
{
for(long long i = 1; i<=2000; i++)
{
dp[i][0] = dp[i][i] = 1;
for(long long j = 1; j<i; j++)
{
dp[i][j] = (dp[i-1][j]+dp[i-1][j-1])%mod;
}
}
}
int main()
{
init();
ios::sync_with_stdio(false);
long long n,m;
while(scanf("%lld %lld",&n,&m)!=EOF)
{
long long ans = (dp[n+m][n]*dp[n+m][n]%mod-dp[n+m][m-1]*dp[n+m][n-1]%mod+mod)%mod;
cout<<ans<<endl;
}
return 0;
}