C. Two Arrays----思维/dp

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
inputCopy
2 2
outputCopy
5
inputCopy
10 1
outputCopy
55
inputCopy
723 9
outputCopy
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数组是非递减的,am就是最大的
b数组是非递增的,bm就是最小的
根据题意ai<=bi,那么am<=bm
那么我们把a,b数组连接起来,怎么连呢?遍历正序a,遍历反序b.
如:a1,a2,a3…am,bm,bm-1…b1;
那么就构成一个2*m长度的新序列,是一个非递减的,每个数的取值范围[1,n].
设f[i][j]
一维:第i个数
二维:第一个数位j
状态方程:f[i][j]=f[i][j+1]+f[i-1][j] .
因为是一个非递减的序列
第i个数为j+1,那么第i-1个数必定为j

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+100;
const int MOD=1e9+7;
typedef long long ll;
ll f[100][1100];
int n,m;
int main()
{
	scanf("%d %d",&n,&m);
	for(int i=1;i<=n;i++) f[1][i]=1; //初始化
	for(int i=2;i<=2*m;i++)
	{
		for(int j=n;j>0;j--)
			f[i][j]=(f[i][j+1]+f[i-1][j])%MOD;
	}
	ll ans=0;
	for(int i=1;i<=n;i++)
	{
		ans=(ans+f[2*m][i])%MOD;//在该长度为2m数组中最后一个位置放1~n的情况之和
	}
	cout<<ans<<endl;
}

你可能感兴趣的:(思维,DP)