HDU 6880 Permutation Counting dp

题意:

给你一个n和一个长度为n-1的由0/1构成的b序列

你需要从[1,n]中构造出来一个满足b序列的序列

我们设使用[1,n]构成的序列为a,那么如果ai>ai+1,那么bi=1,否则bi=0

问你你可以构造出来多少满足b序列的序列a

代码:

看官方题解

 

代码:

#include<stack>
#include<queue>
#include<map>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#define fi first
#define se second
#define pb push_back
using namespace std;
typedef long long ll;
const int maxn=2000+10;
const int mod=1e9+7;
const double eps=1e-8;
const int INF = 0x3f3f3f3f;
int a[5010],dp[5010][5010];
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        for(int i=1; i<=n-1; i++)
        {
            cin>>a[i];
        }
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=n; j++)
            {
                dp[i][j]=0;
            }
        }
        if(a[1]==1)
        {
            dp[2][1]=1;
            dp[2][2]=0;
        }
        else
        {
            dp[2][2]=1;
            dp[2][1]=0;
        }
        for(int i=2; i<=n-1; i++)
        {
            if(a[i]==1)
            {
                for(int j=i; j>=1; j--)
                {
                    dp[i+1][j]=(dp[i+1][j+1]+dp[i][j])%mod;
                }
            }
            else
            {
                dp[i+1][1]=0;  
                for(int j=2; j<=i+1; j++)
                {
                    dp[i+1][j]=(dp[i][j-1]+dp[i+1][j-1])%mod;
                }
            }
        }
        int res=0;
        for(int i=1; i<=n; i++) res=(res+dp[n][i])%mod;
        cout<<res<<endl;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/kongbursi-2292702937/p/13545556.html