ZOJ Problem Set - 3418 Binary Number
2011-03-17 20:13
225 查看
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
Sample Output
Author: CAO, Peng
Source: The 2010 ACM-ICPC Asia Chengdu Regional Contest
关键点:两数抑或运算得到两数二进制表示时不同的位,通过位移和与运算统计两数不同在二进制表示时不同尾数的个数。
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
2 2 5 1 2 1 2 3 4 5 5 2 1000000 9999 1423 3421 0 13245 353
Sample Output
1 2 1 1 1 9999 0
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:
相关文章推荐
- ZOJ Problem Set - 1078(十进制对任意进制转换)
- ZOJ Problem Set - 1049 I Think I Need a Houseboat
- ZOJ Problem Set - 1205 Martian Addition
- ZOJ Problem Set – 1056 The Worm Turns
- ZOJ Problem Set - 3623Battle Ships
- ZOJ Problem Set - 2397 Tian Ji -- The Horse Racing
- ZOJ Problem Set - 2932 The Seven Percent Solution
- ZOJ Problem Set - 2412
- ZOJ Problem Set - 1003 Crashing Balloon
- ZOJ Problem Set - 2412解题报告
- ZOJ Problem Set - 2676 Network Wars
- ZOJ Problem Set - 1423 (Your)((Term)((Project)))
- ZOJ Problem Set - 1243 URLs
- ZOJ Problem Set - 3816 Generalized Palindromic Number 搜索+贪心
- ZOJ Problem Set - 3436 July Number
- ZOJ Problem Set - 2736 Daffodil number
- ZOJ Problem Set - 3665 Yukari's Birthda
- ZOJ Problem Set - 1857
- ZOJ Problem Set - 3812 牡丹江网络赛D题 状态压缩 背包
- ZOJ Problem Set -1259 Rails