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

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

For permutation p?=?p0,?p1,?…,?pn, Polo has 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

In 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 are several suitable permutations, you are allowed to print any of them.

Sample test(s)

Input

4

Output

20

0 2 1 4 3

观察可以发现,两个数异或以后,如果二进制每一位都为1,那么一定最大,所以我们的策略是,枚举每一个2i ?1,当然是从大于等于n的那个开始,然后每次去找符合的满足 A+B=2i ?1的数

/*************************************************************************
    > File Name: CF-177-E.cpp
    > Author: ALex
    > Mail: [email protected]
    > Created Time: 2015年04月08日 星期三 16时01分33秒
 ************************************************************************/

#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 double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;

int per[1001000];

int main()
{
    int n;
    while (~scanf("%d", &n))
    {
        int use1 = 0, use2 = 0;
        for (int i = 1; i <= n; ++i)
        {
            use1 ^= i;
        }
        LL ans = 0;
        int m = n;
        int high = 0;
        while (high < n)
        {
            high = 2 * high + 1;
        }
        int use;
        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 (int i = 1; i <= m; ++i)
        {
            printf(" %d", per[i]);
        }
        printf("\n");
    }
    return 0;
}
时间: 2024-10-11 06:45:18

Codeforces Round #177 (Div. 2)---E. Polo the Penguin and XOR operation(贪心)的相关文章

Codeforces Round #177 (Div. 1) C. Polo the Penguin and XOR operation(贪心)

题目地址:http://codeforces.com/problemset/problem/288/C 思路:保证位数尽量大的情况下使得每二进制位均为一的情况下结果最大,从后向前枚举(保证位数尽量大),num表示该数i二进制有几位,x即为使得与i异或后每二进制位均为一的数,v[x]标记是否使用过该数,若未使用则与i搭配. #include<cstdio> #include<cmath> #include<cstring> #include<iostream>

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

题意:让你构造一个序列,使得序列异或和最大,序列为n 的全排列 ,序列和计算方式为 SUM = a[1] ^ 0 + a[2] ^ 1 + a[3] ^ 2 + .......a[n] ^ n 感想 :之前没做过有关位运算的题,对这一块很陌生,两个数异或以后,如果二进制每一位都为1,那么一定最大,找规律发现当n为偶数时 除开0以外,其他的都是成对出现 当n为奇数时 都是成对出现 例如n=4, 0 1 2 3 4 分别对应0 2 1 4 3 :n=5时 0 1 2 3 4 5 分别对应1 0 5

Codeforces Round #177 (Div. 2)---D. Polo the Penguin and Houses (组合数学+暴力)

Little penguin Polo loves his home village. The village has n houses, indexed by integers from 1 to n. Each house has a plaque containing an integer, the i-th house has a plaque containing integer pi (1?≤?pi?≤?n). Little penguin Polo loves walking ar

Codeforces Round #177 (Div. 2) 题解

[前言]咦?现在怎么流行打CF了?于是当一帮大爷在执着的打div 1的时候,我偷偷的在刷div 2.至于怎么决定场次嘛,一般我报一个数字A,随便再拉一个人选一个数字B.然后开始做第A^B场.如果觉得机密性不高,来点取模吧.然后今天做的这场少有的AK了.(其实模拟赛只做完了4题,最后1题来不及打了) 等等,话说前面几题不用写题解了?算了,让我难得风光一下啦. [A] A. Polo the Penguin and Segments time limit per test 2 seconds mem

Codeforces Round #606 (Div. 2) D. Let&#39;s Play the Words?(贪心+map)

?? ?? ?? 题意:给你一些序列,要求把这些序列翻转之后能首尾相连(01,10),并且字符串不能相同,询问最小步数: 1.我们只关心这个字符串首尾位置,一共只有四种情况:00,01,10,11:00 和 11 是没必要翻转的,剩下 01,10 只要存在就可以相互抵消(0110,1001这种),剩下多的 01 or 10,就是我们最后需要翻转的字符串,数量为abs(num01-num10)/2: 2.为了满足字符串不能相同,一开始就记录下哪写字符串不可以翻转,例,假如s翻转之后为rev,rev

Codeforces Round #422 (Div. 2) C. Hacker, pack your bags! 排序,贪心

C. Hacker, pack your bags! It's well known that the best way to distract from something is to do one's favourite thing. Job is such a thing for Leha. So the hacker began to work hard in order to get rid of boredom. It means that Leha began to hack co

Codeforces Round #482 (Div. 2)D. Kuro and GCD and XOR and SUM+字典树

题目链接:D. Kuro and GCD and XOR and SUM 题意:两种操作:第一种给数组添加一个数,第二种输入x,k,s,要求从数组中找到一个数v,要求k能整除gcd(k,v);并且v<=s-x,然后异或v与k的异或值最大. 题解:对与k大于1的情况我们暴力枚举过去,k为1的特殊处理建一颗字典树,如果可以的满足条件的话,每次取值时往相反方向取. 1 #include<bits/stdc++.h> 2 #include <iostream> 3 #include

CodeForces 288C - Polo the Penguin and XOR operation(思维)

题意: 就是让你构造一个序列,使得序列异或和最大,序列为n 的全排列 ,序列和计算方式为   SUM  =   a[1] ^ 0 + a[2] ^ 1 + a[3] ^ 2 + .......a[n] ^ n 构造出一个序列使得和最大 题解: 策略为使得每次异或出来的结果的1尽可能多,而优先从最大的n  开始考虑,因为n  最有可能出更大的数字 代码: #include<stdio.h> #include<string.h> int Ans[1000005]; int main()

CodeForces 288C - Polo the Penguin and XOR operation(思路)

题意:给定一个 n (1 <= n <= 10^6),求(0 ^ p0) + (1 ^ p1) + (2 ^ p2) +-- + (n ^ pn) 的最大值,其中p0 ~ pn为 0 ~ n 中的数,且每个数只利用一次. 从n ~ 0枚举所有的数,对于每个数找它异或后得到二进制都是 1 的数或者 1 尽量多的数. 对于每个数,先找到与它二进制位数相同的全是 1 的数,然后把这个当成结果,与目前的数异或得到另一个数并记录,然后同时记录另一个数(可能用不到,但是为了节省时间). 加起来的结果可能超