题解:
一开始想到比较简单的
O(log2n)
O
(
log
2
n
)
的方法以为能过,看来我还是太Naive了。
logn
log
n
的方法类似快速幂。 相当于求包含
n+1
n
+
1
个含空子串的对称串的串。
发现
aXaY
a
X
a
Y
比
XY
X
Y
多一倍,
aXYa
a
X
Y
a
多一个,(
a
a
在中未出现)那么直接倍增即可。
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int RLEN=1<<18|1;
inline char nc() {
static char ibuf[RLEN],*ib,*ob;
(ib==ob) && (ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
return (ib==ob) ? -1 : *ib++;
}
inline int rd() {
char ch=nc(); int i=0,f=1;
while(!isdigit(ch)) {if(ch=='-')f=-1; ch=nc();}
while(isdigit(ch)) {i=(i<<1)+(i<<3)+ch-'0'; ch=nc();}
return i*f;
}
LL n; int tot=0;
deque <int> le,re;
inline void solve(LL n) {
if(n==1) return;
solve(n>>1);
le.push_front(++tot);
re.push_front(tot);
if(n&1) {
le.push_front(++tot);
re.push_back(tot);
}
}
int main() {
cin>>n; solve(n+1);
cout<<le.size()+re.size()<<endl;
for(int i=0;i<le.size();++i) cout<<le[i]<<" ";
for(int i=0;i<re.size();++i) cout<<re[i]<<" ";
}