QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#150744 | #5434. Binary Substrings | TadijaSebez | RE | 4ms | 14196kb | C++14 | 1.4kb | 2023-08-26 07:24:57 | 2023-08-26 07:25:00 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
const int N=400050;
vector<int> E[N];
int ptr[N];
vector<int> cyc;
void DFS(int u){
while(true){
if(ptr[u]==E[u].size()){
cyc.pb(u);
return;
}
DFS(E[u][ptr[u]++]);
}
}
void Euler(int sz){
for(int mask=0;mask<(1<<sz);mask++){
E[mask].pb(mask>>1);
E[mask].pb((mask>>1)|(1<<(sz-1)));
}
DFS(0);
}
string Solve(int sz,int n){
vector<int> ord;
vector<int> next(1<<sz,0);
for(int i=0;i+1<cyc.size();i++){
ord.pb((cyc[i]<<1)|(cyc[i+1]&1));
}
for(int i=0;i<ord.size();i++){
next[ord[i]]=ord[(i+1)%ord.size()];
}
vector<int> p(1<<sz,0);
for(int mask=0;mask<(1<<sz);mask++){
p[mask]=next[mask]^1;
}
vector<bool> was(1<<sz,false);
vector<int> sol;
n-=1<<sz;
n-=sz-1;
for(int i=0;i<(1<<sz);i++){
int mask=ord[i];
sol.pb(mask);
if(!was[mask] && n>0){
while(!was[mask]){
was[mask]=true;
sol.pb(p[mask]);
mask=p[mask];
n--;
if(n==0)break;
}
}
}
string ans="";
for(int i=0;i<sz-1;i++){
ans+='0'+(sol[0]>>(sz-1-i)&1);
}
for(int mask:sol){
ans+='0'+(mask&1);
}
return ans;
}
int main(){
int n;
scanf("%i",&n);
if(n<=2){
if(n==1)printf("0\n");
else printf("01\n");
return 0;
}
int sz=1;
while((1<<(sz+1))+sz<=n)sz++;
Euler(sz-1);
string ans=Solve(sz,n);
printf("%s\n",ans.c_str());
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 4ms
memory: 13232kb
input:
2
output:
01
result:
ok meet maximum 3
Test #2:
score: 0
Accepted
time: 1ms
memory: 13660kb
input:
5
output:
01100
result:
ok meet maximum 12
Test #3:
score: 0
Accepted
time: 1ms
memory: 14196kb
input:
1
output:
0
result:
ok meet maximum 1
Test #4:
score: -100
Runtime Error
input:
3