QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#301929 | #4824. Bracket-and-bar Sequences | isaunoya | 0 | 1ms | 3928kb | C++14 | 3.6kb | 2024-01-10 14:31:45 | 2024-01-10 14:31:45 |
answer
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N=25+7; int n;
bitset<80>f1[N*3],f2[N*3];
long long f[N]; char s[N],t[N]; int g[N*3][N*3];
long long dfs(int l,int r){
if(l>r) return 1;
// cout<<l<<","<<r<<","<<g[l][r]<<endl;
long long A=0;
if(g[l][r]){
if(r-l+1==3) return 1;
int L=(r-l+1)/3;
for(int i=1;i<L;i++)
A+=f[i]*f[L-i];
int s=(g[l][r]-l)/3;
// cout<<A<<","<<s<<","<<L<<endl;
for(int i=0;i<s;i++){
A+=f[i]*f[L-1-i];
// cout<<f[i]*f[L-i-1]<<"asjdkasl"<<i<<endl;
}
// cout<<f[s]*f[L-s-1]<<"asdadsa"<<endl;
// if(l==1&&r==6){
// cout<<dfs(l+1,g[l][r]-1)<<","<<dfs(g[l][r]+1,r-1)<<","<<f[2]<<","<<A<<endl;
// }
// cout<<(dfs(l+1,g[l][r]-1)-1)<<","<<dfs(g[l][r]+1,r-1)<<","<<L<<endl;
return A+(dfs(l+1,g[l][r]-1)-1)*f[L-1-s]+dfs(g[l][r]+1,r-1);
}
else{
int s=0;
int L=(r-l+1)/3;
for(int i=1;i<L;i++){
if(g[l][l+i*3-1]){
s=i; break;
}
A+=f[i]*f[L-i];
}
// cout<<A<<"www"<<g[1][3]<<g[4][9]<<endl;
return A+(dfs(l,l+s*3-1)-1)*f[L-s]+dfs(l+s*3,r);
}
}
void solve1(){
int T;
cin>>T;
while(T--){
memset(f,0,sizeof(f));
f[0]=1,f[1]=1;
memset(g,0,sizeof(g));
for(int i=2;i<=25;i++){
for(int j=1;j<i;j++) f[i]+=f[j]*f[i-j];
for(int j=0;j<=i-1;j++) f[i]+=f[j]*f[i-j-1];
}
cin>>n;
scanf("%s",t+1);
for(int i=1;i<=3*n-2;i++)
if(t[i]=='('&&t[i+1]=='|'&&t[i+2]==')') g[i][i+2]=i+1;
for(int i=1;i<=3*n;i++) g[i+1][i]=1,f1[i].reset(),f2[i].reset();
for(int i=1;i<=3*n;i++){
if(t[i]=='('&&t[i+1]=='|') f1[i][i+1]=1;
if(t[i]=='|'&&t[i+1]==')') f2[i+1][i]=1;
}
for(int z=1;z<=n;z++)
for(int l=1;l<=3*n;l++){
int r=z*3-1+l;
if(r>3*n) continue;
if((f1[l]&f2[r]).count()) g[l][r]=(f1[l]&f2[r])._Find_first();
if(g[l][r]){
if(t[l-1]=='('&&t[r+1]=='|') f1[l-1][r+1]=1;
if(t[l-1]=='|'&&t[r+1]==')') f2[r+1][l-1]=1;
}
// cout<<l<<","<<r<<"www"<<endl;
// if(g[l][r]) cout<<l<<","<<r<<","<<g[l][r]<<endl;
}
// cout<<g[1][12]<<"www"<<endl;
cout<<dfs(1,3*n)<<endl;
}
}
void dfs2(int L,long long c){
if(L==0) return;
if(L==1){
printf("(|)"); return;
}
for(int i=1;i<L;i++){
if(c>f[i]*f[L-i]){
c-=f[i]*f[L-i];
}
else{
long long D=c/f[L-i]+1;
if(c%f[L-i]==0) D--;
dfs2(i,D);
D=c%f[L-i];
if(D==0) D=f[L-i];
dfs2(L-i,D);
return;
}
}
// if(L==4) cout<<c<<"ssss"<<endl;
for(int i=0;i<=L-1;i++){
if(c>f[i]*f[L-i-1]){
c-=f[i]*f[L-i-1];
// cout<<f[i]*f[L-i-1]<<"wwww"<<endl;
}
else{
// cout<<i<<":"<<c<<endl;
printf("(");
long long D=c/f[L-i-1]+1;
if(c%f[L-i-1]==0) D--;
// cout<<D<<"www"<<endl;
dfs2(i,D);
printf("|");
D=c%f[L-i-1];
if(c%f[L-i-1]==0) D=f[L-i-1];
// cout<<D<<"eee"<<endl;
dfs2(L-i-1,D);
printf(")");
return;
}
}
}
void solve2(){
int T; cin>>T;
memset(f,0,sizeof(f));
f[0]=1,f[1]=1;
memset(g,0,sizeof(g));
for(int i=1;i<=75;i++)
for(int j=1;j<i;j++) g[i][j]=1;
for(int i=2;i<=25;i++){
for(int j=1;j<i;j++) f[i]+=f[j]*f[i-j];
for(int j=0;j<=i-1;j++) f[i]+=f[j]*f[i-j-1];
}
while(T--){
int n; long long x;
cin>>n>>x;
dfs2(n,x);
cout<<endl;
}
}
signed main(){
scanf("%s",s+1);
if(s[1]=='e') solve1();
else solve2();
}
// en
// 1
// 2
// ((|)|)
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3808kb
input:
encode 3 1 (|) 4 ((((|)|)|)|) 5 (|(|))((|(|))|)
output:
1 67 92
input:
decode 3 1 1 4 67 5 92
output:
(|) ((((|)|)|)|) (|(|))((|(|))|)
result:
ok 3 lines
Test #2:
score: 100
Accepted
time: 1ms
memory: 3876kb
input:
encode 1 1 (|)
output:
1
input:
decode 1 1 1
output:
(|)
result:
ok single line: '(|)'
Test #3:
score: 100
Accepted
time: 0ms
memory: 3928kb
input:
encode 3 2 ((|)|) 1 (|) 2 (|(|))
output:
3 1 2
input:
decode 3 2 3 1 1 2 2
output:
((|)|) (|) (|(|))
result:
ok 3 lines
Test #4:
score: 0
Stage 1: Program answer Time Limit Exceeded
input:
encode 1000 3 (|)(|)(|) 3 (|)(|(|)) 3 (|)((|)|) 3 (|(|))(|) 3 (|(|)(|)) 3 (|(|(|))) 3 (|((|)|)) 3 ((|)|)(|) 3 ((|)|(|)) 3 ((|)(|)|) 3 ((|(|))|) 3 (((|)|)|) 4 (|)(|)(|)(|) 4 (|)(|)(|(|)) 4 (|)(|)((|)|) 4 (|)(|(|))(|) 4 (|)(|(|)(|)) 4 (|)(|(|(|))) 4 (|)(|((|)|)) 4 (|)((|)|)(|) 4 (|)((|)|(|)) 4 (|)((|)...
output:
1 2 3 5