cf Educational Codeforces Round 80 C. Two Arrays

原题:

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],两个序列中对应下标的数要满足aibia_i ≤ b_i,且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是非递增的,而且又要满足aibia_i ≤ b_i这样的条件,那么
设置状态方程
a[i][j]a[i][j]表示序列长度为j,且序列以i作为结尾的时候,可以形成的非递减序列的个数。那么递推方程可以写成如下

a[i][j]=p=t=1ia[t][j1]a[i][j] = p = \sum\limits_{t=1}^ia[t][j-1]

同理,设置b[i][j]b[i][j]表示序列长度为j,且序列以i作为结尾的时候,可以形成非递减序列的个数。

b[i][j]=p=t=inb[t][j1]b[i][j] = p = \sum\limits_{t=i}^nb[t][j-1]

最后结果可以表示为对所有a[i][m]b[j][m]a[i][m] * b[j][m]和,其中要求j>=ij>=i

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章