HDU 5014 Number Sequence (subject H of Xi'an network competition), hdu5014

Source: Internet
Author: User

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 ;}





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.