Submission #1020138

# Submission time Handle Problem Language Result Execution time Memory
1020138 2024-07-11T15:30:19 Z bachhoangxuan Security Gate (JOI18_security_gate) C++17
100 / 100
1184 ms 227668 KB
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int mod=1e9+7;
const int maxn = 305;

int n,ans;
char s[maxn];
int f[maxn][maxn][maxn],g[maxn][2*maxn][3];
int F[maxn][maxn];

bool ok(int i,int x){
    if(s[i]=='(' && x==-1) return false;
    if(s[i]==')' && x==1) return false;
    return true;
};

void dp1(){
    F[0][0]=1;
    for(int i=1;i<=n;i++){
        for(int j=0;j<i;j++) for(int x:{-1,1}){
            if(!ok(i,x)) continue;
            int nj=j+x;
            if(nj>=0) F[i][nj]=(F[i][nj]+F[i-1][j])%mod;
        }
    }
    ans=(ans+F[n][0])%mod;
    //cout << ans << '\n';
}
void dp2(){
    memset(F,0,sizeof(F));
    memset(f,0,sizeof(f));

    f[0][0][0]=1;
    for(int i=1;i<=n;i++) for(int j=0;j<i;j++) for(int k=0;k<i;k++){
        for(int x:{-1,1}){
            if(!ok(i,x)) continue;
            int nj=j+x,nk=max(k,nj);
            if(nj>=0) f[i][nj][nk]=(f[i][nj][nk]+f[i-1][j][k])%mod;
            if(nj==-1) F[i][nk]=(F[i][nk]+f[i-1][j][k])%mod;
        }
    }
    for(int t=1;t<=n;t++){
        memset(g,0,sizeof(g));
        g[n+1][0][0]=1;
        for(int i=n;i>=1;i--) for(int j=0;j<=(n-i);j++){
            for(int x:{-1,1}){
                if(!ok(i,-x)) continue;
                int nj=j+x;
                int *a=g[i][nj],*b=g[i+1][j];
                if(nj>=0) a[0]=(a[0]+b[0])%mod;
                if(nj==t) a[1]=(a[1]+b[0])%mod;
                else if(nj<=2*t) a[1]=(a[1]+b[1])%mod;
            }
        }
        for(int a=0;a<t;a++){
            int b=(a-t)*2;
            for(int i=1;i<=n;i++) ans=(ans+F[i][a]*g[i+1][-1-b][a+b/2<0])%mod;
        }
    }
    //cout << ans << '\n';
}
void dp3(int d){
    for(int t=0;t<=n;t++){
        memset(g,0,sizeof(g));
        g[n+1][n][0]=1;
        if(t+d==0) g[n+1][n][1]=1;
        for(int i=n;i>=1;i--) for(int j=-(n-i);j<=(n-i);j++){
            for(int x:{-1,1}){
                if(!ok(i,-x)) continue;
                int nj=j+x;
                int *a=g[i][nj+n],*b=g[i+1][j+n];
                if(nj>=0) a[0]=(a[0]+b[0])%mod;
                if(nj==t+d) a[1]=(a[1]+b[0])%mod;
                else if(nj>=0) a[1]=(a[1]+b[1])%mod;
                if(nj==-1) a[2]=(a[2]+b[1])%mod;
                if(nj<=2*t) a[2]=(a[2]+b[2])%mod;
            }
        }
        for(int a=0;a<=n;a++){
            int b=2*(a-t);
            if(b>=-n && b<n) for(int i=1;i<=n;i++) ans=(ans+F[i][a]*g[i+1][-1-b+n][2])%mod;
        }
    }
    //cout << ans << '\n';
}

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
    cin >> n;
    for(int i=1;i<=n;i++) cin >> s[i];
    dp1();dp2();dp3(0);
    reverse(s+1,s+n+1);
    for(int i=1;i<=n;i++) if(s[i]!='x') s[i]='('+')'-s[i];
    dp2();dp3(1);
    cout << ans << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 116 ms 227408 KB Output is correct
