Codeforces Round 80 C. Two Arrays

C. Two Arrays

You are given two integers n
and m. Calculate the number of pairs of arrays (a,b)

such that:

the length of both arrays is equal to m;
each element of each array is an integer between 1 and n(inclusive);
ai≤bi for any index i from 1 to m;
array a is sorted in non-descending order;
array b is sorted in non-ascending order.

As the result can be very large, you should print it modulo 109+7

.
Input

The only line contains two integers n and m (1≤n≤1000, 1≤m≤10

).
Output

Print one integer – the number of arrays a and b satisfying the conditions described above modulo 109+7

.
Examples
Input
Copy

2 2

Output
Copy

5

Input

10 1

Output

55

Input

723 9

Output

157557417

Note

In the first test there are 5

suitable arrays:

a=[1,1],b=[2,2];
a=[1,2],b=[2,2];
a=[2,2],b=[2,2];
a=[1,1],b=[2,1];
a=[1,1],b=[1,1].

dp[n][m]为满足长度为m, 最后一位最大为n的不下降序列的个数。

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;

const int maxn = 1e4 + 10;
const int mod = 1e9 + 7;
typedef long long ll;
ll dp[20][maxn];

int main() {
	int n, m;
	cin >> n >> m;
	for(int i = 1; i <= n; i++) {
		dp[1][i] = i;
	}
	for(int i = 2; i <= m; i++) {
		for(int j = 1; j <= n; j++) {
			dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
			dp[i][j] %= mod;
		} 
	}
//	for(int i = 1; i <= n; i++) {
//		cout << dp[m][i] << endl; 
//	}
//	cout << dp[m][n] << endl;
	ll ans = 0;
	for(int i = 1; i <= n; i++) {
		ans += dp[m][i] * (dp[m][n - i + 1] - dp[m][n - i]) % mod;
		ans %= mod; 
	}
	cout << ans << endl;
	return 0;
}

更新另一种写法, 在原来dp的基础上, 把m即长度扩大为原来的二倍, 这样dp[m][n]就是答案, 因为a的最大值小于等于b的最小值, 把b数组反转, 可通过一次dp求出答案。

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;

const int maxn = 1e4 + 10;
const int mod = 1e9 + 7;
typedef long long ll;
ll dp[50][maxn];

int main() {
	int n, m;
	cin >> n >> m;
	for(int i = 1; i <= n; i++) {
		dp[1][i] = i;
	}
	m *= 2; 
	for(int i = 2; i <= m; i++) {
		for(int j = 1; j <= n; j++) {
			dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
			dp[i][j] %= mod;
		}
	}
	ll ans = 0;
	ans = dp[m][n];
	cout << ans << endl;
	return 0;
}
发布了79 篇原创文章 · 获赞 15 · 访问量 1万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 技术黑板 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览