传送门:HDU - 6880
思路分析
设$dp[i][j]$表示前$i$个数且末尾为$j$的排列数
当我们将$dp[i-1][..]->dp[i][j]$的时候,实际上是看做从$i-1$的排列里把$j$换到最后一个位置,并把$i$插入到原排列且满足限制,解决办法就是将前$i-1$中大于等于$j$的数全部加一,这就就刚好空出来一个$j$,且满足限制
AC代码
#include <bits/stdc++.h>
#define fi first
#define se second
#define ll long long
#define pb push_back
#define mp make_pair
#define fun function
#define sz(x) (x).size()
#define lowbit(x) (x)&(-x)
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
namespace FastIO {
#define BUF_SIZE 100000
#define OUT_SIZE 100000
bool IOerror=0;
inline char nc() {
static char buf[BUF_SIZE],*p1=buf+BUF_SIZE,*pend=buf+BUF_SIZE;
if(p1==pend) {
p1=buf;
pend=buf+fread(buf,1,BUF_SIZE,stdin);
if(pend==p1) {
IOerror=1;
return -1;
}
}
return *p1++;
}
inline bool blank(char ch) {
return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';
}
template<class T> inline bool read(T &x) {
bool sign=0;
char ch=nc();
x=0;
for(; blank(ch); ch=nc());
if(IOerror)return false;
if(ch=='-')sign=1,ch=nc();
for(; ch>='0'&&ch<='9'; ch=nc())x=x*10+ch-'0';
if(sign)x=-x;
return true;
}
template<class T,class... U>bool read(T& h,U&... t) {
return read(h)&&read(t...);
}
#undef OUT_SIZE
#undef BUF_SIZE
};
using namespace std;
using namespace FastIO;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int INF = 0x3f3f3f3f;
const int N = 5e3+10;
const int mod = 1e9+7;
#define int long long
int b[N],dp[N][N];
signed main() {
#ifdef xiaofan
freopen("1.in","r",stdin);
freopen("1.out","w",stdout);
#endif
int T;
cin>>T;
while(T--){
int n;
cin>>n;
for(int i=2;i<=n;i++) cin>>b[i];
for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) dp[i][j]=0;
dp[1][1]=1;
for(int i=2;i<=n;i++){
int sum = 0;
//1 small
//0 big
if(b[i]){
for(int j=i-1;j;j--){
sum = (sum + dp[i-1][j])%mod;
dp[i][j] = (dp[i][j]+sum)%mod;
}
}else{
for(int j=2;j<=i;j++){
sum = (sum+dp[i-1][j-1])%mod;
dp[i][j] = (dp[i][j]+sum)%mod;
}
}
}
int ans = 0;
for(int i=1;i<=n;i++) ans = (ans + dp[n][i])%mod;
cout<<ans<<endl;
}
return 0;
}