HDU 3711 Binary Number
Binary Number
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1879 Accepted Submission(s): 1133
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.
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.
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。
#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的更多相关文章
- [HDU] 3711 Binary Number [位运算]
Binary Number Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tot ...
- hdu 3711 Binary Number(暴力 模拟)
Problem Description For non-negative integers x and y, f(x, y) , )=,f(, )=, f(, )=. Now given sets o ...
- 杭州电 3711 Binary Number
Binary Number Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) To ...
- hdu 5898 odd-even number 数位DP
传送门:hdu 5898 odd-even number 思路:数位DP,套着数位DP的模板搞一发就可以了不过要注意前导0的处理,dp[pos][pre][status][ze] pos:当前处理的位 ...
- hdu 2665 Kth number
划分树 /* HDU 2665 Kth number 划分树 */ #include<stdio.h> #include<iostream> #include<strin ...
- 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/ ...
- 主席树[可持久化线段树](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 ...
- 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 ...
- 【Leetcode_easy】693. Binary Number with Alternating Bits
problem 693. Binary Number with Alternating Bits solution1: class Solution { public: bool hasAlterna ...
随机推荐
- Hibernate从入门到精通(七)多对一单向关联映射
上次的博文Hibernate从入门到精通(六)一对一双向关联映射中我们介绍了一下一对一双向关联映射,本次博文我们讲解一下多对一关联映射 多对一单向关联映射 多对一关联映射与一对一关联映射类似,只是在多 ...
- Android模拟神器Genymotion eclipse插件安装问题出解决
我之前一直是打开eclipse之前直接运行Genymotion模拟器就可以连接到adb了,非常方便,但最近突然想来装个eclipse的Genymotion插件玩玩,安装时居然出错了,于是不折腾好心里不 ...
- 【BZOJ 1015】[JSOI2008]星球大战starwar
Description 很久以前,在一个遥远的星系,一个黑暗的帝国靠着它的超级武器统治者整个星系.某一天,凭着一个偶然的机遇,一支反抗军摧毁了帝国的超级武器,并攻下了星系中几乎所有的星球.这些星球通过 ...
- @Repository、@Service、@Controller 和 @Component(转)
鸣谢:http://blog.csdn.net/ye1992/article/details/19971467 @Repository.@Service.@Controller 和 @Componen ...
- 团体程序设计天梯赛-练习集L1-001. Hello World
L1-001. Hello World 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 这道超级简单的题目没有任何输入. 你只需要在一行中输 ...
- uva 348
dp还是比较容易 关键是输出路径 .... #include <cstdlib> #include <cstdio> #include <cstring> #de ...
- xcode 开发ios兼容性问题的上下黑边 和 coco2d-x 游戏分辨率适配 ResolutionPolicy::FIXED_WIDTH 都会引起上下黑边问题!!!
1:Xcode6在iPhone5+iOS7模拟器上编译,上下有黑边问题 问题描述: Xcode6环境下,对iPhone5或iPhone5s模拟器,在iOS7或iOS7.1下运行,屏幕上下有黑边.在iO ...
- google maps v3 添加自定义图标(marker,overlay)
google搜了很久都没找到符合v3版本的google maps自定义图标,可以让图标使用自己的html,都是V2版本的,依靠重写google api属性来完成. 然后就找了个jquery下的goog ...
- javascript高级程序设计读书笔记
第2章 在html中使用javascript 一般都会把js引用文件放在</body>前面,而不是放在<head>里, 目的是最后读取js文件以提高网页载入速度. 引用js文 ...
- SQL数据库还原时备份集中的数据库备份与现有的数据库不同的解决办法
SQL Server 2005数据库还原出错错误具体信息为:备份集中的数据库备份与现有的A数据库不同 具体操作如下:第一次:新建了数据库A,数据库文件放在E:\DB\A目录下,选中该数据库右键-任务- ...