Codeforces Round #177 (Div. 2)---E. Polo the Penguin and XOR operation

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.