C. Two Arrays(DIV2)

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;

}

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