Title Address: Http://codeforces.com/problemset/problem/288/C
Ideas: To ensure that the number of bits is as large as possible so that each bits is one of the results of the maximum, from the back forward enumeration (guaranteed number of digits as large as possible), NUM indicates that the number I binary has several, X is to make with I xor each bits is a number, V[x] Mark whether the number is used, if not used and I collocation
#include <cstdio> #include <cmath> #include <cstring> #include <iostream> #include < Algorithm>using namespace Std;int n;long long sum=0;int v[1000050];int ans[1000050];int Main () { scanf ("%d", &n); for (int i=n;i>0;i--) { if (!v[i]) { int num= (int) (LOG2 (i)) +1; int x= ((1<<num)-1) ^i; v[x]=1,ans[i]=x,ans[x]=i; Sum+= ((((1<<num)-1) <<1);} } printf ("%i64d\n", sum); for (int i=0;i<n;i++) printf ("%d", ans[i]); printf ("%d\n", Ans[n]); return 0;}
Codeforces Round #177 (Div. 1) C. Polo the Penguin and XOR operation (greedy)