Odd number of times?

Source: Internet
Author: User

Test instructions

Given the n logarithm, which indicates that there are 2 * n numbers, only 2 have an odd number of times, the memory limit is 1MB, and now you need to know what these two numbers are.

Exercises

First of all, this problem only two numbers appear odd number of times, then the rest of the number appears even several times, it is easy to think of the "^", the nature of satisfaction is the same number of effects will be offset.

So in the sense of the binary, for the I bit, the record of the number I is 1 of all the numbers of the XOR, then for this array can only appear two values, 0,ans0,ans1,ans0^ans1

And then casually find Ans0, and then with all the number of XOR, different or a bit will know ans1 ~

Code:

#include <cstdio> #include <vector> #include <iostream> #include <algorithm>using namespace std ; #define LL Long Longll s[31], X, Xor, Ans[2];int n;void Add (ll x) {for (int i = 1LL; I >= 0;---I) if ((<< i) & x) s[i] ^= x;} int main () {scanf ("%d", &n), for (int i = 1; I <= 2 * n; ++i) {scanf ("%i64d", &x); Xor ^= X;add (x);} for (int i = +; I >= 0;--i) if (s[i]! = Xor && s[i]) {ans[0] = s[i];break;} Ans[1] = Xor ^ ans[0];if (ans[0] > Ans[1]) swap (ans[0], ans[1]), cout << ans[0] << "" << Ans[1];retur n 0;}

  

Summarize:

Discover the nature of the topic, and then what is missing to maintain what Qaq

Odd number of times?

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.