题目
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有,所以由:
,得,所以有:
相当于构建一个长度为的非递减(或非递增)序列
然后求这个序列就是dp~(想到不状态转移方程那就没法= =)
令为长度为,结尾数字为的非递减序列数量
有 状态转移方程为:
最后输出结果就好啦
代码如下:
#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);
}
来源:CSDN
作者:楚仙子
链接:https://blog.csdn.net/weixin_43901733/article/details/103985410