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.
input
10 1
output
55
有给一个n一个m,然后给两个数组a,b。求满足
1,数组的长度都是m
2,数组内的元素都不超过n
3,a数组递增,b数组递减
4,a[i]<b[i],即a[m]<b[m]
通过观察,a数组是递增的,b数组是递减的,b数组逆置过来就是递增的,而且b[m]>a[m],逆置过来之后b[m]就变成了b[1],b[1]大于等于a[m],所以相连之后就变成了一个长度为2m,整体不递减的序列,所以我们就求一个长度为2m,数的范围在1-n之内的所有符合条件的序列。
这里我们定义dp[ i ][ j ]为长度为i,最后一个数是j的情况下的最优解,所以我们可以得到状态转移方程 dp[ i ][ j ]=dp[ i ][ j ]+dp[i-1][1-j];就是说长度为i,最后一个数是j时候的解是由长度为i-1,最后一个数小于j的所有状态转移过来的。最后在把长度为2m的所有情况加上即使答案。
AC代码:
#include<bits/stdc++.h>
#include<bitset>
#include<unordered_map>
#define pb push_back
#define bp __builtin_popcount
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const int maxn=1e3+100;
const int MOD=1e9+7;
int lowbit(int x){return x&-x;}
inline ll dpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t) % MOD; b >>= 1; t = (t*t) % MOD; }return r; }
inline ll fpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t); b >>= 1; t = (t*t); }return r; }
ll dp[maxn][maxn];//dp[i][j]长度为i,以j结尾的最优解
int main()
{
ios::sync_with_stdio(false);
int n,m;
cin>>n>>m;
for(int i=1;i<=n;i++)
dp[1][i]=1;
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;
//system("pause");
return 0;
}