Binary Number

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

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.
 
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
 
 
 
解析:本题考查位运算。关键在于求x和y的二进制不同位的个数,即x^y的二进制中1的个数。可以用如下方法快速求出:
int cal(int x)
{
int cnt = ;
while(x){
++cnt;
x &= (x-);
}
return cnt;
}

这段代码的核心在于每执行一次x &= (x-1),会将x的二进制中最低位的1变为0。
运用这个原理,我们还可以得到判断一个正整数x是否为2的n(n>=0)次幂的方法:如果(x&(x-1)) == 0,则为true;否则为false。

常用的位运算还有x&1、x&(-x)等。
x&1:x为奇数则结果为1,x为偶数则结果为0。
x&(-x):取出x的二进制中低位起,第一个1所在位置代表的数(若x的二进制中不存在1,即x为0,结果为0)。
 
 
 
 #include <cstdio>
#include <algorithm>
using namespace std; int T;
int m,n;
int A[],b; int cal(int x)
{
int cnt = ;
while(x){
++cnt;
x &= (x-);
}
return cnt;
} int main()
{
scanf("%d",&T);
while(T--){
scanf("%d%d",&m,&n);
for(int i = ; i<m; ++i)
scanf("%d",&A[i]);
sort(A,A+m);
for(int i = ; i<n; ++i){
scanf("%d",&b);
int min_cnt = 0x7fffffff;
int ans;
for(int j = ; j<m; ++j){
int cnt = cal(b^A[j]);
if(cnt<min_cnt){
min_cnt = cnt;
ans = A[j];
}
}
printf("%d\n",ans);
}
}
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. 杭州电 3711 Binary Number

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

  4. hdu 5898 odd-even number 数位DP

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

  5. hdu 2665 Kth number

    划分树 /* HDU 2665 Kth number 划分树 */ #include<stdio.h> #include<iostream> #include<strin ...

  6. hdu 4670 Cube number on a tree(点分治)

    Cube number on a tree Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/ ...

  7. 主席树[可持久化线段树](hdu 2665 Kth number、SP 10628 Count on a tree、ZOJ 2112 Dynamic Rankings、codeforces 813E Army Creation、codeforces960F:Pathwalks )

    在今天三黑(恶意评分刷上去的那种)两紫的智推中,突然出现了P3834 [模板]可持久化线段树 1(主席树)就突然有了不详的预感2333 果然...然后我gg了!被大佬虐了! hdu 2665 Kth ...

  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. 【Leetcode_easy】693. Binary Number with Alternating Bits

    problem 693. Binary Number with Alternating Bits solution1: class Solution { public: bool hasAlterna ...

随机推荐

  1. Hibernate从入门到精通(七)多对一单向关联映射

    上次的博文Hibernate从入门到精通(六)一对一双向关联映射中我们介绍了一下一对一双向关联映射,本次博文我们讲解一下多对一关联映射 多对一单向关联映射 多对一关联映射与一对一关联映射类似,只是在多 ...

  2. Android模拟神器Genymotion eclipse插件安装问题出解决

    我之前一直是打开eclipse之前直接运行Genymotion模拟器就可以连接到adb了,非常方便,但最近突然想来装个eclipse的Genymotion插件玩玩,安装时居然出错了,于是不折腾好心里不 ...

  3. 【BZOJ 1015】[JSOI2008]星球大战starwar

    Description 很久以前,在一个遥远的星系,一个黑暗的帝国靠着它的超级武器统治者整个星系.某一天,凭着一个偶然的机遇,一支反抗军摧毁了帝国的超级武器,并攻下了星系中几乎所有的星球.这些星球通过 ...

  4. @Repository、@Service、@Controller 和 @Component(转)

    鸣谢:http://blog.csdn.net/ye1992/article/details/19971467 @Repository.@Service.@Controller 和 @Componen ...

  5. 团体程序设计天梯赛-练习集L1-001. Hello World

    L1-001. Hello World 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 这道超级简单的题目没有任何输入. 你只需要在一行中输 ...

  6. uva 348

    dp还是比较容易  关键是输出路径 .... #include <cstdlib> #include <cstdio> #include <cstring> #de ...

  7. xcode 开发ios兼容性问题的上下黑边 和 coco2d-x 游戏分辨率适配 ResolutionPolicy::FIXED_WIDTH 都会引起上下黑边问题!!!

    1:Xcode6在iPhone5+iOS7模拟器上编译,上下有黑边问题 问题描述: Xcode6环境下,对iPhone5或iPhone5s模拟器,在iOS7或iOS7.1下运行,屏幕上下有黑边.在iO ...

  8. google maps v3 添加自定义图标(marker,overlay)

    google搜了很久都没找到符合v3版本的google maps自定义图标,可以让图标使用自己的html,都是V2版本的,依靠重写google api属性来完成. 然后就找了个jquery下的goog ...

  9. javascript高级程序设计读书笔记

    第2章  在html中使用javascript 一般都会把js引用文件放在</body>前面,而不是放在<head>里, 目的是最后读取js文件以提高网页载入速度. 引用js文 ...

  10. SQL数据库还原时备份集中的数据库备份与现有的数据库不同的解决办法

    SQL Server 2005数据库还原出错错误具体信息为:备份集中的数据库备份与现有的A数据库不同 具体操作如下:第一次:新建了数据库A,数据库文件放在E:\DB\A目录下,选中该数据库右键-任务- ...