Source Code
/// Zengy Manga
#pragma GCC optimize("O3")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("avx,avx2,fma")
using namespace std;

#include "bits/stdc++.h"
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/rope>

using namespace __gnu_pbds;
using namespace __gnu_cxx;

using ll = long long;
using ii = pair<int, int>;

template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define pb push_back
#define F first
#define S second
#define f(i, a, b)  for(int i = a; i < b; i++)
#define all(a)  a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define sz(x) (int)(x).size()
#define mp make_pair
#define popCnt(x) (__builtin_popcountll(x))
#define int ll

const int N = 1e6+5, LG = log2(N) + 1, MOD = 1e9 + 7;
const double PI = acos(-1);
vector<int> v;
string str;
vector<int> ans;
const string pat = "((())#";
int dp[1005][1005][7];
int solve(int index, int cbs, int match) {
    if(cbs < 0)return 0;
    if(!index)
        return (cbs==0&&match==5);
    int&ret=dp[index][cbs][match];
    if(~ret)
        return ret;
    int nmatch1=match, nmatch2=match;
    if(nmatch1!=5)nmatch1=(pat[match]=='(' ? match + 1 : (match == 3 ? match : 1));
    if(nmatch2!=5)nmatch2=(pat[match]==')' ? match + 1 : 0);
    ret = solve(index-1,cbs+1,nmatch1);
    ret += solve(index-1,cbs-1,nmatch2);
    if(ret>=MOD)ret -= MOD;
    return ret;
}
void doWork() {

    int n;
    cin >> n;
    memset(dp,-1,sizeof dp);
    cout << solve(n,0,0) << '\n';

}
int32_t main() {
#ifdef ONLINE_JUDGE
    ios_base::sync_with_stdio(0);
    cin.tie(0);
#endif // ONLINE_JUDGE

    int t = 1;
//    cin >> t;
    while (t--) {
        doWork();
    }


    return 0;
}
Copy
Bracket Sequence DeadlyPillow
GNU G++17
78 ms
56.5 MB
Accepted