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. google jam 比赛题(设计有问题)

    题目: Problem Do you know how to read the phone numbers in English? Now let me tell you. For example, ...

  2. 关于popupwindow的两种实现方式

    http://104zz.iteye.com/blog/1685389 android PopupWindow实现从底部弹出或滑出选择菜单或窗口 本实例弹出窗口主要是继承PopupWindow类来实现 ...

  3. 【Xamarin For IOS 开发需要的安装文件】

    官网安装文件下载: http://download.xamarin.com/XamarinforMac/Mac/xamarin.mac-2.0.1.64.pkghttp://download.xama ...

  4. JAVA语言学校的危险性

    Java语言学校的危险性(译文) 作者: 阮一峰 日期: 2008年12月 7日 下面的文章是More Joel on Software一书的第8篇. 我觉得翻译难度很大,整整两个工作日,每天8小时以 ...

  5. Linux系统下C++开发工具-远程终端软件使用

    通过前面安装Linux系统介绍,紧接着进入搭建Linux开发环境的第二步,选择C++开发工具,以及必要的客户端软件.从而完整的搭建一个Linux下C++开发的环境,便于初学者在该环境下能够很好的学习. ...

  6. (Data structure)Implement Trie && Add and Search Word

    Implement Trie (Prefix Tree) Implement a trie with insert, search, and startsWith methods. Note:You ...

  7. HTTP请求的TCP瓶颈分析

    这篇文章基本是对<Web性能权威指南>第一章和第二章的读书笔记,另外加一些扩展内容,这本书确实赞,推荐 针对三次握手.流量控制(接收窗口).慢启动(cwnd,拥塞窗口).队首阻塞等方面看下 ...

  8. c语言指针与结构体

    #include <stdio.h> #include <stdlib.h> struct mydata { int num; ]; }; void main1() { /*i ...

  9. npm 和 bower的区别

    npm和bower在功能上有一定的重合,但不是互斥关系,可以在项目中同时运用.区别在于npm在设计之初就采用了的是嵌套的依赖关系树.一个普通的前端包的依赖树比较长,npm 会将开发环境一起下载下来,  ...

  10. hdu 5422 Rikka with Graph(简单题)

    Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, so he ...