Codeforces Round #177 (Div. 2)---E. Polo the Penguin and XOR operation(贪心)
2015-04-08 16:56
561 查看
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,那么一定最大,所以我们的策略是,枚举每一个2i2^{i} −1 - 1,当然是从大于等于n的那个开始,然后每次去找符合的满足 A+B=2iA + B = 2^{i} −1 - 1的数
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,那么一定最大,所以我们的策略是,枚举每一个2i2^{i} −1 - 1,当然是从大于等于n的那个开始,然后每次去找符合的满足 A+B=2iA + B = 2^{i} −1 - 1的数
/************************************************************************* > File Name: CF-177-E.cpp > Author: ALex > Mail: zchao1995@gmail.com > 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; }
相关文章推荐
- Codeforces Round #177 (Div. 1)C. Polo the Penguin and XOR operation【贪心】
- 【贪心】Codeforces Round #177 (Div. 1) C - Polo the Penguin and XOR operation
- Codeforces Round #177 (Div. 1) C. Polo the Penguin and XOR operation(贪心)
- CF 288C (Polo the Penguin and XOR operation)
- CodeForces 288C Polo the Penguin and XOR operation
- CodeForces 288C - Polo the Penguin and XOR operation
- CodeForces 288C Polo the Penguin and XOR operation (位运算,异或)
- CodeForces 288C - Polo the Penguin and XOR operation(思维)
- Codeforces Round #177 (Div. 1) B - Polo the Penguin and Houses
- Codeforces Round #177 (Div. 2)---D. Polo the Penguin and Houses (组合数学+暴力)
- Codeforces Round #177 (Div. 1) A - Polo the Penguin and Strings
- Codeforces Round #177 (Div. 1) B. Polo the Penguin and Houses【组合数学】
- Codeforces Polo the Penguin and Matrix
- codeforces 289B - Polo the Penguin and Matrix 二分+dp
- CF_288B_PoloThePenguinAndHouses
- B. Polo the Penguin and Matrix
- A. Polo the Penguin and Strings
- Codeforces-289a I Polo the Penguin and Segments
- Codeforces-289b J Polo the Penguin and Matrix
- 找规律/贪心 Codeforces Round #310 (Div. 2) A. Case of the Zeros and Ones