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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值