Problem Description
For  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(, )=,f(, )=, f(, )=. Now given  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 ( < T ≤ ), indicating the number of test cases. The first line of each test case contains  positive integers m and n ( < m, n ≤ ), indicating the numbers of integers of the  sets A and B, respectively. Then follow (m + n) lines, each of which contains a non-negative integers no larger than . 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

 
Sample Output

 
Source
 
 
 
法一:求出b[i]、a[j]的二进制数后,再比较统计
 
 
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<cmath>
#include<stdlib.h>
#include<map>
using namespace std;
#define N 106
int n,m;
int a[N];
int b[N];
int s[];
int k1; void change(int x){
memset(s,,sizeof(s));
k1=; while(x){
s[k1++]=x%;
x/=;
} }
int s1[];
int k2;
void change1(int x){
memset(s1,,sizeof(s1));
k2=; while(x){
s1[k2++]=x%;
x/=;
} }
int main()
{
int t;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
for(int i=;i<n;i++){
scanf("%d",&a[i]);
}
for(int i=;i<m;i++){
scanf("%d",&b[i]);
}
sort(a,a+n);
for(int i=;i<m;i++){
change(b[i]);
int minn=;
int f=;
for(int j=;j<n;j++){
change1(a[j]);
int ans=;
int q=max(k1,k2);
for(int w=;w<q;w++){
if(s[w]!=s1[w]){
ans++;
}
}
if(ans<minn){
minn=ans;
f=j;
}
}
printf("%d\n",a[f]);
}
}
return ;
}

法二:先求b[i]^a[j],再一次性统计

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<set>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;
#define ll long long
#define N 50006
int a[];
int b[];
int n,m;
int s1[];
int solve(int x)
{
int k1=;
while(x)
{
s1[k1++]=x%;
x=x/;
}
int ans=;
for(int i=;i<k1;i++)
{
if(s1[i]==)
ans++;
}
return ans;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(int i=;i<n;i++)
{
scanf("%d",&a[i]);
}
for(int i=;i<m;i++)
{
scanf("%d",&b[i]);
int minn=;
int flag;
for(int j=;j<n;j++)
{
int tmp=solve(b[i]^a[j]);
//printf("---%d\n",b[i]^a[j]);
//printf("%d\n",tmp);
if(minn>tmp)
{
minn=tmp;
flag=a[j];
}
else if(minn==tmp)
{
if(flag>a[j])
{
flag=a[j];
}
}
}
printf("%d\n",flag);
}
}
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

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

  3. 杭州电 3711 Binary Number

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

  4. HDU - 1711 A - Number Sequence(kmp

    HDU - 1711 A - Number Sequence   Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1 ...

  5. HDU 4041 Eliminate Witches! (模拟题 ACM ICPC 2011亚洲北京赛区网络赛)

    HDU 4041 Eliminate Witches! (模拟题 ACM ICPC 2011 亚洲北京赛区网络赛题目) Eliminate Witches! Time Limit: 2000/1000 ...

  6. POJ 1013 小水题 暴力模拟

    Counterfeit Dollar Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 35774   Accepted: 11 ...

  7. hihoCoder #1871 : Heshen's Account Book-字符串暴力模拟 自闭(getline()函数) (ACM-ICPC Asia Beijing Regional Contest 2018 Reproduction B) 2018 ICPC 北京区域赛现场赛B

    P2 : Heshen's Account Book Time Limit:1000ms Case Time Limit:1000ms Memory Limit:512MB Description H ...

  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. hdu 5898 odd-even number 数位DP

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

随机推荐

  1. Cuts the cake_hdu_2134.java

    Cuts the cake Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) To ...

  2. JMeter脚本参数化和断言设置( CSV Data Set Config )

    用Badboy录制了Jmeter的脚本,用Jmeter打开后形成了原始的脚本.但是在实际应用中,为了增强脚本的多样性,就要使脚本参数化.这里我以登录为例,参数化用户账号与用户密码.  图1 :原始脚本 ...

  3. OC教程10-NSNumber具体

    NSNumber简单介绍 NSNumber是数字的对象形式,由于在OC的数组和字典中仅仅同意存放对象,所以我们有时候须要转化 我们普通的类型是   123 那么 NSNumber类型的是  @123, ...

  4. Test failed.尝试加载Oracle客户端库时引发BadImageFormatException

    CodeSmith6.5不像前几个版本,需要用户手动添加oracle驱动,内部已经集成了oracle的驱动. 网上遇到很多win7 64位机子使用CodeSmith连接oracle的时候出现错误如下:

  5. 获取sqlserver数据库中所有库、表、字段名的方法

    获取sqlserver数据库中所有库.表.字段名的方法 2009年03月12日 星期四 下午 12:51 1.获取所有数据库名: SELECT Name FROM Master..SysDatabas ...

  6. Web App 前端构建(纯净版)

    asp.net 母版页: <!DOCTYPE html> <html> <head> <meta charset="utf-8" name ...

  7. html的空格显示距离问题

    一.使用全角空格 全角空格被解释为汉字,所以不会被被解释为HTML分隔符,可以按照实际的空格数显示. 二.使用空格的替代符号 替代符号就是在需要显示空格的地方加入替代符号,这些符号会被浏览器解释为空格 ...

  8. DTO学习系列之AutoMapper(四)

    本篇目录: Mapping Inheritance-映射继承 Queryable Extensions (LINQ)-扩展查询表达式 Configuration-配置 Conditional Mapp ...

  9. 探索A@1db9742

    public class S { /**   * @param args   */ public static void main(String[] args) { System.out.printl ...

  10. hdu2488 dfs

    G - 深搜 基础 Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:65536KB     64bi ...