Source Code
#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define p1 2*p
#define p2 p1+1
#define ll long long
#define pb push_back
#define LFT p1,L,Mid
#define pf push_front
#define Mid ((L+R)/2)
#define RGT p2,Mid+1,R
#define pi pair<int,int>
#define pii pair<pi,pi>
#define deb(x) cout<<#x<<"="<<x<<endl
#define go ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
string wanted="((())";
const int mod=1e9+7;
int sum(int x,int y)
{
    if((x+=y)>=mod)
        x-=mod;
    return x;
}
int n,dp[1000][1000][4][2];
int Solve(int i,int j,int op,int k)
{
    if(j<0)
        return 0;
    if(i==n)
    {
        if(j!=0 || k!=1)
            return 0;
        return 1;
    }
    int &ret=dp[i][j][op][k];
    if(ret+1)
        return ret;
    ret=0;
    if(!op)
    {
        ret=sum(ret,Solve(i+1,j-1,0,k));
        ret=sum(ret,Solve(i+1,j+1,1,k));
    }
    else if(op==1)
    {
        ret=sum(ret,Solve(i+1,j+1,2,k));
        ret=sum(ret,Solve(i+1,j-1,0,k));
    }
    else if(op==2)
    {
        ret=sum(ret,Solve(i+1,j-1,0,k));
        ret=sum(ret,Solve(i+1,j+1,3,k));
    }
    else if(op==3)
    {
        ret=sum(ret,Solve(i+1,j+1,3,k));
        if(i+1==n)
            ret=sum(ret,Solve(i+1,j-1,0,k));
        else
        {
            ret=sum(ret,Solve(i+2,j-2,0,1));
            ret=sum(ret,Solve(i+2,j,1,k));
        }
    }
    return ret;
}
int main()
{
    go;
    cin>>n;
    if(n<6)
        return cout<<0,0;
    memset(dp,-1,sizeof dp);
    cout<<Solve(0,0,0,0);
    return 0;
}
Copy
Bracket Sequence Grapeee
GNU G++17
47 ms
32.5 MB
Accepted