Two Arrays(dp)

不打扰是莪最后的温柔 提交于 2020-03-08 12:56:24

问题

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 1e9+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
2 2
Output
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].

题意:

给出n和m,据此找出两个长度为m的数组,ai<=bi,数组a为不下降序列,数组b为不上升序序列。

思路:

由上边的关系可以得出a1<=…ai<=…am<=bm…<=bi<=…b1.
所以问题就转化为求一个长度为2m的不下降序列的个数
设dp[i][j],i表示数组长度,j表示数组的最后一位,dp[i][j]表示长度为i最后一位为j时所有可能的个数。
那么dp[1][j]=1,dp[i][j]=dp[i][j]+dp[i-1][k],k从1到j遍历

代码实现

#include<iostream>
using namespace std;
#include<cmath>
const int mod=1e9+7;

int main()
{
	long long sum,dp[30][1000];
	int n,m;
	int i,j,k;
	long long ans=0;
	cin>>n>>m;
	for(i=1;i<=n;i++)
	dp[1][i]=1;
	for(i=2;i<=(2*m);i++)
	{
		for(j=1;j<=n;j++)
		{
			for(k=1;k<=j;k++)
			dp[i][j]=(dp[i][j]+dp[i-1][k])%mod;
		}
	}
	for(i=1;i<=n;i++)
	ans=(ans+dp[2*m][i])%mod;
	cout<<ans<<endl;
	return 0;
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!