Source Code
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
 
#define pb push_back
#define mk make_pair
#define ff first
#define ss second
#define fast ios::sync_with_stdio(false);cin.tie(0);
 
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef tree<int, null_type, less<int>,
        rb_tree_tag, tree_order_statistics_node_update> ordered_set;

const int mod = 1e9 + 7;

ll dp[1001][2005][2];
int n;
int calc(int idx, int cnt, bool done, string last){
	if(idx > n || cnt < 0) return 0;
	if(last == "((())") return 0;
	if(idx == n){
		if(!cnt && done)return 1;
		else return 0;
		}
	if(dp[idx][cnt][done] != -1) return dp[idx][cnt][done];
	ll &ret = dp[idx][cnt][done];
	ret = 0;
	string Last = "";
	if(last.size() <5) Last = last;
	else{
		for(int i=1;i<last.size();i++)
			Last += last[i];
		}
	ret = (ret + calc(idx+1, cnt+1, done, Last+"("))%mod; 
	ret = (ret + calc(idx+1, cnt-1, done, Last+")"))%mod;
	if(!done) ret = (ret + calc(idx + 5, cnt+1, 1, ""))%mod;
	return ret;
	}

int main(){
	fast
	cin>>n;
	memset(dp,-1,sizeof(dp));
	
	ll ans = 0;
	
	ans = (ans + calc(5,1,1, ""))%mod;
	ans = (ans + calc(1,1,0, "("))%mod;
	
	
	cout<<ans<<endl;
	
return 0;
}
Copy
Bracket Sequence Bahaa
GNU G++17
75 ms
32.6 MB
Wrong Answer