链接:https://www.nowcoder.com/acm/contest/139/A
来源:牛客网
题目描述
Count the number of n x m matrices A satisfying the following condition modulo (10^9+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 ≤ 10^3
* The number of test cases does not exceed 10^5
题解:
定理链接:http://www.cnblogs.com/jszkc/p/7309468.html
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1 << 11;
const int MOD = 1E9 +7;
ll C[maxn][maxn];
void init()
{
C[0][0] = 1;
for(int i = 1; i < maxn; i ++) {
C[i][0] = 1;
for(int j = 1; j <= i; j ++) {
C[i][j] = C[i - 1][j - 1] + C[i - 1][j];
C[i][j] %= MOD;
}
}
}
int main()
{
ios::sync_with_stdio(false), cin.tie(0);
init();
ll n, m;
while(cin >> n >> m) {
ll res = C[n+m][n] * C[n+m][n] % MOD;
ll t = C[n+m][n-1] * C[n+m][m-1] % MOD;
res -= t;
cout << (res % MOD + MOD) % MOD << '\n';
}
return 0;
}