CF 177(div2) E(greedy )
来源:互联网 发布:it应届生简历模板 编辑:程序博客网 时间:2024/04/30 23:37
Little penguin Polo likes permutations. But most of all he likes permutations of integers from0 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 numbersx and y. This operation exists in all modern programming languages, for example, in languageC++ and Java it is represented as "^" and inPascal — as "xor".
Help him find among all permutations of integers from 0 ton the permutation with the maximum beauty.
The single line contains a positive integer n (1 ≤ n ≤ 106).
In the first line print integer m the maximum possible beauty. In the second line print any permutation of integers from0 to n with the beauty equal tom.
If there are several suitable permutations, you are allowed to print any of them.
4
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <vector>#include <stack>#define LL long longusing namespace std;const int maxn = 1000000 + 5;LL pre[32];int ans[maxn];LL len(int x){ LL ret = 0; while(x){ ret++; x = x/2;; } return ret;}int main(){ int n; LL total; pre[0] = 0; pre[1] = 1; for(int i = 2;i < 32;i++) pre[i] = (pre[i-1]<<1) + 1; while(cin >> n){ memset(ans,-1,sizeof(ans)); total = 0; for(int i = n;i >= 0;i--){ if(ans[i] == -1){ int j = pre[len(i)]-i; ans[i] = j; ans[j] = i; total += 2*pre[len(i)]; } } cout << total << endl; for(int i = 0;i <= n;i++) printf("%d ",ans[i]); cout << endl; } return 0;}
- CF 177(div2) E(greedy )
- CF DIV2 236E Strictly Positive Matrix(强连通)
- E. Paths and Trees (CF 303 div2)最短路
- CF #148 DIV2 E
- cf#ff(div2)E
- CF 260div2 E
- CF div2 341-E
- CF 183(div2)
- CF 162(div2)
- CF 174(div2) C
- CF 171(div2)A
- CF 171 (div2)C
- CF 170(div2)B
- CF #190(DIV2)总结
- cf#383 div2(ABCDE)
- CF-287E(Main Sequence) greedy(贪心)
- cf 151.div2 E Blood Cousins Return
- cf 167.div2 E.Dima and Horses
- 黑马程序员-Java网络编程
- Remove Duplicates from Sorted List II
- 用ASIHTTPRequest 的block方式请求数据
- 【JEECMS】Jeecms 图文滚动显示及常见问题
- <python>字典,语句
- CF 177(div2) E(greedy )
- Mysql主从复制详解
- IBM笔试 银行排队
- 开源android项目分享
- lua程序设计第二版 读书笔记(24-26章)
- c#中的常量
- latex paper abstract 不加编号
- C++构造函数中的初始化
- linux文件目录权限