Submission #43970

# Submission time Handle Problem Language Result Execution time Memory
43970 2018-03-29T01:24:40 Z model_code Security Gate (JOI18_security_gate) C++17
100 / 100
1794 ms 817784 KB
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=int(a);i<int(b);i++)
#define REP(i,b) FOR(i,0,b)
#define ZERO(x) memset(x,0,sizeof(x))
using ll=long long;

const int mod=1000000007;
void add(int&a,int b){
	a+=b;
	if(a>=mod)a-=mod;
}
void sub(int&a,int b){
	a-=b;
	if(a<0)a+=mod;
}
void mult(int&a,int b){
	a=ll(a)*b%mod;
}

const int Nmax=310;
int n,org[Nmax];

namespace ValidWithoutFlip{
	int dp[Nmax][Nmax];
	int Calc(){
		ZERO(dp);
		dp[0][0]=1;
		REP(i,n){
			if(org[i]>=0)
				REP(j,n)add(dp[i+1][j+1],dp[i][j]);
			if(org[i]<=0)
				FOR(j,1,n+1)add(dp[i+1][j-1],dp[i][j]);
		}
		int ans=dp[n][0];
		return ans;
	}
}

namespace InvalidOneSide{
	int gx[Nmax/2][Nmax][2][Nmax];
	int gz[Nmax][Nmax][2][Nmax];
	int wf[Nmax][Nmax];
	int CalcGivenAX1(int a,int x){
		int res=0;
		for(int len=0;len+1<=n;len+=2)if(org[len]<=0){
			int p=gx[x][len][1][0];
			int q=wf[len+1][2*a-1];
			mult(p,q);
			add(res,p);
		}
		return res;
	}
	int CalcGivenAX2(int a,int x){
		int res=0;
		for(int len=0;len+1<=n;len+=2)if(org[len]<=0){
			int p=gx[x][len][1][0];
			int q=gz[a+x][len+1][1][2*a-1];
			mult(p,q);
			add(res,p);
		}
		return res;
	}
	int CalcGivenAX(int a,int x){
		if(a<=x){
			return CalcGivenAX1(a,x);
		}else{
			return CalcGivenAX2(a,x);
		}
	}
	void PreCalc(){
		ZERO(gx);
		REP(x,n/2){
			gx[x][0][x==0][0]=1;
			REP(i,n){
				if(org[i]>=0){
					REP(j,x)
						add(gx[x][i+1][j+1==x][j+1],gx[x][i][0][j]);
					REP(j,x)
						add(gx[x][i+1][1][j+1],gx[x][i][1][j]);
				}
				if(org[i]<=0){
					FOR(j,1,x+1)
						add(gx[x][i+1][0][j-1],gx[x][i][0][j]);
					FOR(j,1,x+1)
						add(gx[x][i+1][1][j-1],gx[x][i][1][j]);
				}
			}
		}
		ZERO(gz);
		REP(z,n){
			gz[z][n][z==0][0]=1;
			for(int i=n-1;i>=0;i--){
				if(org[i]<=0){
					REP(j,n)
						add(gz[z][i][0][j+1],gz[z][i+1][0][j]);
					if(z>0)
						add(gz[z][i][1][z],gz[z][i+1][0][z-1]);
					FOR(j,z,min(z*2,n))
						add(gz[z][i][1][j+1],gz[z][i+1][1][j]);
				}
				if(org[i]>=0){
					FOR(j,1,n)
						add(gz[z][i][0][j-1],gz[z][i+1][0][j]);
					add(gz[z][i][1][z],gz[z][i+1][0][z+1]);
					FOR(j,z+2,min(z*2+1,n))
						add(gz[z][i][1][j-1],gz[z][i+1][1][j]);
				}
			}
		}
		ZERO(wf);
		wf[n][0]=1;
		for(int i=n-1;i>=0;i--){
			if(org[i]<=0){
				REP(j,n)
					add(wf[i][j+1],wf[i+1][j]);
			}
			if(org[i]>=0){
				FOR(j,1,n)
					add(wf[i][j-1],wf[i+1][j]);
			}
		}
	}
	int Calc(){
		PreCalc();
		int res=0;
		FOR(a,1,n/2+1)REP(x,n/2-a+1)add(res,CalcGivenAX(a,x));
		return res;
	}
}

