Binary Number

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1475    Accepted Submission(s): 933

Problem Description
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 integer 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.
 

题解:定义函数f(x,y)表示非负整数x,y二进制对应位上不同数字个数。求在集合B中所有整数bi在集合A中找一个对应的数ai,使得f(ai,bi)最小,如果f(ai,bi)相等,使ai尽量小。

因为0 < m, n ≤ 100,所以直接O(nm)暴力扫一遍,直接ci=ai xor bi然后统计二进制ci上1的个数,求ci末尾是否为1直接判断ci是否为奇数即可,然后ci>>=1,右移一位。

代码:

 #include<cstdio>
#include<algorithm> const int INF=1e9+;
using namespace std; int main()
{
int T,i,j,p,a[],b[],minn,minx,num,m,n; scanf("%d",&T);
while(T--) {
scanf("%d%d",&m,&n);
for(int i=;i<m;i++) {
scanf("%d",&a[i]);
}
for(int i=;i<n;i++) {
scanf("%d",&b[i]);
} for(i=;i<n;i++)
{
minn=INF;minx=INF;
for(j=;j<m;j++) {
num=;
p=a[j] xor b[i];
while(p>) {
if(p%!=) num++;
p>>=;
}
if(num<minn) { minx=a[j];minn=num;}
else if(num==minn && a[j]<minx) minx=a[j];
} printf("%d\n",minx);
}
} 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(暴力 模拟)

    Problem Description For non-negative integers x and y, f(x, y) , )=,f(, )=, f(, )=. Now given sets o ...

  3. uestc 1709 Binary Operations 位运算的灵活运用

    Binary Operations Time Limit: 2000 ms Memory Limit: 65535 kB Solved: 56 Tried: 674   Description   B ...

  4. 136.137.260. Single Number && 位运算

    136. Single Number 意思就是给你一堆数,每个数都出现了两次,只有一个数只出现了一次,找出这个数 位运算(和c艹一样) &:按位与 |:按位或 ^:异或(一样为0,不一样为1) ...

  5. 杭州电 3711 Binary Number

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

  6. Same binary weight (位运算)

    题目描述 The binary weight of a positive  integer is the number of 1's in its binary representation.for ...

  7. hdu 1882 Strange Billboard(位运算+枚举)

    http://acm.hdu.edu.cn/showproblem.php?pid=1882 感觉非常不错的一道题. 给一个n*m(1<=n,m<=16)的矩阵,每一个格子都有黑白两面,当 ...

  8. hdu 5023 线段树+位运算

    主要考线段树的区间修改和区间查询,这里有一个问题就是这么把一个区间的多种颜色上传给父亲甚至祖先节点,在这里题目告诉我们最多30颜色,那么我们可以把这30中颜色用二进制储存和传给祖先节点,二进制的每一位 ...

  9. HDU 1882 Strange Billboard(位运算)

    题目链接 题意 : 给你一个矩阵,有黑有白,翻转一个块可以让上下左右都翻转过来,问最少翻转多少次能让矩阵变为全白. 思路 : 我们从第一行开始枚举要翻转的状态,最多可以枚举到2的16次方,因为你只要第 ...

随机推荐

  1. Abstract Factory模式的几个要点

    1.如果没有应对“多系列对象构建”的需求变化,则没有必要使用Abstract Factory模式.这时候使用简单的静态工厂完全可以.2.“系列对象”指的是这些对象之间有相互依赖.或作用的关系3.Abs ...

  2. 【剑指offer】面试题44:扑克牌的顺子

    题目: LL今天心情特别好,因为他去买了一副扑克牌,发现里面居然有2个大王,2个小王(一副牌原本是54张^_^)...他随机从中抽出了5张牌,想测测自己的手气,看看能不能抽到顺子,如果抽到的话,他决定 ...

  3. 潘石屹的SOHO小报猝死

    东莞时报多媒体数字报刊平台 潘石屹的SOHO小报猝死

  4. 好多邮箱的SMTP设置

    http://731771490.diandian.com/post/2011-04-20/19576550

  5. java.lang.NoClassDefFoundError 异常

    在项目实施过程中,当访问某一个功能时,出现异常为  java.lang.NoClassDefFoundError  com/xxx/yyy/Zzzz > ,检查发现这个类实际已经存在于应用服务器 ...

  6. Javascript&Jquery获取浏览器和屏幕各种高度宽度方法总结及运用

    <js篇> Javascript获取浏览器和屏幕各种高度宽度方法总结 document.body.clientWidth       //网页可见区域宽(body) document.bo ...

  7. Android软键盘弹出时布局问题

    最近项目需要做一个类似聊天室的模块,基于Socket实现的,这部分稍后一段时间再做总结,功能上的相关点都实现了小例子也做出来了,最后发现一个比较腻歪的问题就是软键盘弹出时总是会把标题“挤出”屏幕,(无 ...

  8. 由mysql数据库基础上的php程序实现单词的查询、删除、更改和查询

    我做了一个php程序,将表单数据添加到数据库,借用mysql扩展库函数实现对mysql数据库的操作,能够实现添加单词.删除单词.更新和查询单词.运行环境是普通的mysql数据库和php.Apache服 ...

  9. 卸载rpm包提示:error: specifies multiple packages

    –allmatches  Remove all versions of the package which match PACKAGE_NAME. Normally an error is issue ...

  10. scala中的view bound与context bound

    1.scala中的<%意识是“view bounds”(视界) ,它比<:的使用范围更广,还能进行隐式转换,是一种语法糖. 下面的两种写法是等效的,在编译之后完全一样. object Te ...