尽可能凑2^x-1
#include <cstdio>
#include <cstring>
const int N = 100005;
int a[N], p[N];
int init(int x) {
int cnt = 0;
while(x > 1) {
x /= 2;
cnt ++;
}
return cnt + 1;
}
int main() {
int n;
while(~scanf("%d", &n)){
for(int i = 0; i <= n; i ++) {
scanf("%d", &a[i]);
}
memset(p, 0, sizeof p);
long long ans = 0;
for(int i = n; i >= 0; i --) {
if(p[i] != 0) continue;
if(i == 0) continue;
int t = 1<<(init(i));
if(t == 2 * i) {
p[i] = i-1;
p[i-1] = i;
ans += 2 * (i^(i-1));
} else if(t == i + 1) {
p[i] = 0;
p[0] = i;
ans += 2*i;
} else {
if(p[t-i] == 0) {
p[i] = t - i - 1;
p[t-i-1] = i;
ans += 2 * (t-1);
}
}
}
printf("%I64d\n", ans);
for(int i = 0; i <= n; i ++) {
if(i != 0) printf(" ");
printf("%d", p[a[i]]);
}puts("");
}
return 0;
}