本质思路很简单,对一个数i
找一个数Rev(i)使得i^Rev(i)的二进制上每一位都是1
一个Rev(i)对应的i不唯一,有Rev(1100) = 11,Rev(100) = 11 //二进制下显然会选择Rev(11) = 1100
所以注意要记录我们已经求出了Rev(11)从贪心的思路想,肯定是先让最大的找到自己的Rev
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<vector>
#include<set>
#include<queue>
#include<map>
using namespace std;
#define INF 1000000000
#define maxn 100010
#define rep(i,x,y) for(int i=x;i<=y;i++)
#define mset(x) memset(x,0,sizeof(x))
typedef __int64 ll;
int n, a[maxn], b[maxn];
int main(){
// freopen("a.txt","r",stdin);
// freopen(".out","w",stdout);
while(cin>>n){
rep(i,0,n) scanf("%d", &a[i]);
memset(b, -1, sizeof(b));
for(int i=n; i>=0; i--){
if(b[i]!=-1) continue;
int x = log2(i)+1;
int Rev = (((1<<x)-1) ^ i);
b[i] = Rev;
b[Rev] = i;
}
ll ans=0;
rep(i, 0, n) ans += (ll)(a[i] ^ b[a[i]]);
cout<<ans<<endl;
rep(i, 0, n-1)
printf("%d ", b[a[i]]);
printf("%d\n",b[a[n]]);
}
return 0;
}
/*
DESCRIPTION:
*/