http://acm.hdu.edu.cn/showproblem.php?pid=6880
根据长度为n的排列a,构造长度n-1的序列b
思路:DP
官方题解:
代码:
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<bitset> #include<cassert> #include<cctype> #include<cmath> #include<cstdlib> #include<ctime> #include<deque> #include<iomanip> #include<list> #include<map> #include<queue> #include<set> #include<stack> #include<vector> #include <vector> #include <iterator> #include <utility> #include <sstream> #include <limits> #include <numeric> #include <functional> using namespace std; #define gc getchar() #define mem(a) memset(a,0,sizeof(a)) #define debug(x) cout<<"debug:"<<#x<<" = "<<x<<endl; #define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef pair<int,int> pii; typedef char ch; typedef double db; const double PI=acos(-1.0); const double eps=1e-6; const int inf=0x3f3f3f3f; //const int maxn=1e5+10; const int maxn = 5010; //const int maxm=100+10; const int N=1e6+10; const int mod=1e9+7; int A[maxn] = {0}; int P[maxn][maxn] = {0}; int main() { int n = 0 , ans = 0; int T = 0; cin >> T; while(T--) { cin >> n; for(int i = 0;i < n;i++) { if(i < n-1) { cin >> A[i]; } for(int j = 0;j < n;j++) { P[i][j] = 0; } } P[1][0] = A[0]; P[1][1] = !A[0]; for(int i = 1;i < n-1;i++) { if(A[i]) { for(int j = 0;j < i;j++) { P[i+1][i-j] = (P[i+1][i-j+1] + P[i][i-j]) % mod; } P[i+1][i+1] = 0; } else { P[i+1][1] = 0; for(int j = 1;j < i+1;j++) { P[i+1][j] = (P[i][j-1] + P[i+1][j-1]) % mod; } } } for(int i = 0;i < n;i++) { ans = (ans + P[n][i]) % mod; } cout << ans << endl; } return 0; }