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

246 篇文章 1 订阅
98 篇文章 1 订阅

题干:

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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值