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. There is a legend about a bird

          There is a legend about a bird which sings just once in its life, more sweetly than any other ...

  2. ITaCS Change Password web part

    http://changepassword.codeplex.com/ A webpart is used to change your sharepoint AD password.

  3. Hadoop1.0.3集成eclipse开发

    本文来自:http://www.ilablog.org/%E7%BC%96%E8%AF%91hadoop-eclipse%E6%8F%92%E4%BB%B6/ 本人由于工作原因目前没有亲自尝试,那位尝 ...

  4. TWaver初学实战——如何在TWaver属性表中添加日历控件?

    在日期输入框中添加日历控件,是一种非常流行和实用的做法.临渊羡鱼不如退而写代码,今天就看看在TWaver中是如何实现的.   资源准备   TWaver的在线使用文档中,就有TWaver Proper ...

  5. docker 通过commit方法创建镜像(Tomcat+Java+Scala)

    前一阵试了试写Dockerfile创建docker image,但有时全靠Dockerfile写实在有些难度,退而求其次试一试使用commit来创建镜像: 想了想干脆创建一个Java+Scala+To ...

  6. VMware linux与windows文件共享

    将要共享的文件做成一个iso文件,然后打开VMware

  7. c++ string c_str() 和data()区别

    看下面的英文解释: const char* c_str ( ) const;Get C string equivalentGenerates a null-terminated sequence of ...

  8. module_init宏解析 linux驱动的入口函数module_init的加载和释放

    linux驱动的入口函数module_init的加载和释放 http://blog.csdn.net/zhandoushi1982/article/details/4927579 void free_ ...

  9. IOS - DatePicker的使用

    UIDatePicker *picker = [[UIDatePicker alloc] init]; picker.datePickerMode = UIDatePickerModeDate; pi ...

  10. MyBatis的动态SQL操作--查询

    查询条件不确定,需要根据情况产生SQL语法,这种情况叫动态SQL,即根据不同的情况生成不同的sql语句. 模拟一个场景,在做多条件搜索的时候,