This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define N 305
using namespace std;
const int mod = 1e9 + 7;
int val[N];
int val2[N];
int ans[N];
void solve1(){
string s;
cin >> s;
int n = s.size();
vector<int> r,rr,points;
string ans = "";
int a = 0,b = 0;
for(int i = 0;i<n;i++){
ans += ".";
if(s[i] == '(')a++;
else b++;
if(2*a < b){
cout << "impossible" << endl;
return;
}
if(s[i] == '('){
ans[i] = 'R';
r.push_back(i);
rr.push_back(i);
}
if(s[i] == ')'){
if(rr.empty()){
ans[r.back()] = 'G';
ans[i] = 'B';
r.pop_back();
continue;
}
rr.pop_back();
points.push_back(i);
ans[i] = 'R';
}
}
if(2*b < a){
cout << "impossible" << endl;
return;
}
for(int i = 0;i<rr.size();i++){
ans[rr[i]] = 'B';
ans[points[points.size() - rr.size() + i]] = 'G';
if(rr[i] > points[points.size() - rr.size() + i]){
cout << "impossible" << endl;
return;
}
}
cout << ans << endl;
}
void solve2(){
int n;
cin >> n;
cout << (ans[n] + val[n])%mod << endl;
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
#ifdef Local
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
for(int i = 0;i*3<N;i++){
int a = i,b = 2*i;
vector<vector<int>> dp(N,vector<int>(N,0));
dp[0][0] = 1;
for(int i = 0;i<N;i++){
for(int j = 0;j<N;j++){
bool ok = 1;
if(2*(i+1) < j)
ok = 0;
if(i + 1 > a)
ok = 0;
if(ok){
dp[i+1][j] = (dp[i+1][j] + dp[i][j])%mod;
}
ok = 1;
if(2*i < j+1)
ok = 0;
if(j + 1 > b)
ok = 0;
if(ok){
dp[i][j+1] = (dp[i][j+1] + dp[i][j])%mod;
}
}
}
//cout << dp[a][b] << endl;
val[3*i] = dp[a][b];
}
//for(int a = 1;a<N;a++){
// for(int b = 1;b<2*a && b + a < N;b++){
for(int num = -(N-1);num < 2*(N-1);num++){
vector<vector<int>> dp(N,vector<int>(N,0));
dp[0][1] = 1;
for(int i = 0;i<N;i++){
for(int j = 1;j<N;j++){
bool ok = 1;
if(i == 2*j)
ok = 0;
if(num <= 2*i-j)
ok = 0;
if(i + 1 == N)
ok = 0;
if(ok){
dp[i+1][j] = (dp[i+1][j] + dp[i][j])%mod;
}
ok = 1;
if(num <= 2*i-j)
ok = 0;
if(j + 1 == N)
ok = 0;
if(ok){
dp[i][j+1] = (dp[i][j+1] + dp[i][j])%mod;
}
}
}
for(int i = 0;i<N;i++){
for(int j = 0;j<N;j++){
if(2*i - j == num && i+j < N && 2*i > j && 2*j >= i){
val2[i+j] = (val2[i+j] + dp[i][j])%mod;
}
}
}
}
for(int i = 1;i<N;i++){
for(int j = 0;j<N;j++){
ans[i+j] = (ans[i+j] + (long long)val2[i]*val[j])%mod;
}
}
int type;
cin >> type;
int t=1;
cin>>t;
while(t--){
if(type == 1)
solve1();
if(type ==2)
solve2();
}
#ifdef Local
cout<<endl<<fixed<<setprecision(2)<<1000.0 * clock() / CLOCKS_PER_SEC<< " milliseconds ";
#endif
}
Compilation message (stderr)
parentrises.cpp: In function 'void solve1()':
parentrises.cpp:44:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
44 | for(int i = 0;i<rr.size();i++){
| ~^~~~~~~~~~
parentrises.cpp: In function 'int32_t main()':
parentrises.cpp:131:32: warning: iteration 304 invokes undefined behavior [-Waggressive-loop-optimizations]
131 | ans[i+j] = (ans[i+j] + (long long)val2[i]*val[j])%mod;
| ~~~~~~~^
parentrises.cpp:130:24: note: within this loop
130 | for(int j = 0;j<N;j++){
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |