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

Source: Internet
Author: User

Little Penguin Polo likes permutations. But the most of the all he likes permutations of integers from 0 to n, inclusive.

For permutation p?=?p0,?p1,?...,? pn, Polo have defined its beauty-number.

Expression means applying the operation of bitwise excluding "OR" to numbers x and Y. This operation exists in all modern programming languages, for example, in language C + + and Java It is represented as "^" And in Pascal-as "XOR".

Help him find among all permutations of integers from 0 to n the permutation with the maximum beauty.
Input

The single line contains a positive integer n (1?≤?n?≤?106).
Output

The first line print integer m the maximum possible beauty. In the second line print any permutation of integers from 0 to n with the beauty equal to M.

If there is several suitable permutations, you is allowed to print any of them.
Sample Test (s)
Input

4

Output

20
0 2 1) 4 3

Observations can be found, two-digit XOR or later, if the binary each bit is 1, then it must be the largest, so our strategy is to enumerate each 2 i ? 1 , of course, starting from the one that is greater than or equal to N, and then each time to find the satisfaction that meets A+B= 2 i ? 1 The number

/************************************************************************* > File name:cf-177-e.cpp > Aut Hor:alex > Mail: [email protected] > Created time:2015 April 08 Wednesday 16:01 33 seconds ******************************* *****************************************/#include <functional>#include <algorithm>#include <iostream>#include <fstream>#include <cstring>#include <cstdio>#include <cmath>#include <cstdlib>#include <queue>#include <stack>#include <map>#include <bitset>#include <set>#include <vector>using namespace STD;Const DoublePI =ACOs(-1.0);Const intINF =0x3f3f3f3f;Const DoubleEPS =1e-15;typedef Long LongLL;typedefPair <int,int> PLL;intper[1001000];intMain () {intN while(~scanf("%d", &n)) {intUse1 =0, Use2 =0; for(inti =1; I <= N;        ++i) {use1 ^= i; } LL ans =0;intm = n;intHigh =0; while(High < N) {high =2* High +1; }intUse while(high) {use = n; while(Use >0&& high-use <= N) {ans + = high;                Per[use] = High-use;                Use2 ^= (High-use);            --use; } High >>=1;        n = use; } per[0] = (use2 ^ use1); Ans + = per[0];printf("%i64d\n", ans);printf("%d", per[0]); for(inti =1; I <= m; ++i) {printf("%d", Per[i]); }printf("\ n"); }return 0;}

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

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.