Cf 251d two sets (bitwise operation, solving the equation)

Source: Internet
Author: User
Tags bitset

Reprint please indicate the source, thank you http://blog.csdn.net/ACM_cxlove? Viewmode = Contents
By --- cxlove

Question: N numbers are given. They are divided into two sets, which can be null.

The first set. The value is exclusive or X1, and the second set is exclusive or X2.

The maximum value of X1 + X2 is required. In the same case, the X1 should be as small as possible.

Http://codeforces.com/problemset/problem/251/D

First of all, I would like to thank Xiaodao for its guidance.

The first step is to think of greed.

Given the sum of the two integers, we separate all bits first.

If n is in the number, there are m digits of the current digit. That is, the current bit has M 1

If M is an even number and M is not 0, it must be divided into two odd sets. For the two sets, the current BIT is 1, and it must be the best

If M is an odd number, it can only be divided into odd + even. If X1 is required to be as small as possible, it must be given to an even number of sets 1.

A11 * X1 ^ A12 * X2... A1N * xn = b1
A21 * X1 ^ A22 * X2... a2n * xn = b2
...
A62_1 * X1 ^ a62_2 * X2 ^... a62_n * xn = b62

Then we can obtain the equation for each bit, solving x1, x2 ...... XN is enough.

Ai_j indicates whether the number of J is 1 or 0

XI indicates whether the number of I is in the 1st sets.

Note: greedy should be from high to low. Greedy

In my writing, when an equation is added, we can start to disagree or process it. We can also do all the equations together.

What I don't understand: Why do I have to be greedy first from an even number? Otherwise, WA will happen. It doesn't affect me, sad.

Is 1 + 1 better than 1 + 0 ???

# Include <iostream> # include <cstdio> # include <map> # include <cstring> # include <cmath> # include <vector> # include <algorithm> # include <set> # include <string> # include <queue> # include <bitset> # define INF 1 <30 # define M 2005 # define n 100000 # define maxn 300005 # define EPS 1e-10 # define zero () FABS (a) <EPS # define min (A, B) (a) <(B )? (A) :( B) # define max (A, B) (a)> (B )? (A) :( B) # define Pb (a) push_back (a) # define MP (a, B) make_pair (a, B) # define MEM (A, B) memset (a, B, sizeof ()) # define ll long # define lson step <1 # define rson step <1 | 1 # define mod 1000000009 # define sqr (A) (a) * ()) # define key_value ch [CH [root] [1] [0] # pragma comment (linker, "/Stack: 1024000000,1024000000") using namespace STD; vector <bitset <n> A; vector <int> B; // equation A * x = B ll num [N]; int N, CNT [62]; int I Mportant [62]; int ans [N] = {0}; void add (bitset <n> A, int B) {int M =. size (); For (INT I = 0; I <m; I ++) {if (a [important [I]) {A ^ = A [I]; B ^ = B [I] ;}} int idx =-1; for (INT I = 0; I <n; I ++) if (a [I]) {idx = I; break;} If (idx =-1) return; important [m] = idx; For (INT I = 0; I <m; I ++) {if (a [I] [idx]) {A [I] ^ = A; B [I] ^ = B ;}}. pb (a); B. pb (B) ;}int main () {scanf ("% d", & N); For (INT I = 0; I <n; I ++) {scanf ("% i64d", & num [I]); For (Int J = 0; j <6 2; j ++) CNT [J] + = (Num [I]> J) & 1 ;}// even for (INT I = 61; I >= 0; I --) {If (CNT [I] &! (CNT [I] & 1) {bitset <n> T; T. reset (); For (Int J = 0; j <n; j ++) T [J] = (Num [J]> I) & 1; add (t, 1) ;}}// odd for (INT I = 61; I> = 0; I --) {If (CNT [I] & (CNT [I] & 1) {bitset <n> T; T. reset (); For (Int J = 0; j <n; j ++) T [J] = (Num [J]> I) & 1; add (t, 0) ;}}for (INT I = 0; I <. size (); I ++) {ans [important [I] = B [I];} For (INT I = 0; I <n; I ++) printf ("% d % C", 2-ans [I], I = n-1? '\ 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.