ZOJ Problem Set – 3418 Binary Number

來源:互聯網
上載者:User

Time Limit: 2 Seconds      Memory Limit: 65536 KB

For 2 non-negative integers x and y, f(x, y) is defined as the number of different bits in the binary format of x and y. For example, f(2, 3)=1, f(0, 3)=2, f(5, 10)=4.

Now given 2 sets of non-negative integers A and B, for each integer b in B, you should find an integer a in A such that f(a, b) is minimized. If there are more than one such integers in set A, choose the smallest one.

Input

The first line of the input is an integer T (0 < T ≤ 100), indicating the number of test cases. The first line of each test case contains 2 positive integers m and n (0 < m, n ≤ 100), indicating the numbers of integers of the 2 sets A and B, respectively. Then follow (m + n) lines, each of which contains a non-negative integers no larger than 1000000. The first m lines are the integers in set A and the other n lines are the integers in set B.

Output

For each test case you should output n lines, 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

關鍵點:兩數抑或運算得到兩數二進位表示時不同的位,通過位移和與運算統計兩數不同在二進位表示時不同尾數的個數。

  1: #include<iostream>
  2: #include<vector>
  3: 
  4: using namespace std;
  5: /* 
  6:  * 尋找兩個數以二進位表示時的不同位元的個數
  7: */
  8: unsigned  countDef(unsigned a,unsigned b){
  9:     int result = 0;
 10:         
 11:     for(int c = a^b; c > 0;c = c >> 1){
 12:         if(c&1 > 0){
 13:             result ++;
 14:         }
 15:     }
 16:     return result;
 17: }
 18: /* 
 19:  * 尋找A中最合格數字a
 20: */
 21: unsigned findMinCountInA(const vector<unsigned>& set, unsigned b){
 22:     unsigned min = set[0];
 23:     unsigned count = countDef(set[0], b);
 24:     unsigned count_ = 0;
 25:     for(unsigned i = 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:     return min;
 35: }
 36: int main(){
 37:     unsigned cases = 0;
 38:     cin >> cases;
 39:     unsigned aSize = 0, bSize = 0;
 40:     while(cases--){
 41:         cin>>aSize>>bSize; 
 42:         vector<unsigned> aSet;//用來存放容器A中的數字
 43:         unsigned elemIna = 0;
 44:         while(aSize--){
 45:             cin>>elemIna;
 46:             aSet.push_back(elemIna);
 47:         }
 48:         unsigned elemInb = 0;
 49:         while(bSize--){
 50:             cin>>elemInb;
 51:             cout<<findMinCountInA(aSet, elemInb)<<endl;
 52:         }
 53:     }
 54: }
 55: 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.