Test instructions: Allows you to construct a sequence that makes the sequence XOR and maximum, the sequence is full of n, and the sequence and calculation method is SUM = A[1] ^ 0 + a[2] ^ 1 + a[3] ^ 2 + ... a[n] ^ n
Feelings: Before did not do the problem of bit operation, to this piece is very strange, two different or later, if the binary every bit is 1, then the largest, the law found that when n is even 0, the other is in pairs appear
When n is odd, it is paired. For example, n=4, 0 1 2 3 4 corresponds to 0 2 1 4 3; n=5 0 1 2 3 4 5 respectively corresponds to 1 0 5 4 3 2 Self can be simulated on the draft paper;
AC Code:
#include <iostream>
#include <cstring>
#include <cstdio>
typedef __int64 LL;
using namespace Std;
ll result[1000010];
ll N;
int main ()
{
while (scanf ("%i64d", &n)!=eof)
{
ll K=1;
while (k<=n)
k*=2;
k--;
memset (result,-1,sizeof (result));
for (int i=n;i>=0;i--)//Counter-find not to be divided into odd and even discussions
{
if (result[i]!=-1)//has matched the continuation of the next number
Continue
Else
{
while ((k^i) >n| | RESULT[K^I]!=-1)
k/=2;
Result[k^i]=i;
result[i]=k^i;//For example in the second example above result[2]=5;result[5]=2;
}
}//note w^q^q=w; The above k is the result of the following j^result[j]
ll Sum=0;
for (int j=0;j<=n;j++)
SUM+=J^RESULT[J];
printf ("%i64d\n", sum);
for (int q=0;q<n;q++)
printf ("%i64d", Result[q]);
printf ("%i64d\n", Result[n]);
}
return 0;
}
Bad words directly put forward, I level too low hope everyone advice
Codeforces Round #177 (Div. 2)---E. Polo the Penguin and XOR operation