Test instructions: Given an n (1 <= n <= 10^6), ask (0 ^ p0) + (1 ^ p1) + (2 ^ p2) + ... + (n ^ pn) maximum, where the number of P0 ~ PN is 0 ~ N, and each number is only used once.
Enumerate all the numbers from N ~ 0, for each number to find it xor or to get the binary to be 1 of the number or 1 as many as possible.
For each number, first find the same number of bits as it is 1, and then take this as a result, with the current number of different or get another number and record, and then simultaneously record another number (may not be used, but in order to save time).
The combined result may be super int, so a long long is used.
#include <cstdio>#include<cstring>#include<cctype>#include<cstdlib>#include<cmath>#include<iostream>#include<sstream>#include<iterator>#include<algorithm>#include<string>#include<vector>#include<Set>#include<map>#include<deque>#include<queue>#include<stack>#include<list>typedefLong Longll; typedef unsignedLong LongLlu; Const intMAXN = -+Ten; Const intMaxt =1000000+Ten; Const intINF =0x7f7f7f7f; Const DoublePI = ACOs (-1.0); Const DoubleEPS = 1e-6; using namespacestd; intN, Ans[maxt]; intMain () {scanf ("%d", &N); memset (ans,-1,sizeofans); for(inti = n; I >=0; --i) { if(Ans[i]! =-1)Continue; intK1 = log2 (i) +1;//find out the number of bits and add one to prepare for the next step intK2 = (1<< K1)-1;//find the number with the same number of digits as the binary representation of all 1Ans[k2 ^ i] = i;//RecordAns[i] = K2 ^ i;//Record} ll sum=0; for(inti =0; I <= N; ++i) Sum+ = ll (i ^Ans[i]); printf ("%i64d\n", sum); for(inti =0; I <= N; ++i) { if(i) printf (" "); printf ("%d", Ans[i]); } printf ("\ n"); return 0; }
Codeforces 288c-polo the Penguin and XOR operation (IDEA)