Source Code
#include<bits/stdc++.h>

using namespace std;
typedef long long ll;

#define FAST ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define mp make_pair
#define pb push_back
#define lp(i,s,f) for(ll i = s; i < ll(f); i++)
#define inF freopen("input.in", "r", stdin);
#define outF freopen("output.in", "w", stdout);
#define endl '\n'
#define MOD 1000000007
#define mm(arr) memset(arr, 0, sizeof(arr))
#define F first
#define S second

const long double PI = atan(1) * 4.0;

const int N = 1001;
const int M = 5;
int dp[N][N][(1 << M)];
int n;
int calc(int ind, int cnt, int mask){
    if(ind == n){
        return ind >= 5 && mask == 7 && cnt == 0;
    }
    if(cnt < 0){
        return 0;
    }
    if(dp[ind][cnt][mask] != -1){
        return dp[ind][cnt][mask];
    }
    int new_mask = 0;
    for(int i = 1; i < 5; i++){
        if((1 << i)&mask){
            new_mask |= (1 << (i - 1));
        }
    }
    return dp[ind][cnt][mask] = (calc(ind + 1, cnt + 1, (mask == 7 && ind >= 5) ? mask : (new_mask | (1 << 4))) + calc(ind + 1, cnt - 1, (mask == 7 && ind >= 5) ? mask : new_mask))%MOD;
}
int main(){
    FAST
    cin >> n;
    memset(dp, -1, sizeof(dp));
    cout << calc(0, 0, 0) << endl;
    return 0;
}
Copy
Bracket Sequence Basilhijaz
GNU G++17
324 ms
126.6 MB
Accepted