Acdream XOR simple mathematics

Source: Internet
Author: User

Given a set a, a set B, A, and B have the same number of elements, then, ask if there is a number x so that all elements in a are in the same bit or the result of the operation is a set of B. If there is a minimum number of output, there is no output-1.

Idea: given n is an odd number, you can determine the unique group based on the rightmost bit of the binary, And then you only need to determine whether the group is reasonable.

Groups are divided in this way. If there are data groups A and B, group A is divided into two groups based on the last 0 and 1. Group B is also divided, then only 00 pairs or 01 pairs are matched, this is determined by the number of groups. After the matching mode is determined, the matching mode can be determined 30 times.

# Include <cstdlib> # include <cstdio> # include <iostream> # include <cstring> # include <cstdio> # include <cstdlib> using namespace STD; const int maxn = 100005; int A [maxn], B [maxn], n; int ans [40]; int st [4] [maxn]; int CNT [4], bit [4] [40]; // if n is an odd number, int CAL (int x, int B []) {for (INT I = 0; I <30; ++ I) {If (x> I) & 1) ++ B [I] ;}} int judge (int I, int Mode) {// If the mode is 0, that is, st0 matches st2 if (mode = 0) {If (bit [0] [I] = bit [2] [I] & bit [1] [I] = bit [3] [I]) return 0; else if (bit [0] [I] = CNT [2]-bit [2] [I] & bit [1] [I] = CNT [3]- bit [3] [I]) return 1; else return-1;} else {// If Mode 1 is used, that is, if (bit [0] [I] = bit [3] [I] & bit [1] [I] = bit [2] [I ]) return 0; else if (bit [0] [I] = CNT [3]-bit [3] [I] & bit [1] [I] = CNT [2]- bit [2] [I]) return 1; else return-1 ;}} void Gao () {for (INT I = 0; I <4; ++ I) {memse T (ST [I], 0, sizeof (ST [I]); memset (bit [I], 0, sizeof (bit [I]);} memset (ANS, 0, sizeof (ANS); memset (CNT, 0, sizeof (CNT); For (INT I = 0; I <n; ++ I) {If (! (A [I] & 1) ST [0] [CNT [0] ++] = I; else st [1] [CNT [1] ++] = I; if (! (B [I] & 1) ST [2] [CNT [2] ++] = I; else st [3] [CNT [3] ++] = I ;}for (INT I = 0; I <4; ++ I) {for (Int J = 0; j <CNT [I]; ++ J) {if (I <2) Cal (A [st [I] [J], bit [I]); else CAL (B [st [I] [J], bit [I]) ;}} int mode =-1; // This mode is divided by the number of values of 1 in the second bit, since N is an odd number, the partitioning method is unique if (CNT [0] = CNT [2]) ans [0] = mode = 0; else if (CNT [0] = CNT [3]) ans [0] = mode = 1; if (mode =-1) {puts ("-1 "); return;} For (INT I = 1; I <30; + + I) {ans [I] = judge (I, mode); If (ANS [I] =-1) {puts ("-1"); return ;}} int ret = 0; For (INT I = 0; I <30; ++ I) {RET + = ans [I]? (1 <I): 0;} printf ("% d \ n", RET);} int main () {While (scanf ("% d", & N) = 1) {for (INT I = 0; I <n; ++ I) {scanf ("% d", & A [I]);} for (INT I = 0; I <n; ++ I) {scanf ("% d", & B [I]) ;} Gao () ;}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.