#include "bits/stdc++.h"
using namespace std;
// vector <char> v[1000010];
string solve(string s) {
int n = s.size();
stack <int> p;
stack <int> q;
string ans;
for(int i = 0; i < n; i++) ans += '#';
for(int i = 0; i < n; i++) {
if(s[i] == ')') {
if(p.empty()) {
if(q.empty()) return "impossible";
else {
ans[q.top()] = 'G';
ans[i] = 'B';
q.pop();
}
} else {
ans[p.top()] = 'R';
ans[i] = 'R';
p.pop();
}
} else {
p.push(i);
q.push(i);
}
}
stack <int> thic;
for(int i = 0; i < n; i++) {
if(ans[i] == '#') {
thic.push(i);
} else if (s[i] == ')' && !thic.empty()) {
ans[thic.top()] = 'B';
ans[i] = 'G';
thic.pop();
}
}
if(thic.empty()) return ans;
else return "impossible";
}
const int mod = 1000000000 + 7;
int mem[31][31][31];
int fem[31][31][31];
int dp(int cur, int p, int q) {
if(cur == 0) {
return p == 0;
}
if(mem[cur][p][q] != -1) return mem[cur][p][q];
int ans = 0;
ans += dp(cur - 1, p + 1, q + 1);
if(p > 0) ans += dp(cur - 1, p - 1, q);
else if(q > 0) ans += dp(cur - 1, p, q - 1);
ans %= mod;
return mem[cur][p][q] = ans;
}
int fn(int cur, int p, int q) {
if(cur == 0) {
return q == 0;
}
if(fem[cur][p][q] != -1) return fem[cur][p][q];
int ans = 0;
ans += fn(cur - 1, p + 1, q + 1);
if(p > 1) {
ans += fn(cur - 1, p - 1, max(0, q - 2));
}
ans %= mod;
return fem[cur][p][q] = ans;
}
int main(int argc, char const *argv[])
{
ios_base :: sync_with_stdio (false);
cin.tie(0);
int p;
cin >> p;
int t;
cin >> t;
memset(mem, -1, sizeof mem);
memset(fem, -1, sizeof fem);
while(t--) {
if(p == 1) {
string s;
cin >> s;
cout << solve(s) << '\n';
} else {
int n;
cin >> n;
long long ans = 0;
for(int i = 0; i <= n; i++) {
ans += 1LL * dp(i, 0, 0) * fn(n-i, 0, 0);
ans %= mod;
}
cout << ans << endl;
}
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
632 KB |
Output is correct |
2 |
Correct |
3 ms |
744 KB |
Output is correct |
3 |
Correct |
3 ms |
744 KB |
Output is correct |
4 |
Correct |
3 ms |
744 KB |
Output is correct |
5 |
Correct |
3 ms |
744 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
744 KB |
Output is correct |
2 |
Correct |
3 ms |
744 KB |
Output is correct |
3 |
Correct |
3 ms |
776 KB |
Output is correct |
4 |
Correct |
3 ms |
776 KB |
Output is correct |
5 |
Correct |
5 ms |
776 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
744 KB |
Output is correct |
2 |
Correct |
3 ms |
744 KB |
Output is correct |
3 |
Correct |
3 ms |
776 KB |
Output is correct |
4 |
Correct |
3 ms |
776 KB |
Output is correct |
5 |
Correct |
5 ms |
776 KB |
Output is correct |
6 |
Correct |
3 ms |
776 KB |
Output is correct |
7 |
Correct |
2 ms |
800 KB |
Output is correct |
8 |
Correct |
4 ms |
812 KB |
Output is correct |
9 |
Correct |
3 ms |
812 KB |
Output is correct |
10 |
Correct |
4 ms |
812 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
744 KB |
Output is correct |
2 |
Correct |
3 ms |
744 KB |
Output is correct |
3 |
Correct |
3 ms |
776 KB |
Output is correct |
4 |
Correct |
3 ms |
776 KB |
Output is correct |
5 |
Correct |
5 ms |
776 KB |
Output is correct |
6 |
Correct |
3 ms |
776 KB |
Output is correct |
7 |
Correct |
2 ms |
800 KB |
Output is correct |
8 |
Correct |
4 ms |
812 KB |
Output is correct |
9 |
Correct |
3 ms |
812 KB |
Output is correct |
10 |
Correct |
4 ms |
812 KB |
Output is correct |
11 |
Correct |
4 ms |
848 KB |
Output is correct |
12 |
Correct |
3 ms |
852 KB |
Output is correct |
13 |
Correct |
3 ms |
864 KB |
Output is correct |
14 |
Correct |
3 ms |
864 KB |
Output is correct |
15 |
Correct |
2 ms |
880 KB |
Output is correct |
16 |
Correct |
12 ms |
896 KB |
Output is correct |
17 |
Correct |
7 ms |
1408 KB |
Output is correct |
18 |
Correct |
6 ms |
1408 KB |
Output is correct |
19 |
Correct |
6 ms |
1408 KB |
Output is correct |
20 |
Correct |
7 ms |
1428 KB |
Output is correct |
21 |
Correct |
59 ms |
1552 KB |
Output is correct |
22 |
Correct |
39 ms |
5796 KB |
Output is correct |
23 |
Correct |
24 ms |
5796 KB |
Output is correct |
24 |
Correct |
33 ms |
5796 KB |
Output is correct |
25 |
Correct |
38 ms |
5824 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5824 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5824 KB |
Output is correct |
2 |
Correct |
4 ms |
5824 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5824 KB |
Output is correct |
2 |
Correct |
4 ms |
5824 KB |
Output is correct |
3 |
Runtime error |
5 ms |
5824 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |