Zoj problem set-3418 binary number

Source: Internet
Author: User

Time Limit: 2 seconds memory limit: 65536 KB

For 2 non-negative integersXAndY, F (X,Y) Is defined as the number of different bits in the binary formatXAndY. For example, F (2, 3) = 1, F (0, 3) = 2, F (5, 10) = 4.

Now given 2 sets of non-negative integersAAndB, For each integerBInB, You shoshould find an integerAInASuch that F (A,B) Is minimized. If there are more than one such integers in SetA, Choose the smallest one.

Input

The first line of the input is an integerT(0 <T≤ 100), indicating the number of test cases. The first line of each test case contains 2 positive integersMAndN(0 <M,N≤ 100), indicating the numbers of Integers of the 2 setsAAndB, Respectively. Then follow (M+N) Lines, each of which contains a non-negative integers no larger than 1000000. The firstMLines are the integers in SetAAnd the otherNLines are the integers in SetB.

Output

For each test case you shoshould outputNLines, each of which contains the result for each query in a single line.

Sample Input

 
22 512123455 21000000999914233421013245353

Sample output

 
1211199990

Author:Cao, Peng
Source:The 2010 ACM-ICPC Asia Chengdu Regional Contest

Key Point: two numbers or different bits in binary representation are obtained through calculation. The number of different tails in binary representation is measured by displacement and operation.

 
1: # include <iostream>
 
2: # include <vector>
 
3:
 
4:Using NamespaceSTD;
 
5:/*
 
6: * search for two numbers of different digits in binary representation.
 
7 :*/
 
8:UnsignedCountdef (UnsignedA,UnsignedB ){
 
9:IntResult = 0;
 
10:
11:For(IntC = a ^ B; C> 0; C = C> 1 ){
 
12:If(C & 1> 0 ){
 
13: Result ++;
 
14 :}
 
15 :}
 
16:ReturnResult;
 
17 :}
 
18:/*
 
19: * find the most qualified number A in.
 
20 :*/
 
21:UnsignedFindmincountina (ConstVector <Unsigned> & Set,UnsignedB ){
 
22:UnsignedMin = set [0];
23:UnsignedCount = countdef (set [0], B );
 
24:UnsignedCount _ = 0;
 
25:For(UnsignedI = 1; I <set. Size (); I ++ ){
 
26: Count _ = countdef (set [I], B );
 
27:If(Count _ <count ){
 
28: min = set [I];
 
29: Count = count _;
 
30 :}Else If(Count _ = count & set [I] <min ){
 
31: min = set [I];
 
32 :}
 
33 :}
 
34:ReturnMin;
35 :}
 
36:IntMain (){
 
37:UnsignedCases = 0;
 
38: CIN> cases;
 
39:UnsignedAsize = 0, bsize = 0;
 
40:While(Cases --){
 
41: CIN> asize> bsize;
 
42: vector <Unsigned> Aset;// Used to store the number in container
 
43:UnsignedElemina = 0;
 
44:While(Asize --){
 
45: CIN> elemina;
 
46: aset. push_back (elemina );
47 :}
 
48:UnsignedEleminb = 0;
 
49:While(Bsize --){
 
50: CIN> eleminb;
 
51: cout <findmincountina (ASET, eleminb) <Endl;
 
52 :}
 
53 :}
 
54 :}
 
55:

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.