HDU 5014 Number Sequence (subject H of Xi'an network competition), hdu5014
HDU 5014 Number Sequence
Question Link
Train of Thought: for 0-N, try not to let the 1 in the binary disappear is the best, so as long as the two numbers complement each other, so that each time from the largest number, we can find the numbers that complement each other, and then we can determine the range. Then we can solve the remaining recursion for a subproblem.
Code:
# Include <cstdio> # include <cstring> const int N = 100005; int n, a [N], ans [N]; int cnt [N]; int count (int x) {if (x = 0) return 1; int ans = 0; while (x) {ans ++; x >>=1 ;} return ans;} void dfs (int n) {if (n <= 0) return; int tmp = (n ^ cnt [n]); for (int I = n; i> = tmp; I --) {ans [I] = n + tmp-I;} dfs (tmp-1);} int main () {for (int I = 0; I <N; I ++) cnt [I] = (1 <count (I)-1); while (~ Scanf ("% d", & n) {for (int I = 0; I <= n; I ++) scanf ("% d ", & a [I]); memset (ans, 0, sizeof (ans); dfs (n); long out = 0; for (int I = 0; I <= n; I ++) out + = (long) (a [I] ^ ans [a [I]); printf ("% I64d \ n ", out); for (int I = 0; I <= n; I ++) printf ("% d % c", ans [a [I], I = n? '\ N': '');} return 0 ;}