1. 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 ≤ n, m ≤ 103
The number of test cases does not exceed 10 5.
输入
1 2
2 2
1000 1000
输出
6
20
540949876
题意:一个矩阵,满足所有元素由0,1,2组成,且满足a[i][j]<=a[i+1][j]和a[i][j]<=a[i][j+1],给出矩阵边长,问满足条件的方案数。
思路:其实这个矩阵满足在矩阵中找两条线,两条不相交的线,两条线一端在左边界或下边界,另一端在上边界或右边界。
利用Lindström–Gessel–Viennot lemma引理:
来自:https://blog.csdn.net/qq_25576697/article/details/81138213
维基百科有详细讲解。
那么对应这道题,也就是一个两阶的行列式展开就是C(n+m,n)*C(n+m,n)-C(n+m,n-1)*C(n+m,n+1)。
long long c[2000 + 5][2000 + 5];
void init()
{
for (int i = 0; i <= 1005; i++)
{
for (int j = 0; j <= 1005; j++)
{
if (i == 0 || j == 0)
c[i][j] = 1;
else
c[i][j] = (c[i - 1][j] + c[i][j - 1])%MOD;
}
}
}
int main()
{
int n, m;
init();
while (scanf("%d%d",&n,&m)!=EOF)
{
printf("%d\n", ((c[n][m] * c[n][m])%MOD - (c[m+1][n - 1] * c[n+1][m - 1]) % MOD+MOD) % MOD);
}
return 0;
}