Two Arrays(DP递推)

You are given two integers nn and mm. Calculate the number of pairs of arrays (a,b)(a,b) such that:

the length of both arrays is equal to mm;
each element of each array is an integer between 11 and nn (inclusive);
ai≤biai≤bi for any index ii from 11 to mm;
array aa is sorted in non-descending order;
array bb is sorted in non-ascending order.
As the result can be very large, you should print it modulo 109+7109+7.

Input
The only line contains two integers nn and mm (1≤n≤10001≤n≤1000, 1≤m≤101≤m≤10).

Output
Print one integer – the number of arrays aa and bb satisfying the conditions described above modulo 109+7109+7.

Examples
Input
2 2
Output
5
Input
10 1
Output
55
Input
723 9
Output
157557417
Note
In the first test there are 55 suitable arrays:

a=[1,1],b=[2,2]a=[1,1],b=[2,2];
a=[1,2],b=[2,2]a=[1,2],b=[2,2];
a=[2,2],b=[2,2]a=[2,2],b=[2,2];
a=[1,1],b=[2,1]a=[1,1],b=[2,1];
a=[1,1],b=[1,1]a=[1,1],b=[1,1].
构造两个数组,有几个要求。
①长度为m。并且数组中的数字范围为1~n。
②a数组是非递减数组,b数组是非递增数组。
③对于两个数组的相同位置的数字,ai<bi。
问能构造出几个这样的数组来。
思路:一开始想的很复杂,无从下手。试想一下,如果同时考虑a和b两个数组,这样的话,要考虑两个方面,不太好写。如果把a数组正序排列,b数组倒序排列,然后组合起来,这样的话,不就是一个长度为2m的非递减数组了吗。这样就转化成了长度为2m,数组范围1~n的非递减数组有多少个了。DP递推即可。
代码如下:

#include<bits/stdc++.h>
#define ll long long
#define mod 1000000007
using namespace std;

const int maxx=1e3+100;
int n,m;
ll dp[21][maxx];

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

努力加油a啊,(o)/~

发布了414 篇原创文章 · 获赞 23 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/starlet_kiss/article/details/104130639