原題:
C. Two Arrays
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
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 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,兩個序列中的數字都是都是[1,n],兩個序列中對應下標的數要滿足,且a序列是非遞減的,b序列是非遞增的,問你這樣的序列對的個數。序列對數量的結果對1e9 +7 取模。
代碼:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int maxn = 1005;
const int maxm = 15;
const ll mod = 1e9 + 7;
int n,m;
int a[maxn][maxm],b[maxn][maxm];
int main()
{
ios::sync_with_stdio(false);
while(cin>>n>>m)
{
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
for(int i=1;i<=n;i++)
{
a[i][1]=b[i][1]=1;
}
for(int i=1;i<=m;i++)
{
b[n][i]=1;
}
for(int i=1;i<=n;i++)//num
{
for(int j=2;j<=m;j++)//len
{
for(int t=1;t<=i;t++)
{
a[i][j] = ((a[i][j]%mod) + (a[t][j-1]%mod))%mod;
}
}
}
/*
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
cout<<a[i][j]<<" ";
cout<<endl;
}
*/
for(int i=n-1;i>=1;i--)//num
{
for(int j=2;j<=m;j++)//len
{
for(int t=i;t<=n;t++)
{
b[i][j] = ((b[i][j]%mod) + (b[t][j-1]%mod))%mod;
}
}
}
int ans = 0;
for(int i = 1;i<=n;i++)
{
for(int j=i;j<=n;j++)
{
ans = ((ans% mod) + (a[i][m]%mod * b[j][m]%mod))%mod;
}
}
cout<<ans<<endl;
}
return 0;
}
解答:
這題紙上畫一畫很容易想出遞推方程,因爲序列a是非遞減的,序列b是非遞增的,而且又要滿足這樣的條件,那麼
設置狀態方程
表示序列長度爲j,且序列以i作爲結尾的時候,可以形成的非遞減序列的個數。那麼遞推方程可以寫成如下
同理,設置表示序列長度爲j,且序列以i作爲結尾的時候,可以形成非遞減序列的個數。
最後結果可以表示爲對所有和,其中要求