namespace InvalidBothSide{
	int gx[Nmax/2][Nmax][2][Nmax];
	int gz[Nmax/2][Nmax][3][Nmax*2];
	int CalcGivenAX(int a,int x){
		int res=0;
		for(int len=0;len+1<=n;len+=2)if(org[len]<=0){
			int p=gx[x][len][1][0];
			int q=gz[a+x][len+1][2][a*2-1+n];
			mult(p,q);
			add(res,p);
		}
		return res;
	}
	void PreCalc(bool inc){
		ZERO(gx);
		REP(x,n/2){
			gx[x][0][x==0][0]=1;
			REP(i,n){
				if(org[i]>=0){
					REP(j,x)
						add(gx[x][i+1][j+1==x][j+1],gx[x][i][0][j]);
					REP(j,x)
						add(gx[x][i+1][1][j+1],gx[x][i][1][j]);
				}
				if(org[i]<=0){
					FOR(j,1,x)
						add(gx[x][i+1][0][j-1],gx[x][i][0][j]);
					FOR(j,1,x+1)
						add(gx[x][i+1][1][j-1],gx[x][i][1][j]);
				}
			}
		}
		ZERO(gz);
		REP(z,n/2){
			gz[z][n][(z+inc)==0][0+n]=1;
			for(int i=n-1;i>=0;i--){
				if(org[i]<=0){
					REP(j,n)
						add(gz[z][i][j+1==z+inc][j+1+n],gz[z][i+1][0][j+n]);
					REP(j,n)
						add(gz[z][i][1][j+1+n],gz[z][i+1][1][j+n]);
					FOR(j,-n,min(z*2,n))
						add(gz[z][i][2][j+1+n],gz[z][i+1][2][j+n]);
				}
				if(org[i]>=0){
					FOR(j,1,n)
						add(gz[z][i][0][j-1+n],gz[z][i+1][0][j+n]);
					REP(j,n)
						add(gz[z][i][(j-1)==-1?2:1][j-1+n],gz[z][i+1][1][j+n]);
					FOR(j,-n+1,min(z*2+1,n))
						add(gz[z][i][2][j-1+n],gz[z][i+1][2][j+n]);
				}
			}
		}
	}
	int Calc(){
		int res=0;
		REP(_,2){
			PreCalc(_);
			FOR(a,-n/2+1,n/2)FOR(x,max(0,-a),n/2-abs(a)+1)add(res,CalcGivenAX(a,x));
			reverse(org,org+n);
			REP(i,n)org[i]*=-1;
		}
		return res;
	}
}

int Solve(){
	int ans=0;
	add(ans,ValidWithoutFlip::Calc());
	REP(_,2){
		add(ans,InvalidOneSide::Calc());
		reverse(org,org+n);
		REP(i,n)org[i]*=-1;
	}
	add(ans,InvalidBothSide::Calc());
	return ans;
}

