Problem Description
For  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(, )=,f(, )=, f(, )=. Now given  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 integer in set A, choose the smallest one.
 
Input
The first line of the input is an integer T ( < T ≤ ), indicating the number of test cases. The first line of each test case contains  positive integers m and n ( < m, n ≤ ), indicating the numbers of integers of the  sets A and B, respectively. Then follow (m + n) lines, each of which contains a non-negative integers no larger than . 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

 
Source
 
 
 
法一:求出b[i]、a[j]的二进制数后,再比较统计
 
 
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<cmath>
#include<stdlib.h>
#include<map>
using namespace std;
#define N 106
int n,m;
int a[N];
int b[N];
int s[];
int k1; void change(int x){
memset(s,,sizeof(s));
k1=; while(x){
s[k1++]=x%;
x/=;
} }
int s1[];
int k2;
void change1(int x){
memset(s1,,sizeof(s1));
k2=; while(x){
s1[k2++]=x%;
x/=;
} }
int main()
{
int t;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
for(int i=;i<n;i++){
scanf("%d",&a[i]);
}
for(int i=;i<m;i++){
scanf("%d",&b[i]);
}
sort(a,a+n);
for(int i=;i<m;i++){
change(b[i]);
int minn=;
int f=;
for(int j=;j<n;j++){
change1(a[j]);
int ans=;
int q=max(k1,k2);
for(int w=;w<q;w++){
if(s[w]!=s1[w]){
ans++;
}
}
if(ans<minn){
minn=ans;
f=j;
}
}
printf("%d\n",a[f]);
}
}
return ;
}

法二:先求b[i]^a[j],再一次性统计

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<set>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;
#define ll long long
#define N 50006
int a[];
int b[];
int n,m;
int s1[];
int solve(int x)
{
int k1=;
while(x)
{
s1[k1++]=x%;
x=x/;
}
int ans=;
for(int i=;i<k1;i++)
{
if(s1[i]==)
ans++;
}
return ans;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(int i=;i<n;i++)
{
scanf("%d",&a[i]);
}
for(int i=;i<m;i++)
{
scanf("%d",&b[i]);
int minn=;
int flag;
for(int j=;j<n;j++)
{
int tmp=solve(b[i]^a[j]);
//printf("---%d\n",b[i]^a[j]);
//printf("%d\n",tmp);
if(minn>tmp)
{
minn=tmp;
flag=a[j];
}
else if(minn==tmp)
{
if(flag>a[j])
{
flag=a[j];
}
}
}
printf("%d\n",flag);
}
}
return ;
}

hdu 3711 Binary Number(暴力 模拟)的更多相关文章

  1. [HDU] 3711 Binary Number [位运算]

    Binary Number Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tot ...

  2. HDU 3711 Binary Number

    Binary Number Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tot ...

  3. 杭州电 3711 Binary Number

    Binary Number Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) To ...

  4. HDU - 1711 A - Number Sequence(kmp

    HDU - 1711 A - Number Sequence   Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1 ...

  5. HDU 4041 Eliminate Witches! (模拟题 ACM ICPC 2011亚洲北京赛区网络赛)

    HDU 4041 Eliminate Witches! (模拟题 ACM ICPC 2011 亚洲北京赛区网络赛题目) Eliminate Witches! Time Limit: 2000/1000 ...

  6. POJ 1013 小水题 暴力模拟

    Counterfeit Dollar Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 35774   Accepted: 11 ...

  7. hihoCoder #1871 : Heshen's Account Book-字符串暴力模拟 自闭(getline()函数) (ACM-ICPC Asia Beijing Regional Contest 2018 Reproduction B) 2018 ICPC 北京区域赛现场赛B

    P2 : Heshen's Account Book Time Limit:1000ms Case Time Limit:1000ms Memory Limit:512MB Description H ...

  8. BNU 13024 . Fi Binary Number 数位dp/fibonacci数列

    B. Fi Binary Number     A Fi-binary number is a number that contains only 0 and 1. It does not conta ...

  9. hdu 5898 odd-even number 数位DP

    传送门:hdu 5898 odd-even number 思路:数位DP,套着数位DP的模板搞一发就可以了不过要注意前导0的处理,dp[pos][pre][status][ze] pos:当前处理的位 ...

随机推荐

  1. Android Studio使用技巧系列教程(二)

    尊重劳动成果,转载请注明出处:http://blog.csdn.net/growth58/article/details/46764575 关注新浪微博:@于卫国 邮箱:yuweiguocn@gmai ...

  2. js计算日期天数差-2013-9-26

    function daymath(sdate, edate) {             var startdate = sdate;             var enddate = edate; ...

  3. 阿里云安装docker

    选centos6.5输入操作系统  yum install docker-io docker -d 提示没有备用IP地址可以用来桥接卡 接下来的网卡中编辑eth0 DEVICE=eth0 ONBOOT ...

  4. Android怎么让一个service开机自动启动

    1.首先开机启动后系统会发出一个Standard Broadcast Action,名字叫android.intent.action.BOOT_COMPLETED,这个Action只会发出一次. 2. ...

  5. 光盘自动运行HTML页,Autorun文件写法

    1.把你的网页放在一个根目录下面,起名为index.html 2.在目录新建一个autorun.inf的文件,打开后编辑为以下内容: 代码如下: [autorun]icon=***.ico(加图标) ...

  6. UI基础视图----UILabel总结

    UILabel是UIKit框架中非常常用的视图类,是UIView的子类,是UIWindow,UIImageView等的兄弟类,因为继承自UIView,所以继承了UIView中的属性和方法,大部分都可以 ...

  7. C语言float型数据在内存中的储存方式

  8. mvc上传,下载,浏览文件功能(用uploadify插件)

    类 public class UpLoadFileController : Controller { // // GET: /UpLoadFile/ public ActionResult Index ...

  9. android run process

    http://www.jb51.net/article/32798.htm http://www.elecfans.com/tongxin/119/20120315263977.html 图 1 详细 ...

  10. 磁盘IO性能监控(Linux 和 Windows)

    磁盘IO性能监控(Linux 和 Windows) 作者:终南   <li.zhongnan@hotmail.com> 磁盘的IO性能是衡量计算机总体性能的一个重要指标.Linux提供了i ...