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,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;
}