Codeforces 288c-polo the Penguin and XOR operation (IDEA)

Source: Internet
Author: User

Test instructions: Given an n (1 <= n <= 10^6), ask (0 ^ p0) + (1 ^ p1) + (2 ^ p2) + ... + (n ^ pn) maximum, where the number of P0 ~ PN is 0 ~ N, and each number is only used once.

Enumerate all the numbers from N ~ 0, for each number to find it xor or to get the binary to be 1 of the number or 1 as many as possible.

For each number, first find the same number of bits as it is 1, and then take this as a result, with the current number of different or get another number and record, and then simultaneously record another number (may not be used, but in order to save time).

The combined result may be super int, so a long long is used.

#include <cstdio>#include<cstring>#include<cctype>#include<cstdlib>#include<cmath>#include<iostream>#include<sstream>#include<iterator>#include<algorithm>#include<string>#include<vector>#include<Set>#include<map>#include<deque>#include<queue>#include<stack>#include<list>typedefLong Longll; typedef unsignedLong LongLlu; Const intMAXN = -+Ten; Const intMaxt =1000000+Ten; Const intINF =0x7f7f7f7f; Const DoublePI = ACOs (-1.0); Const DoubleEPS = 1e-6; using namespacestd; intN, Ans[maxt]; intMain () {scanf ("%d", &N); memset (ans,-1,sizeofans);  for(inti = n; I >=0; --i) {          if(Ans[i]! =-1)Continue; intK1 = log2 (i) +1;//find out the number of bits and add one to prepare for the next step        intK2 = (1<< K1)-1;//find the number with the same number of digits as the binary representation of all 1Ans[k2 ^ i] = i;//RecordAns[i] = K2 ^ i;//Record} ll sum=0;  for(inti =0; I <= N; ++i) Sum+ = ll (i ^Ans[i]); printf ("%i64d\n", sum);  for(inti =0; I <= N; ++i) {          if(i) printf (" "); printf ("%d", Ans[i]); } printf ("\ n"); return 0; }  

Codeforces 288c-polo the Penguin and XOR operation (IDEA)

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.