1. 程式人生 > 實用技巧 >HDU 6880 Permutation Counting dp

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; }