int main(){
	cin>>n;
	string str;
	cin>>str;
	if(n%2==1){
		cout<<0<<endl;
		return 0;
	}
	REP(i,n){
		if(str[i]=='(')org[i]=1;
		if(str[i]==')')org[i]=-1;
	}
	cout<<Solve()<<endl;
}
# Verdict Execution time Memory Grader output
1 Correct 714 ms 817324 KB Output is correct
2 Correct 739 ms 817472 KB Output is correct
3 Correct 2 ms 817472 KB Output is correct
4 Correct 730 ms 817472 KB Output is correct
5 Correct 711 ms 817504 KB Output is correct
6 Correct 713 ms 817512 KB Output is correct
7 Correct 711 ms 817728 KB Output is correct
8 Correct 734 ms 817728 KB Output is correct
9 Correct 737 ms 817728 KB Output is correct
10 Correct 740 ms 817728 KB Output is correct
11 Correct 714 ms 817728 KB Output is correct
12 Correct 733 ms 817728 KB Output is correct
13 Correct 769 ms 817728 KB Output is correct
14 Correct 791 ms 817728 KB Output is correct
15 Correct 859 ms 817728 KB Output is correct
16 Correct 817 ms 817728 KB Output is correct
17 Correct 741 ms 817728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 714 ms 817324 KB Output is correct
2 Correct 739 ms 817472 KB Output is correct
3 Correct 2 ms 817472 KB Output is correct
4 Correct 730 ms 817472 KB Output is correct
5 Correct 711 ms 817504 KB Output is correct
6 Correct 713 ms 817512 KB Output is correct
7 Correct 711 ms 817728 KB Output is correct
8 Correct 734 ms 817728 KB Output is correct
9 Correct 737 ms 817728 KB Output is correct
10 Correct 740 ms 817728 KB Output is correct
11 Correct 714 ms 817728 KB Output is correct
12 Correct 733 ms 817728 KB Output is correct
13 Correct 769 ms 817728 KB Output is correct
14 Correct 791 ms 817728 KB Output is correct
15 Correct 859 ms 817728 KB Output is correct
16 Correct 817 ms 817728 KB Output is correct
17 Correct 741 ms 817728 KB Output is correct
18 Correct 776 ms 817728 KB Output is correct
19 Correct 760 ms 817728 KB Output is correct
20 Correct 784 ms 817728 KB Output is correct
21 Correct 782 ms 817728 KB Output is correct
22 Correct 806 ms 817728 KB Output is correct
23 Correct 821 ms 817728 KB Output is correct
24 Correct 826 ms 817776 KB Output is correct
25 Correct 875 ms 817776 KB Output is correct
26 Correct 851 ms 817776 KB Output is correct
27 Correct 814 ms 817776 KB Output is correct
28 Correct 810 ms 817776 KB Output is correct
29 Correct 849 ms 817776 KB Output is correct
30 Correct 827 ms 817776 KB Output is correct
31 Correct 829 ms 817776 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 714 ms 817324 KB Output is correct
2 Correct 739 ms 817472 KB Output is correct
3 Correct 2 ms 817472 KB Output is correct
4 Correct 730 ms 817472 KB Output is correct
5 Correct 711 ms 817504 KB Output is correct
6 Correct 713 ms 817512 KB Output is correct
7 Correct 711 ms 817728 KB Output is correct
8 Correct 734 ms 817728 KB Output is correct
9 Correct 737 ms 817728 KB Output is correct
10 Correct 740 ms 817728 KB Output is correct
11 Correct 714 ms 817728 KB Output is correct
12 Correct 733 ms 817728 KB Output is correct
13 Correct 769 ms 817728 KB Output is correct
14 Correct 791 ms 817728 KB Output is correct
15 Correct 859 ms 817728 KB Output is correct
16 Correct 817 ms 817728 KB Output is correct
17 Correct 741 ms 817728 KB Output is correct
18 Correct 776 ms 817728 KB Output is correct
19 Correct 760 ms 817728 KB Output is correct
20 Correct 784 ms 817728 KB Output is correct
21 Correct 782 ms 817728 KB Output is correct
22 Correct 806 ms 817728 KB Output is correct
23 Correct 821 ms 817728 KB Output is correct
24 Correct 826 ms 817776 KB Output is correct
25 Correct 875 ms 817776 KB Output is correct
26 Correct 851 ms 817776 KB Output is correct
27 Correct 814 ms 817776 KB Output is correct
28 Correct 810 ms 817776 KB Output is correct
29 Correct 849 ms 817776 KB Output is correct
30 Correct 827 ms 817776 KB Output is correct
31 Correct 829 ms 817776 KB Output is correct
32 Correct 793 ms 817776 KB Output is correct
33 Correct 784 ms 817776 KB Output is correct
34 Correct 868 ms 817776 KB Output is correct
35 Correct 801 ms 817776 KB Output is correct
36 Correct 772 ms 817776 KB Output is correct
37 Correct 746 ms 817776 KB Output is correct
38 Correct 803 ms 817776 KB Output is correct
39 Correct 795 ms 817776 KB Output is correct
40 Correct 862 ms 817776 KB Output is correct
41 Correct 817 ms 817776 KB Output is correct
42 Correct 822 ms 817776 KB Output is correct
43 Correct 808 ms 817776 KB Output is correct
44 Correct 843 ms 817776 KB Output is correct
45 Correct 796 ms 817776 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 714 ms 817324 KB Output is correct
2 Correct 739 ms 817472 KB Output is correct
3 Correct 2 ms 817472 KB Output is correct
4 Correct 730 ms 817472 KB Output is correct
5 Correct 711 ms 817504 KB Output is correct
6 Correct 713 ms 817512 KB Output is correct
7 Correct 711 ms 817728 KB Output is correct
8 Correct 734 ms 817728 KB Output is correct
9 Correct 737 ms 817728 KB Output is correct
10 Correct 740 ms 817728 KB Output is correct
11 Correct 714 ms 817728 KB Output is correct
12 Correct 733 ms 817728 KB Output is correct
13 Correct 769 ms 817728 KB Output is correct
14 Correct 791 ms 817728 KB Output is correct
15 Correct 859 ms 817728 KB Output is correct
16 Correct 817 ms 817728 KB Output is correct
17 Correct 741 ms 817728 KB Output is correct
18 Correct 776 ms 817728 KB Output is correct
19 Correct 760 ms 817728 KB Output is correct
20 Correct 784 ms 817728 KB Output is correct
21 Correct 782 ms 817728 KB Output is correct
22 Correct 806 ms 817728 KB Output is correct
23 Correct 821 ms 817728 KB Output is correct
24 Correct 826 ms 817776 KB Output is correct
25 Correct 875 ms 817776 KB Output is correct
26 Correct 851 ms 817776 KB Output is correct
27 Correct 814 ms 817776 KB Output is correct
28 Correct 810 ms 817776 KB Output is correct
29 Correct 849 ms 817776 KB Output is correct
30 Correct 827 ms 817776 KB Output is correct
31 Correct 829 ms 817776 KB Output is correct
32 Correct 793 ms 817776 KB Output is correct
33 Correct 784 ms 817776 KB Output is correct
34 Correct 868 ms 817776 KB Output is correct
35 Correct 801 ms 817776 KB Output is correct
36 Correct 772 ms 817776 KB Output is correct
37 Correct 746 ms 817776 KB Output is correct
38 Correct 803 ms 817776 KB Output is correct
39 Correct 795 ms 817776 KB Output is correct
40 Correct 862 ms 817776 KB Output is correct
41 Correct 817 ms 817776 KB Output is correct
42 Correct 822 ms 817776 KB Output is correct
43 Correct 808 ms 817776 KB Output is correct
44 Correct 843 ms 817776 KB Output is correct
45 Correct 796 ms 817776 KB Output is correct
46 Correct 722 ms 817776 KB Output is correct
47 Correct 810 ms 817776 KB Output is correct
48 Correct 2 ms 817776 KB Output is correct
49 Correct 824 ms 817776 KB Output is correct
50 Correct 792 ms 817776 KB Output is correct
51 Correct 805 ms 817776 KB Output is correct
52 Correct 825 ms 817776 KB Output is correct
53 Correct 845 ms 817776 KB Output is correct
54 Correct 833 ms 817776 KB Output is correct
55 Correct 790 ms 817776 KB Output is correct
56 Correct 781 ms 817776 KB Output is correct
57 Correct 803 ms 817776 KB Output is correct
58 Correct 812 ms 817776 KB Output is correct
59 Correct 833 ms 817784 KB Output is correct
60 Correct 862 ms 817784 KB Output is correct
61 Correct 798 ms 817784 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 714 ms 817324 KB Output is correct
2 Correct 739 ms 817472 KB Output is correct
3 Correct 2 ms 817472 KB Output is correct
4 Correct 730 ms 817472 KB Output is correct
5 Correct 711 ms 817504 KB Output is correct
6 Correct 713 ms 817512 KB Output is correct
7 Correct 711 ms 817728 KB Output is correct
8 Correct 734 ms 817728 KB Output is correct
9 Correct 737 ms 817728 KB Output is correct
10 Correct 740 ms 817728 KB Output is correct
11 Correct 714 ms 817728 KB Output is correct
12 Correct 733 ms 817728 KB Output is correct
13 Correct 769 ms 817728 KB Output is correct
14 Correct 791 ms 817728 KB Output is correct
15 Correct 859 ms 817728 KB Output is correct
16 Correct 817 ms 817728 KB Output is correct
17 Correct 741 ms 817728 KB Output is correct
18 Correct 776 ms 817728 KB Output is correct
19 Correct 760 ms 817728 KB Output is correct
20 Correct 784 ms 817728 KB Output is correct
21 Correct 782 ms 817728 KB Output is correct
22 Correct 806 ms 817728 KB Output is correct
23 Correct 821 ms 817728 KB Output is correct
24 Correct 826 ms 817776 KB Output is correct
25 Correct 875 ms 817776 KB Output is correct
26 Correct 851 ms 817776 KB Output is correct
27 Correct 814 ms 817776 KB Output is correct
28 Correct 810 ms 817776 KB Output is correct
29 Correct 849 ms 817776 KB Output is correct
30 Correct 827 ms 817776 KB Output is correct
31 Correct 829 ms 817776 KB Output is correct
32 Correct 793 ms 817776 KB Output is correct
33 Correct 784 ms 817776 KB Output is correct
34 Correct 868 ms 817776 KB Output is correct
35 Correct 801 ms 817776 KB Output is correct
36 Correct 772 ms 817776 KB Output is correct
37 Correct 746 ms 817776 KB Output is correct
38 Correct 803 ms 817776 KB Output is correct
39 Correct 795 ms 817776 KB Output is correct
40 Correct 862 ms 817776 KB Output is correct
41 Correct 817 ms 817776 KB Output is correct
42 Correct 822 ms 817776 KB Output is correct
43 Correct 808 ms 817776 KB Output is correct
44 Correct 843 ms 817776 KB Output is correct
45 Correct 796 ms 817776 KB Output is correct
46 Correct 722 ms 817776 KB Output is correct
47 Correct 810 ms 817776 KB Output is correct
48 Correct 2 ms 817776 KB Output is correct
49 Correct 824 ms 817776 KB Output is correct
50 Correct 792 ms 817776 KB Output is correct
51 Correct 805 ms 817776 KB Output is correct
52 Correct 825 ms 817776 KB Output is correct
53 Correct 845 ms 817776 KB Output is correct
54 Correct 833 ms 817776 KB Output is correct
55 Correct 790 ms 817776 KB Output is correct
56 Correct 781 ms 817776 KB Output is correct
57 Correct 803 ms 817776 KB Output is correct
58 Correct 812 ms 817776 KB Output is correct
59 Correct 833 ms 817784 KB Output is correct
60 Correct 862 ms 817784 KB Output is correct
61 Correct 798 ms 817784 KB Output is correct
62 Correct 1230 ms 817784 KB Output is correct
63 Correct 1180 ms 817784 KB Output is correct
64 Correct 971 ms 817784 KB Output is correct
65 Correct 906 ms 817784 KB Output is correct
66 Correct 1737 ms 817784 KB Output is correct
67 Correct 1547 ms 817784 KB Output is correct
68 Correct 1794 ms 817784 KB Output is correct
69 Correct 946 ms 817784 KB Output is correct
70 Correct 1022 ms 817784 KB Output is correct
71 Correct 864 ms 817784 KB Output is correct
72 Correct 908 ms 817784 KB Output is correct
73 Correct 1744 ms 817784 KB Output is correct
74 Correct 1445 ms 817784 KB Output is correct