HDU 3711Binary Number
题目大意
§
f(
a,b
)
是非负整数
a
和
b
二进制时不同的位数(
hamming
距离)
§
给两个集合
A
,
B
,
A
和
B
的大小不超过
100
§
对于
B
中每个数
b
,求
A
中
f(
a,b
)
最小的数。
思路
¡
直接暴力
¡
对于
B
中的每个值,暴力枚举
A
中所有的值,记录下
f(
a,b
)
最小的那个。
代码
#include <functional>
#include <algorithm>
#include <stdexcept>
#include <iostream>
#include <sstream>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <cstdlib>
#include <cstring>
#include <utility>
#include <cctype>
#include <vector>
#include <string>
#include <bitset>
#include <cmath>
#include <queue>
#include <stack>
#include <ctime>
#include <list>
#include <map>
#include <set>
#include <cstdio>
#include <deque>
using namespace std;
#define PB push_back
#define MP make_pair
#define FU(i,n) for(i=0;i<n;i++)
#define FD(i,n) for(i=n-1;i>=0;i--)
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<double> VD;
typedef pair<int,int> PII;
typedef long long LL;
typedef unsigned long long ULL;
int cnt1(int x){
int c = 0;
for(;x;x>>=1) if(x&1) c++;
return c;
}
int main(){
int a[100],b,i,j,n,m,k,min,t,ca;
cin >> ca;
while(ca--){
cin >> n >> m;
FU(i,n) cin>>a[i];
FU(i,m){
cin>>b;
min = cnt1(b^a[0]);
k = 0;
for(j=1;j<n;j++){
t = cnt1(b^a[j]);
if(t < min || t == min && a[j] < a[k]) min = t, k = j;
}
cout << a[k] << endl;
}
}
return 0;
}