Monotonic Matrix

链接:https://www.nowcoder.com/acm/contest/139/A
 

题目描述

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

输入

复制

1 2
2 2
1000 1000

输出

复制

6
20
540949876

备注:

 

* 1 ≤ n, m ≤ 103
* The number of test cases does not exceed 105.

分析:

考虑n*m中01的分界线和12的分界线,一开始两条路径的起点都是(1,1)终点都是(n,m)

然后考虑他们相交的情况,相交包括重叠,所以将01的分界线往右上角移动,起点变成(2,0)终点变成(n+1,m-1)

在交点处交换他们后面的边使01的分界线起点变成(2,0)终点变成(n,m)。

12的分界线,起点变成(1,1),终点变成(n+1,m-1)

Cm+n,n(2)-Cm+n,n-1*Cm+n,m-1

代码:

#include <bits/stdc++.h>

const int MOD = 1e9 + 7;

const int N = 1005;

int dp[N][N];

void update(int& x, int a)
{
    x += a;
    if (x >= MOD) {
        x -= MOD;
    }
}

int sqr(int x)
{
    return 1LL * x * x % MOD;
}

int main()
{
    dp[0][0] = 1;
    for (int i = 0; i < N; ++ i) {
        for (int j = 0; j < N; ++ j) {
            if (i) {
                update(dp[i][j], dp[i - 1][j]);
            }
            if (j) {
                update(dp[i][j], dp[i][j - 1]);
            }
        }
    }
    int n, m;
    while (scanf("%d%d", &n, &m) == 2) {
        printf("%d\n", ((sqr(dp[n][m]) + MOD - 1LL * dp[n - 1][m + 1] * dp[n + 1][m - 1] % MOD) % MOD));
    }
}

猜你喜欢

转载自blog.csdn.net/lml11111/article/details/81138776