【CodeForces - 289E 】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 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;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441126.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

龙果学院mysql分布式集群代码_龙果学院-MySQL大型分布式集群解决方案

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼1 课程概述2 课程背景3 纵观大型网站架构发展&#xff0c;总结持久化部分需要应对的问题4 操作系统安装以及配置5 在CentOS上通过yum安装mysql5.76 mysql初次见面-mysql5.7的用户以及安全策略7 mysql初次见面续-mysql基本操作8 认识…

【SPOJ - TOURS 387】Travelling tours (最小费用最大流,拆点)

题干&#xff1a; In Hanoi, there are N beauty-spots (2 < N < 200), connected by M one-way streets. The length of each street does not exceed 10000. You are the director of a travel agency, and you want to create some tours around the city which sati…

python问题化教学设计_基于IPO的Python教学设计

冯艳茹 陈平摘要&#xff1a;程序设计基础课程是培养大学生解决计算问题的思维和能力的课程&#xff0c;使用Python作为大学生的首门编程语言课程&#xff0c;可操作性强&#xff0c;入门容易&#xff0c;上手快。该文提出了基于IPO的教学设计新思维&#xff0c;使教学活动和教…

【SPOJ - SCITIES】Selfish Cities (二分图最优匹配,最大费用流)

题干&#xff1a; Far, far away there is a world known as Selfishland because of the nature of its inhabitants. Hard times have forced the cities of Selfishland to exchange goods among each other. C1 cities are willing to sell some goods and the other C2 c…

将方孔分段的lisp_AutoLisp编写工程地质剖面图小工具

AutoLisp编写工程地质剖面图小工具朱红雷李健民 (浙江省水利水电勘测设计院杭州 310002)在我院应用的CAD工程地质制图系统中&#xff0c;通常采用的各种高级语言编制的程序&#xff0c;一般是通过编制数据文件&#xff0c;生成CAD图形数据交换文件(一般为*.SCR或*.DXF)达到绘制…

【CodeForces - 922B 】Magic Forest (数学,异或,暴力,水题,三元组问题)

题干&#xff1a; Imp is in a magic forest, where xorangles grow (wut?) A xorangle of order n is such a non-degenerate triangle, that lengths of its sides are integers not exceeding n, and the xor-sum of the lengths is equal to zero. Imp has to count the …

【牛客 -2A】矩阵(二分,字符串哈希)

题干&#xff1a; 给出一个n * m的矩阵。让你从中发现一个最大的正方形。使得这样子的正方形在矩阵中出现了至少两次。输出最大正方形的边长。 输入描述: 第一行两个整数n, m代表矩阵的长和宽&#xff1b; 接下来n行&#xff0c;每行m个字符&#xff08;小写字母&#xff09…

java生产者消费者代码_Java实现Kafka生产者消费者代码实例

Kafka的结构与RabbitMQ类似&#xff0c;消息生产者向Kafka服务器发送消息&#xff0c;Kafka接收消息后&#xff0c;再投递给消费者。生产者的消费会被发送到Topic中&#xff0c;Topic中保存着各类数据&#xff0c;每一条数据都使用键、值进行保存。每一个Topic中都包含一个或多…

java dom创建xml文件_Java 如何使用dom方式读取和创建xml文件

Java 如何使用dom方式读取和创建xml文件发布时间&#xff1a;2020-11-11 17:08:31来源&#xff1a;亿速云阅读&#xff1a;101作者&#xff1a;Leah本篇文章给大家分享的是有关Java 如何使用dom方式读取和创建xml文件&#xff0c;小编觉得挺实用的&#xff0c;因此分享给大家学…

【CodeForces - 304B】Calendar (前缀和,水题)

题干&#xff1a; Calendars in widespread use today include the Gregorian calendar, which is the de facto international standard, and is used almost everywhere in the world for civil purposes. The Gregorian reform modified the Julian calendars scheme of le…

java 刷新jtextarea_Java JTextArea不能实时刷新的问题

相信JTextArea用法都了解吧&#xff0c;JTextArea textArea new JTextArea();生成一块空白的区域&#xff0c; 我的需求就是点击发送邮件按钮后&#xff0c;后台的执行日志能输出到textArea中。但是我点击发送按钮的时候&#xff0c;由于邮件的附件要好久&#xff0c;界面一直…

【CodeForces - 312C】The Closest Pair (思维)

题干&#xff1a; Currently Tiny is learning Computational Geometry. When trying to solve a problem called "The Closest Pair Of Points In The Plane", he found that a code which gave a wrong time complexity got Accepted instead of Time Limit Excee…

java request 封装对象_java通过request自动封装复杂对象

参考&#xff1a;Jfinal源码&#xff0c;在上面基础上改的&#xff0c;然后分享出来适用JAVAEE平台[Java]代码/*** 实现深层封装对象的实例 从request封装对象* 举例&#xff1a;* House.class 属性有三个 ID:id 名称&#xff1a;name 门类&#xff1a;Door doorDoor类: id nam…

【UVA - 10020 】Minimal coverage (贪心,区间覆盖问题)

题干&#xff1a;&#xff08;Uva题不给题干了&#xff09; t组样例&#xff0c;每组首先给出一个M&#xff0c;然后给出一些线段&#xff08;0 0结束&#xff09;&#xff0c;然后问怎么取能使得最少的线段覆盖区间[0, M]。 Sample Input 2 1 -1 0 -5 -3 2 5 0 0 1 -1 0 0 1 …

java邮箱地址正则表达式_Java 中用正则表达式修改 Email 地址

需求系统中有一列会用来存储 email 地址&#xff0c;现在需要对输入的字符串进行过滤&#xff0c;要求是&#xff0c;把无效的地址过滤掉。有一些需要说明的是这些地址是通过图像识别得到的&#xff0c;有些是用户自己输入的已有历史记录已经存在了脏数据&#xff0c;需要替换这…

【CodeForces - 305C】Ivan and Powers of Two(思维,STL,set,优先队列)

题干&#xff1a; Ivan has got an array of n non-negative integers a1, a2, ..., an. Ivan knows that the array is sorted in the non-decreasing order. Ivan wrote out integers 2a1, 2a2, ..., 2an on a piece of paper. Now he wonders, what minimum number …

【CodeForces - 1082B】Vova and Trophies (贪心模拟,暴力)

题干&#xff1a; Vova has won nn trophies in different competitions. Each trophy is either golden or silver. The trophies are arranged in a row. The beauty of the arrangement is the length of the longest subsegment consisting of golden trophies. Vova wan…

java mac 转换 整形_JAVA的整型与字符串相互转换

1如何将字串 String 转换成整数 int?A. 有两个方法:1). int i Integer.parseInt([String]); 或i Integer.parseInt([String],[int radix]);2). int i Integer.valueOf(my_str).intValue();注: 字串转成 Double, Float, Long 的方法大同小异.2 如何将整数 int 转换成字串 St…

【CodeForces - 798A】Mike and palindrome (回文串,水题,字符串问题)

题干&#xff1a; Mike has a string s consisting of only lowercase English letters. He wants to change exactly one character from the string so that the resulting one is a palindrome. A palindrome is a string that reads the same backward as forward, for e…

java迷宫生成算法_DFS算法迷宫生成器

我正在尝试使用DFS算法在ASCII中创建迷宫(&#xff03;表示墙和自由空间)&#xff0c;其左上角开始&#xff0c;右下角出口 . 问题是迷宫开始创建&#xff0c;然后它被阻止&#xff0c;因为它的所有邻居都已被访问过 .我从左上角开始&#xff0c;将单元格标记为已访问并放置一个…