HRBUST-1688 The XOR in number theory

Source: Internet
Author: User

Given two sets of a and B, find the smallest nonnegative integer x to make the a⊕x=b.
Suppose A={a1,a2,..., an}, A⊕x={a1⊕x,a2⊕x,..., an⊕x}.⊕ represent XOR or operation
The first line of input inputs is an integer t representing a total of T-group test data;
For each set of test data, the first row is an integer n representing the size of the collection A and B
The second line contains n integers a1,a2,a3,.... An, which represents the elements of collection A.
The third line contains n integers b1,b2,b3,.... bn, representing the elements of collection B.
(1<=n<=100000,n is odd, 0<=ai<2^30)

Output if x is present, outputs the smallest x, or 1 if it does not exist. Sample Input 1
3
0 1 3
1 2 3
Sample Output 2


#include <stdio.h>

#include <algorithm>
using namespace std;
int main ()
{
	int t,n,ans,f,i;
	int a[100005],b[100005],c[100005];
	scanf ("%d", &t);
	while (t--)
	{
		scanf ("%d", &n);
		ans=0;
		for (i=0;i<n;i++)
		{
			scanf ("%d", &a[i]);
			Ans=ans^a[i];
		}  
		for (i=0;i<n;i++)
		{
			scanf ("%d", &b[i]);
			Ans=ans^b[i];	
		}
	    for (i=0;i<n;i++)
	      c[i]=ans^a[i];
	    Sort (c,c+n);
	    Sort (b,b+n);
	    f=0;
	    for (i=0;i<n;i++)
	    {
	    	if (C[i]==b[i])
	    	  continue;
	    	else
	    	{
	    		f=1;
	    		break;
			}
		}
		if (f==1)
		  printf (" -1\n");
		else
		  printf ("%d\n", ans);
	}
	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.