题干:

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 xand 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.

Examples

Input

4

Output

20
0 2 1 4 3

题目大意:

给n个数,分成两组A和B,A中的可以找B中的连线,求最优匹配,权值为二者的异或值。(

解题报告:

我把题目大意搞得跟二分图一样,,,但是其实不是的。。只是方便理解(看数据量也知道不是)

可以证明,一定对于每一个数都找到一个对应的匹配

Since we need to maximize the result, we need to find such permutation, for which the least number of bit disappear. (We consider bit disappeared if it was 1 both in i and pi, so in  it is 0). It turns out that for each n there is such permutation that no bit disappear. How to build it? We will be solving problem by iterations while n > 0. On each iteration, we need to find the biggest (the leftmost in binary representation) bit which is not 0 in binary representation of n and denote it position (bits are numbered from 0) by b. Now we need to find integer m — minimal integer from 0 to n, inclusive, such that b-th bit is also 1 in it. After that you can see (look image below), that at  no bit disappear, at  no bit disappear, ..., at  no bit disappear. So, it is good to assign exactly that integers to our permutation, i. e. pm = m - 1 and pm - 1 = mpm + 1 = m - 2 and pm - 2 = m + 1 and so on. After that assign value m - (n - m + 1) - 1 to n and go to next iteration.

Now when we know how to build permutation and that no bit disappear, the value of the answer is equal to .

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
using namespace std;
const int MAX=1e6 + 5;
int ans[MAX];
ll sum=0,x=1;
int main() {int n;cin >>n;while(x<=n) x<<=1;x--; for(int i=n; i>=0; i--) {if(ans[i]!=0) continue;while((x^i)>n||ans[x^i]!=0) x>>=1;ans[x^i]=i;ans[i]=x^i;}sum=0;for(int i=0; i<=n; i++)sum+=i^ans[i];cout <<sum<<endl;for(int i=0; i<=n; i++) {printf("%d%c",ans[i],i == n ? '\n' : ' ');}return 0;
}

【CodeForces - 289E 】Polo the Penguin and XOR operation (数学,异或,贪心)相关推荐

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

    http://codeforces.com/contest/288/problem/C 按二进制位从大到小进行匹配 #include <cstdio> #include <cstri ...

  2. 289B. Polo the Penguin and Matrix

    B. Polo the Penguin and Matrix:题目 思路:纯暴力 #include <bits/stdc++.h> using namespace std; // #def ...

  3. BUUCTF-Reverse:xor(涉及异或脚本编写)

    xor一般指异或.异或,英文为exclusive OR,缩写成xor异或(eor)是一个数学运算符. 题目地址:https://buuoj.cn/challenges#xor 用IDA载入,寻找mai ...

  4. Codeforces Round #774 (Div. 2) Power Board(数学)

    Codeforces Round #774 (Div. 2) Power Board(数学) 链接 题意:给一个n*m的矩阵,每个元素的值是 i j i^j ij,问有多少个不一样的元素 思路:不过这 ...

  5. 【CodeForces - 289D】Polo the Penguin and Houses (带标号的无根树,Cayley定理,Prufer编码)

    题干: Little penguin Polo loves his home village. The village has n houses, indexed by integers from 1 ...

  6. 【CodeForces - 289C】Polo the Penguin and Strings (水题,字符串,思维构造,有坑)

    题干: Little penguin Polo adores strings. But most of all he adores strings of length n. One day he wa ...

  7. 【Codeforces/HDU】76A Plus and xor / 2095 find your present (2)(异或)。

    http://codeforces.com/contest/76/problem/D A = X + Y B = X xor Y 异或(不进位加法):两个二进制数,对应的位置上,相同为0,不同为1 性 ...

  8. Codeforces Round #743 (Div. 2) D. Xor of 3 模拟 + 构造

    传送门 文章目录 题意: 思路: 题意: 给你一个010101序列aaa,定义一次操作是选择一个[1,n−2][1,n-2][1,n−2]范围内的下表,将ai,ai+1,ai+2a_i,a_{i+1} ...

  9. Polo the Penguin and Matrix

    Little penguin Polo has an n × m matrix, consisting of integers. Let's index the matrix rows from 1 ...

最新文章

  1. C++设计模式(转)
  2. [操作系统]从零开始的OS
  3. Chrome浏览器手动添加Cookie的方法
  4. 数据库-优化-慢查日志分析工具-pt-query-digest介绍及作用
  5. Jackson第一篇【JSON字符串、实体之间的相互转换】
  6. 创建虚拟机并加载镜像文件
  7. Go语言: go-redis客户端通过MSet方法一次性批量写入多个key的值
  8. python蒙特卡洛模拟return_python蒙特卡洛脚本模拟—挑战者号爆炸概率
  9. 视频教程:Java常见面试题目深度解析!
  10. c++:苹果和虫子(疑问)
  11. not executable: 64-bit ELF file 已解决
  12. 异构图注意力网络Heterogeneous Graph Attention Network ( HAN )
  13. 华为p10关闭更新_华为P10怎么取消系统更新提醒
  14. Wayland/Weston 启动方式简介
  15. 插入式隔离器的全球与中国市场2022-2028年:技术、参与者、趋势、市场规模及占有率研究报告
  16. 基于NODE.JS与KUE搭建消息队列[转]
  17. CTFHub技能树 Web-XSS 详解
  18. VM虚拟机 -Mac 镜像安装(黑苹果系统)
  19. cpu超线程优缺点_转一篇文章:关于超线程技术的优缺点的。。。
  20. 10款效果惊艳的HTML5应用和源码

热门文章

  1. Membership 数据字典
  2. [Leetcode][第647题][JAVA][回文子串][动态规划][中心扩展][Manacher 算法]
  3. [Leetcode][第733题][JAVA][图像渲染][BFS][DFS]
  4. 大学物理实验长度的测量实验报告_大学物理实验教案长度和质量的测量两篇
  5. 仿苹果涂鸦软件_这些iPhone自带软件,学会一个少装十几个APP,16G手机也够用
  6. php8vsgo,服务端 I/O 性能:Node、PHP、Java、Go 的对比
  7. python股票网格交易法详解_干货 | 浅谈网格交易法
  8. 表单内如何直接贴图而不用上传图片_表单如何添加图片?
  9. sqlite3_finalize sqlite3_close
  10. java实现c语言的函数_C语言实现返回字符串函数的四种方法