#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL MOD = 1000000007;
const LL OFF = 101;
const LL MAXS = 204;
LL check2(string s){
vector<int> psums(1,0);
for(int j = 0; j < s.size(); j++){
if(s[j] == '('){
psums.push_back(psums[psums.size() - 1] + 1);
} else {
psums.push_back(psums[psums.size() - 1] - 1);
}
}
int laste = psums.size() - 1;
while(laste > 0 && psums[laste - 1] >= psums[psums.size() - 1]){
laste--;
}
int lasts = 0;
while(lasts + 1 < psums.size() && psums[lasts + 1] >= psums[0]){
lasts++;
}
int a = psums[psums.size() - 1] / 2;
if(lasts == psums.size() - 1 && laste == 0){
return 1;
} else if(lasts == psums.size() - 1){
int maxv = 0;
for(int i = laste; i < psums.size(); i++){
maxv = max(maxv, psums[i] - psums[psums.size() - 1]);
}
for(int i = laste; i >= 0; i--){
if(psums[i] - psums[psums.size() - 1] <= maxv - a) return 1;
if(psums[i] - psums[psums.size() - 1] > 2 * maxv) return 0;
}
} else if(laste == 0){
int maxv = 0;
for(int i = 0; i <= lasts; i++){
maxv = max(maxv, psums[i]);
}
for(int i = lasts; i < psums.size(); i++){
if(psums[i] <= maxv + a) return 1;
if(psums[i] > 2 * maxv) return 0;
}
} else {
int maxl = psums[0];
int maxm = psums[lasts];
int maxr = psums[psums.size() - 1];
for(int i = 0; i <= lasts; i++){
maxl = max(maxl, psums[i]);
}
for(int i = lasts; i <= laste; i++){
maxm = max(maxm, psums[i]);
}
for(int i = laste; i < psums.size(); i++){
maxr = max(maxr, psums[i]);
}
return (maxm <= 2 * maxl && maxm <= 2 * (maxr - a));
}
return 0;
}
LL case1(string s){
// how many correct bracket sequences are there
LL dp[s.size() + 1][MAXS];
for(int i = 0; i <= s.size(); i++){
for(int j = 0; j < MAXS; j++){
dp[i][j] = 0;
}
}
dp[0][OFF] = 1;
for(int i = 0; i < s.size(); i++){
for(int j = 0; j <= i; j++){
if(s[i] != ')'){
dp[i + 1][OFF + j + 1] += dp[i][OFF + j];
}
if(s[i] != '('){
dp[i + 1][OFF + j - 1] += dp[i][OFF + j];
}
}
for(int j = 0; j < MAXS; j++){
dp[i + 1][j] %= MOD;
}
}
return dp[s.size()][OFF];
}
LL case2(string s){
// phases:
// not found max, found max, gone negative, found a + b/2
LL ans = 0;
int z = s.size();
for(int b = -z; b < 0; b += 2){
for(int m = 0; m <= s.size(); m++){
LL dp[s.size() + 1][MAXS][4]; // i, pos,
for(int i = 0; i <= s.size(); i++){
for(int r = 0; r < MAXS; r++){
for(int c = 0; c < 4; c++){
dp[i][r][c] = 0;
}
}
}
dp[0][OFF][(m == 0)] = 1;
for(int i = 0; i < s.size(); i++){
for(int j = -z; j <= z; j++){
if(s[i] != ')'){
if(j + 1 <= m){
dp[i + 1][OFF + j + 1][(j + 1 == m)] += dp[i][OFF + j][0];
dp[i + 1][OFF + j + 1][1] += dp[i][OFF + j][1];
}
if(j + 1 <= 2 * m){
dp[i + 1][OFF + j + 1][2] += dp[i][OFF + j][2];
}
dp[i + 1][OFF + j + 1][3] += dp[i][OFF + j][3];
}
if(s[i] != '('){
if(j - 1 >= b){
if(j - 1 >= 0){
dp[i + 1][OFF + j - 1][0] += dp[i][OFF + j][0];
dp[i + 1][OFF + j - 1][1] += dp[i][OFF + j][1];
} else {
dp[i + 1][OFF + j - 1][2 + (j - 1 <= m + b/2)] += dp[i][OFF + j][1];
}
dp[i + 1][OFF + j - 1][2 + (j - 1 <= m + b/2)] += dp[i][OFF + j][2];
dp[i + 1][OFF + j - 1][3] += dp[i][OFF + j][3];
}
}
}
for(int j = 0; j < MAXS; j++){
for(int c = 0; c < 4; c++){
dp[i + 1][j][c] %= MOD;
}
}
}
ans += dp[s.size()][OFF + b][3];
ans %= MOD;
if(dp[s.size()][OFF + b][3] > 0){
//cout << m << " " << b << " " << dp[s.size()][OFF + b][3] << endl;
}
}
}
return ans;
}
LL case3(string s){
// do shitty n^4 dp here
LL ans = 0;
int z = s.size();
for(int b = -z; b <= z; b += 2){
for(int m = 0; m <= s.size(); m++){
if(m < b) continue;
// phases:
// not hit needed, hit needed, went negative, hit max, went positive, hit needed
LL dp[s.size() + 1][MAXS][6];
for(int i = 0; i <= s.size(); i++){
for(int r = 0; r < MAXS; r++){
for(int c = 0; c < 6; c++){
dp[i][r][c] = 0;
}
}
}
int lneeded = (m + 1) / 2;
int rneeded = lneeded + b/2;
dp[0][OFF][(0 >= lneeded)] = 1;
for(int i = 0; i < s.size(); i++){
for(int j = -z; j <= z; j++){
if(s[i] != ')'){
dp[i + 1][OFF + j + 1][(j + 1 >= lneeded)] += dp[i][OFF + j][0];
dp[i + 1][OFF + j + 1][1] += dp[i][OFF + j][1];
if(j + 1 <= m){
dp[i + 1][OFF + j + 1][2 + (j + 1 == m)] += dp[i][OFF + j][2];
dp[i + 1][OFF + j + 1][3] += dp[i][OFF + j][3];
if(j + 1 == b){
if(j + 1 == m){
dp[i + 1][OFF + j + 1][4 + (j + 1 >= rneeded)] += dp[i][OFF + j][2];
}
dp[i + 1][OFF + j + 1][4 + (j + 1 >= rneeded)] += dp[i][OFF + j][3];
}
}
dp[i + 1][OFF + j + 1][4 + (j + 1 >= rneeded)] += dp[i][OFF + j][4];
dp[i + 1][OFF + j + 1][5] += dp[i][OFF + j][5];
}
if(s[i] != '('){
if(j - 1 >= 0){
dp[i + 1][OFF + j - 1][0] += dp[i][OFF + j][0];
dp[i + 1][OFF + j - 1][1] += dp[i][OFF + j][1];
} else {
dp[i + 1][OFF + j - 1][2 + (m == 0)] += dp[i][OFF + j][1];
}
dp[i + 1][OFF + j - 1][2] += dp[i][OFF + j][2];
dp[i + 1][OFF + j - 1][3] += dp[i][OFF + j][3];
if(j - 1 >= b){
dp[i + 1][OFF + j - 1][4] += dp[i][OFF + j][4];
dp[i + 1][OFF + j - 1][5] += dp[i][OFF + j][5];
}
}
}
for(int j = 0; j < MAXS; j++){
for(int c = 0; c < 6; c++){
dp[i + 1][j][c] %= MOD;
}
}
}
ans += dp[s.size()][OFF + b][5];
ans %= MOD;
}
}
return ans;
}
LL solve(string s){
string t = s;
reverse(t.begin(), t.end());
return case1(s) + case2(s) + case2(t) + case3(s);
}
LL cnt(string s, int idx){
if(idx == s.size()){
return check2(s);
}
if(s[idx] != 'x'){
return cnt(s, idx + 1);
}
string s1 = s;
string s2 = s;
s1[idx] = '(';
s2[idx] = ')';
for(int z = idx + 1; z <= s.size(); z++){
if(z == s.size() || s[z] == 'x'){
return (cnt(s1, z) + cnt(s2, z)) % MOD;
}
}
}
int main(){
int n;
string s;
cin >> n >> s;
if(n % 2 == 1){
cout << 0 << '\n';
return 0;
}
//cout << cnt(s, 0) << '\n';
cout << solve(s) << '\n';
}
Compilation message
securitygate.cpp: In function 'LL check2(std::__cxx11::string)':
securitygate.cpp:10:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j = 0; j < s.size(); j++){
~~^~~~~~~~~~
securitygate.cpp:22:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(lasts + 1 < psums.size() && psums[lasts + 1] >= psums[0]){
~~~~~~~~~~^~~~~~~~~~~~~~
securitygate.cpp:26:11: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(lasts == psums.size() - 1 && laste == 0){
~~~~~~^~~~~~~~~~~~~~~~~~~
securitygate.cpp:28:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
} else if(lasts == psums.size() - 1){
~~~~~~^~~~~~~~~~~~~~~~~~~
securitygate.cpp:30:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = laste; i < psums.size(); i++){
~~^~~~~~~~~~~~~~
securitygate.cpp:42:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = lasts; i < psums.size(); i++){
~~^~~~~~~~~~~~~~
securitygate.cpp:56:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = laste; i < psums.size(); i++){
~~^~~~~~~~~~~~~~
securitygate.cpp: In function 'LL case1(std::__cxx11::string)':
securitygate.cpp:67:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i <= s.size(); i++){
~~^~~~~~~~~~~
securitygate.cpp:73:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < s.size(); i++){
~~^~~~~~~~~~
securitygate.cpp: In function 'LL case2(std::__cxx11::string)':
securitygate.cpp:94:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int m = 0; m <= s.size(); m++){
~~^~~~~~~~~~~
securitygate.cpp:96:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i <= s.size(); i++){
~~^~~~~~~~~~~
securitygate.cpp:104:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < s.size(); i++){
~~^~~~~~~~~~
securitygate.cpp: In function 'LL case3(std::__cxx11::string)':
securitygate.cpp:149:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int m = 0; m <= s.size(); m++){
~~^~~~~~~~~~~
securitygate.cpp:154:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i <= s.size(); i++){
~~^~~~~~~~~~~
securitygate.cpp:164:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < s.size(); i++){
~~^~~~~~~~~~
securitygate.cpp: In function 'LL cnt(std::__cxx11::string, int)':
securitygate.cpp:216:9: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(idx == s.size()){
~~~~^~~~~~~~~~~
securitygate.cpp:226:25: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int z = idx + 1; z <= s.size(); z++){
~~^~~~~~~~~~~
securitygate.cpp:227:8: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(z == s.size() || s[z] == 'x'){
~~^~~~~~~~~~~
securitygate.cpp:231:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Execution timed out |
5092 ms |
1388 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Execution timed out |
5092 ms |
1388 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Execution timed out |
5092 ms |
1388 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Execution timed out |
5092 ms |
1388 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Execution timed out |
5092 ms |
1388 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |