题意:给你n+1个数(0->n),让你为这n+1个数在0->n中分别找一个数与其异或,求最后的最大值
思路:假设一个数5 (二进制1 0 1),则找的另一个数在5的0位上最好是1 , 1位上最好为0,使其异或后为1.
#include <cstdio>
#include <cstring>
#define mod 10000007
typedef long long ll;
int n;
int a[100005],num[100005];
int main()
{
while(scanf("%d",&n) != EOF)
{
for(int i = 0; i <= n; i++)
scanf("%d",&a[i]);
memset(num,-1,sizeof(num));
ll sum = 0;
for(int i = n; i >= 0; i--)
{
if(num[i] != -1)
continue;
int an=0,tmp = 1,ant=1;
while(tmp < i)
{
if( (tmp & i) == 0)
{
an+=ant;
}
tmp<<=1;
ant*= 2;
}
num[i] = an;
num[an] = i;
sum+=(i^an)*2;
}
printf("%I64d\n",sum);
printf("%d",num[a[0]]);
for(int i = 1; i <= n; i++)
printf(" %d",num[a[i]]);
printf("\n");
}
return 0;
}