Two Arrays

非 Y 不嫁゛ 提交于 2020-01-16 07:19:45

题目

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 10^9+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 10^9+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].

思路

由题意可知:a数组是一个不递减的序列,b数组是一个不递增的序列,且对于每一个i有ai<=biai<=bi,所以由:
am<=bm,am>=am1,bm<=bm1am<=bm,am>=am-1,bm<=bm-1,得bm1>=bm>=am>=am1bm-1>=bm>=am>=am-1,所以有:
                               b1b2b3....bmamam1am2...a1b_{1}\geq b_{2}\geq b_{3}\geq....b{m}\geq a_{m}\geq a{m-1}\geq a_{m-2}\geq...a_{1}
相当于构建一个长度为2m2*m的非递减(或非递增)序列
然后求这个序列就是dp~(想到不状态转移方程那就没法= =)
dp[i][j]dp[i][j]为长度为ii,结尾数字为jj的非递减序列数量
有 状态转移方程为:
                                                                        dp[i][j]=k=1jdp[i1][k]dp[i][j]=\sum ^{j}_{k=1}dp\left[ i-1\right] \left[ k\right]
最后输出结果就好啦

代码如下:

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#define INF 0x3f3f3f3f
typedef long long ll;
using namespace std;
const int mx=1e3+10;
const int mod=1e9+7;
int n,m;
ll dp[30][mx];//长度为i,结尾为j的数量 
 
void init()
{
	for(int i=1;i<=n;i++)//初始化
	dp[1][i]=1;
	for(int i=2;i<=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;
		}
	}
}
 
int main()//求一个长度为2m的非递增序列 
{
	ll sum=0;
	scanf("%d%d",&n,&m);
    m*=2;
	init();
	for(int i=1;i<=n;i++)
	 sum=(sum+dp[m][i])%mod;
	printf("%lld\n",sum);
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!