2 Correct 180 ms 227412 KB Output is correct
3 Correct 179 ms 227412 KB Output is correct
4 Correct 114 ms 227548 KB Output is correct
5 Correct 127 ms 227408 KB Output is correct
6 Correct 128 ms 227668 KB Output is correct
7 Correct 128 ms 227408 KB Output is correct
8 Correct 182 ms 227536 KB Output is correct
9 Correct 179 ms 227412 KB Output is correct
10 Correct 184 ms 227412 KB Output is correct
11 Correct 127 ms 227412 KB Output is correct
12 Correct 154 ms 227560 KB Output is correct
13 Correct 148 ms 227412 KB Output is correct
14 Correct 146 ms 227408 KB Output is correct
15 Correct 178 ms 227412 KB Output is correct
16 Correct 182 ms 227412 KB Output is correct
17 Correct 187 ms 227496 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 116 ms 227408 KB Output is correct
2 Correct 180 ms 227412 KB Output is correct
3 Correct 179 ms 227412 KB Output is correct
4 Correct 114 ms 227548 KB Output is correct
5 Correct 127 ms 227408 KB Output is correct
6 Correct 128 ms 227668 KB Output is correct
7 Correct 128 ms 227408 KB Output is correct
8 Correct 182 ms 227536 KB Output is correct
9 Correct 179 ms 227412 KB Output is correct
10 Correct 184 ms 227412 KB Output is correct
11 Correct 127 ms 227412 KB Output is correct
12 Correct 154 ms 227560 KB Output is correct
13 Correct 148 ms 227412 KB Output is correct
14 Correct 146 ms 227408 KB Output is correct
15 Correct 178 ms 227412 KB Output is correct
16 Correct 182 ms 227412 KB Output is correct
17 Correct 187 ms 227496 KB Output is correct
18 Correct 137 ms 227408 KB Output is correct
19 Correct 177 ms 227412 KB Output is correct
20 Correct 124 ms 227408 KB Output is correct
21 Correct 170 ms 227412 KB Output is correct
22 Correct 191 ms 227560 KB Output is correct
23 Correct 179 ms 227408 KB Output is correct
24 Correct 178 ms 227604 KB Output is correct
25 Correct 158 ms 227412 KB Output is correct
26 Correct 129 ms 227408 KB Output is correct
27 Correct 157 ms 227544 KB Output is correct
28 Correct 122 ms 227404 KB Output is correct
29 Correct 180 ms 227408 KB Output is correct
30 Correct 174 ms 227412 KB Output is correct
31 Correct 187 ms 227668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 116 ms 227408 KB Output is correct
2 Correct 180 ms 227412 KB Output is correct
3 Correct 179 ms 227412 KB Output is correct
4 Correct 114 ms 227548 KB Output is correct
5 Correct 127 ms 227408 KB Output is correct
6 Correct 128 ms 227668 KB Output is correct
7 Correct 128 ms 227408 KB Output is correct
8 Correct 182 ms 227536 KB Output is correct
9 Correct 179 ms 227412 KB Output is correct
10 Correct 184 ms 227412 KB Output is correct
11 Correct 127 ms 227412 KB Output is correct
12 Correct 154 ms 227560 KB Output is correct
13 Correct 148 ms 227412 KB Output is correct
14 Correct 146 ms 227408 KB Output is correct
15 Correct 178 ms 227412 KB Output is correct
16 Correct 182 ms 227412 KB Output is correct
17 Correct 187 ms 227496 KB Output is correct
18 Correct 137 ms 227408 KB Output is correct
19 Correct 177 ms 227412 KB Output is correct
20 Correct 124 ms 227408 KB Output is correct
21 Correct 170 ms 227412 KB Output is correct
22 Correct 191 ms 227560 KB Output is correct
23 Correct 179 ms 227408 KB Output is correct
24 Correct 178 ms 227604 KB Output is correct
25 Correct 158 ms 227412 KB Output is correct
26 Correct 129 ms 227408 KB Output is correct
27 Correct 157 ms 227544 KB Output is correct
28 Correct 122 ms 227404 KB Output is correct
29 Correct 180 ms 227408 KB Output is correct
30 Correct 174 ms 227412 KB Output is correct
31 Correct 187 ms 227668 KB Output is correct
32 Correct 118 ms 227532 KB Output is correct
33 Correct 114 ms 227420 KB Output is correct
34 Correct 126 ms 227408 KB Output is correct
35 Correct 120 ms 227408 KB Output is correct
36 Correct 179 ms 227384 KB Output is correct
37 Correct 174 ms 227412 KB Output is correct
38 Correct 185 ms 227404 KB Output is correct
39 Correct 120 ms 227384 KB Output is correct
40 Correct 161 ms 227412 KB Output is correct
41 Correct 162 ms 227412 KB Output is correct
42 Correct 176 ms 227424 KB Output is correct
43 Correct 179 ms 227412 KB Output is correct
44 Correct 181 ms 227408 KB Output is correct
45 Correct 179 ms 227580 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 116 ms 227408 KB Output is correct
2 Correct 180 ms 227412 KB Output is correct
3 Correct 179 ms 227412 KB Output is correct
4 Correct 114 ms 227548 KB Output is correct
5 Correct 127 ms 227408 KB Output is correct
6 Correct 128 ms 227668 KB Output is correct
7 Correct 128 ms 227408 KB Output is correct
8 Correct 182 ms 227536 KB Output is correct
9 Correct 179 ms 227412 KB Output is correct
10 Correct 184 ms 227412 KB Output is correct
11 Correct 127 ms 227412 KB Output is correct
12 Correct 154 ms 227560 KB Output is correct
13 Correct 148 ms 227412 KB Output is correct
14 Correct 146 ms 227408 KB Output is correct
15 Correct 178 ms 227412 KB Output is correct
16 Correct 182 ms 227412 KB Output is correct
17 Correct 187 ms 227496 KB Output is correct
18 Correct 137 ms 227408 KB Output is correct
19 Correct 177 ms 227412 KB Output is correct
20 Correct 124 ms 227408 KB Output is correct
21 Correct 170 ms 227412 KB Output is correct
22 Correct 191 ms 227560 KB Output is correct
23 Correct 179 ms 227408 KB Output is correct
24 Correct 178 ms 227604 KB Output is correct
25 Correct 158 ms 227412 KB Output is correct
26 Correct 129 ms 227408 KB Output is correct
27 Correct 157 ms 227544 KB Output is correct
28 Correct 122 ms 227404 KB Output is correct
29 Correct 180 ms 227408 KB Output is correct
30 Correct 174 ms 227412 KB Output is correct
31 Correct 187 ms 227668 KB Output is correct
32 Correct 118 ms 227532 KB Output is correct
33 Correct 114 ms 227420 KB Output is correct
34 Correct 126 ms 227408 KB Output is correct
35 Correct 120 ms 227408 KB Output is correct
36 Correct 179 ms 227384 KB Output is correct
37 Correct 174 ms 227412 KB Output is correct
38 Correct 185 ms 227404 KB Output is correct
39 Correct 120 ms 227384 KB Output is correct
40 Correct 161 ms 227412 KB Output is correct
41 Correct 162 ms 227412 KB Output is correct
42 Correct 176 ms 227424 KB Output is correct
43 Correct 179 ms 227412 KB Output is correct
44 Correct 181 ms 227408 KB Output is correct
45 Correct 179 ms 227580 KB Output is correct
46 Correct 111 ms 227412 KB Output is correct
47 Correct 189 ms 227412 KB Output is correct
48 Correct 131 ms 227412 KB Output is correct
49 Correct 176 ms 227412 KB Output is correct
50 Correct 142 ms 227412 KB Output is correct
51 Correct 118 ms 227576 KB Output is correct
52 Correct 165 ms 227620 KB Output is correct
53 Correct 178 ms 227408 KB Output is correct
54 Correct 180 ms 227408 KB Output is correct
55 Correct 187 ms 227412 KB Output is correct
56 Correct 136 ms 227408 KB Output is correct
57 Correct 126 ms 227408 KB Output is correct
58 Correct 123 ms 227388 KB Output is correct
59 Correct 152 ms 227412 KB Output is correct
60 Correct 181 ms 227408 KB Output is correct
61 Correct 178 ms 227408 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 116 ms 227408 KB Output is correct
2 Correct 180 ms 227412 KB Output is correct
3 Correct 179 ms 227412 KB Output is correct
4 Correct 114 ms 227548 KB Output is correct
5 Correct 127 ms 227408 KB Output is correct
6 Correct 128 ms 227668 KB Output is correct
7 Correct 128 ms 227408 KB Output is correct
8 Correct 182 ms 227536 KB Output is correct
9 Correct 179 ms 227412 KB Output is correct
10 Correct 184 ms 227412 KB Output is correct
11 Correct 127 ms 227412 KB Output is correct
12 Correct 154 ms 227560 KB Output is correct
13 Correct 148 ms 227412 KB Output is correct
14 Correct 146 ms 227408 KB Output is correct
15 Correct 178 ms 227412 KB Output is correct
16 Correct 182 ms 227412 KB Output is correct
17 Correct 187 ms 227496 KB Output is correct
18 Correct 137 ms 227408 KB Output is correct
19 Correct 177 ms 227412 KB Output is correct
20 Correct 124 ms 227408 KB Output is correct
21 Correct 170 ms 227412 KB Output is correct
22 Correct 191 ms 227560 KB Output is correct
23 Correct 179 ms 227408 KB Output is correct
24 Correct 178 ms 227604 KB Output is correct
25 Correct 158 ms 227412 KB Output is correct
26 Correct 129 ms 227408 KB Output is correct
27 Correct 157 ms 227544 KB Output is correct
28 Correct 122 ms 227404 KB Output is correct
29 Correct 180 ms 227408 KB Output is correct
30 Correct 174 ms 227412 KB Output is correct
31 Correct 187 ms 227668 KB Output is correct
32 Correct 118 ms 227532 KB Output is correct
33 Correct 114 ms 227420 KB Output is correct
34 Correct 126 ms 227408 KB Output is correct
35 Correct 120 ms 227408 KB Output is correct
36 Correct 179 ms 227384 KB Output is correct
37 Correct 174 ms 227412 KB Output is correct
38 Correct 185 ms 227404 KB Output is correct
39 Correct 120 ms 227384 KB Output is correct
40 Correct 161 ms 227412 KB Output is correct
41 Correct 162 ms 227412 KB Output is correct
42 Correct 176 ms 227424 KB Output is correct
43 Correct 179 ms 227412 KB Output is correct
44 Correct 181 ms 227408 KB Output is correct
45 Correct 179 ms 227580 KB Output is correct
46 Correct 111 ms 227412 KB Output is correct
47 Correct 189 ms 227412 KB Output is correct
48 Correct 131 ms 227412 KB Output is correct
49 Correct 176 ms 227412 KB Output is correct
50 Correct 142 ms 227412 KB Output is correct
51 Correct 118 ms 227576 KB Output is correct
52 Correct 165 ms 227620 KB Output is correct
53 Correct 178 ms 227408 KB Output is correct
54 Correct 180 ms 227408 KB Output is correct
55 Correct 187 ms 227412 KB Output is correct
56 Correct 136 ms 227408 KB Output is correct
57 Correct 126 ms 227408 KB Output is correct
58 Correct 123 ms 227388 KB Output is correct
59 Correct 152 ms 227412 KB Output is correct
60 Correct 181 ms 227408 KB Output is correct
61 Correct 178 ms 227408 KB Output is correct
62 Correct 632 ms 227628 KB Output is correct
63 Correct 568 ms 227412 KB Output is correct
64 Correct 323 ms 227408 KB Output is correct
65 Correct 256 ms 227412 KB Output is correct
66 Correct 1184 ms 227408 KB Output is correct
67 Correct 1083 ms 227620 KB Output is correct
68 Correct 1180 ms 227620 KB Output is correct
69 Correct 314 ms 227412 KB Output is correct
70 Correct 367 ms 227408 KB Output is correct
71 Correct 212 ms 227408 KB Output is correct
72 Correct 261 ms 227408 KB Output is correct
73 Correct 1144 ms 227412 KB Output is correct
74 Correct 968 ms 227412 KB Output is correct