QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#323365 | #4824. Bracket-and-bar Sequences | hotboy2703 | 0 | 0ms | 3888kb | C++14 | 3.6kb | 2024-02-09 12:00:16 | 2024-02-09 12:00:16 |
answer
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
#define pll pair <ll,ll>
#define fi first
#define se second
#define sz(a) (ll((a).size()))
#define BIT(mask,i) (((mask) >> (i))&1LL)
#define MASK(i) (1LL << (i))
ull dp[60];
ll f(ll x,ll y){
return ll(dp[x] * dp[y]);
}
ll pr_to_int(string s){
// cout<<s<<endl;
ll n = sz(s)/3;
if (n < 2)return 0;
ll sum = 0;
ll l = -1;
for (ll i = sz(s) - 1;i >= 0;i --){
sum += (s[i] == '|' ? 0 : (s[i] == '(' ? 1 : -1));
if (sum == 0){l = i;break;}
}
ll right_sz = (sz(s)-l)/3;
// cout<<"XXX "<<l<<endl;
if (right_sz==n){
ll res = 0;
for (ll i = 1;i < n;i ++){
res += f(n - i,i);
}
ll sum = 0;
ll l = -1;
for (ll i = sz(s) - 2;i >= 0;i --){
sum += (s[i] == '|' ? 0 : (s[i] == '(' ? 1 : -1));
if (sum == 0){l = i;break;}
}
if (l == sz(s)-2)l++;
ll nw_sz = (sz(s) - 1 - l) / 3;
for (ll i = 0;i < nw_sz;i++){
res+=f(n-1-i,i);
}
string left_part(s,1,l-2),right_part(s,l,sz(s)-1-l);
// cout<<"??? "<<left_part<<' '<<right_part<<endl;
return res + pr_to_int(left_part) * dp[sz(right_part)/3] + pr_to_int(right_part);
}
else{
ll res = 0;
for (ll i = 1;i < right_sz;i ++){
res += f(n - i,i);
}
// cout<<s<<"SUS"<<l<<' '<<res<<endl;
string left_part(s,0,sz(s)-right_sz*3),right_part(s,sz(s)-right_sz*3,right_sz*3);
// cout<<"??? "<<left_part<<' '<<right_part<<endl;
return res + pr_to_int(left_part) * dp[sz(right_part)/3] + pr_to_int(right_part);
}
}
ll to_int(string s){
ll res = 0;
ll n=sz(s)/3;
for (ll i = 0;i < n;i++)res+=dp[i];
res += pr_to_int(s);
return res;
}
string pr_to_string(ll n,ll k){
if (n == 1)return "(|)";
if (n==0)return "";
for (ll j = 1;j < n;j ++){
if (f(n-j,j)<=k)k-=f(n-j,j);
else{
ll k1,k2;
k1 = k / dp[j];
k2 = k % dp[j];
return pr_to_string(n-j,k1) + pr_to_string(j,k2);
}
}
for (ll i = 0;i < n;i ++){
if (f(n-1-i,i)<=k)k-=f(n-1-i,i);
else{
ll k1,k2;
k1 = k / dp[i];
k2 = k % dp[i];
return "(" + pr_to_string(n-1-i,k1) + "|" + pr_to_string(i,k2) + ")";
}
}
}
string too_string(ll x){
for (ll i = 0;i <= 25;i ++){
if (x >= dp[i])x -= dp[i];
else {return pr_to_string(i,x);}
}
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(nullptr);cout.tie(nullptr);
dp[0] = 1;
dp[1] = 1;
for (ll i = 1;i <= 25;i ++){
for (ll j = 0;j <= i;j ++){
if (j == 0)dp[i+1] += 2 * dp[i];
else{
if (j != i)dp[i+j] += dp[i] * dp[j] * 2;
else dp[i+j] += dp[i] * dp[i];
if (j != i)dp[i+j+1] += dp[i] * dp[j] * 2;
else dp[i+j+1] += dp[i] * dp[i];
}
}
// cout<<i<<' '<<dp[i]<<'\n';
}
string task;
cin>>task;
ll t;
cin>>t;
while (t--){
ll n;
cin>>n;
if (task=="encode"){
string s;
cin>>s;
cout<<to_int(s)<<'\n';
// cout<<pr_to_int(s)<<'\n';
}
else{
ll x;
cin>>x;
cout<<too_string(x)<<'\n';
}
}
}
/*
decode
3
1
1
4
60
5
225
1274049
1273964
*/
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 3648kb
input:
encode 3 1 (|) 4 ((((|)|)|)|) 5 (|(|))((|(|))|)
output:
1 60 225
input:
decode 3 1 1 4 60 5 225
output:
(|) ((((|)|)|)|) (|(|))((|(|))|)
result:
ok 3 lines
Test #2:
score: 100
Accepted
time: 0ms
memory: 3888kb
input:
encode 1 1 (|)
output:
1
input:
decode 1 1 1
output:
(|)
result:
ok single line: '(|)'
Test #3:
score: 100
Accepted
time: 0ms
memory: 3680kb
input:
encode 3 2 ((|)|) 1 (|) 2 (|(|))
output:
3 1 4
input:
decode 3 2 3 1 1 2 4
output:
((|)|) (|) (|(|))
result:
ok 3 lines
Test #4:
score: 0
Stage 1: Program answer Memory 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 (|)((|)...