题目链接:

http://codeforces.com/contest/1257/problem/F

题意:

给出$n$个30位整数

找到一个数,让它与这$n$个数分别异或,得到的$n$个数二进制1的个数相同

数据范围:

$1\leq n \leq 100$

分析:

CF官方题解称这是中间相遇技巧

枚举答案的低15位,这时有$2^{15}$种情况

与给出的$n$个数的低15位去异或,得到1的数量定义为$low[i]$

把$(low[2]-low[1],low[3]-low[1],.....low[n]-low[1])$这个vector放入set

再枚举答案的高15位,得到$high[i]$数组

在set中寻找$(high[1]-high[2],high[1]-high[3],.....high[1]-high[1])$

AC代码:

#include<bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define pcc pair<char,char>
using namespace std;
const int maxn=100+7;
const int maxm=(1<<15)+7;
struct Node{
int ans;
vector<int>ve;
bool operator<(const Node &a)const{
return ve<a.ve;
}
}node;
set<Node>se;
int l[maxn],h[maxn];
int getlen[maxm],n;
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++){
int x;
scanf("%d",&x);
l[i]=(x&((1<<15)-1));
h[i]=x>>15;
}
for(int i=0;i<maxm;i++){
int v=i;
while(v){
if(v&1)getlen[i]++;
v/=2;
}
}
int len=(1<<15);
for(int i=0;i<len;i++){
node.ans=i;
node.ve.clear();
int v=getlen[i^l[1]];
for(int j=2;j<=n;j++)
node.ve.push_back(v-getlen[i^l[j]]);
se.insert(node);
}
for(int i=0;i<len;i++){
node.ans=i;
node.ve.clear();
int v=getlen[i^h[1]];
for(int j=2;j<=n;j++)
node.ve.push_back(getlen[i^h[j]]-v);
if(se.find(node)!=se.end()){
Node now=*se.find(node);
printf("%d\n",(i<<15)+now.ans);
//if((i<<15)+now.ans==1073709057)return 0;
return 0;
}
}
printf("-1\n");
return 0;
}

  

codeforces#1257 F. Make Them Similar ( 经典中间相遇问题 )的更多相关文章

  1. Codeforces 959 F. Mahmoud and Ehab and yet another xor task

    \(>Codeforces\space959 F. Mahmoud\ and\ Ehab\ and\ yet\ another\ xor\ task<\) 题目大意 : 给出一个长度为 \ ...

  2. Codeforces 835 F. Roads in the Kingdom

    \(>Codeforces\space835 F. Roads in the Kingdom<\) 题目大意 : 给你一棵 \(n\) 个点构成的树基环树,你需要删掉一条环边,使其变成一颗 ...

  3. Codeforces 731 F. Video Cards(前缀和)

    Codeforces 731 F. Video Cards 题目大意:给一组数,从中选一个数作lead,要求其他所有数减少为其倍数,再求和.问所求和的最大值. 思路:统计每个数字出现的个数,再做前缀和 ...

  4. Educational Codeforces Round 76 (Rated for Div. 2)F - Make Them Similar

    题意: 给你n个数字(<230),求出一个数字使得所有数字^这个数字之后,二进制状态下的1的个数相同. 解析: 因为最大数字二进制数有30位,所以分为前15位和后15位,分别枚举0-1<& ...

  5. Codeforces 797 F Mice and Holes

    http://codeforces.com/problemset/problem/797/F F. Mice and Holes time limit per test             1.5 ...

  6. Codeforces 622 F. The Sum of the k-th Powers

    \(>Codeforces \space 622\ F. The\ Sum\ of\ the\ k-th\ Powers<\) 题目大意 : 给出 \(n, k\),求 \(\sum_{i ...

  7. Codeforces 379 F. New Year Tree

    \(>Codeforces \space 379 F. New Year Tree<\) 题目大意 : 有一棵有 \(4\) 个节点个树,有连边 \((1,2) (1,3) (1,4)\) ...

  8. Codeforces 538 F. A Heap of Heaps

    \(>Codeforces \space 538 F. A Heap of Heaps<\) 题目大意 :给出 \(n\) 个点,编号为 \(1 - n\) ,每个点有点权,将这些点构建成 ...

  9. codeforces 825F F. String Compression dp+kmp找字符串的最小循环节

    /** 题目:F. String Compression 链接:http://codeforces.com/problemset/problem/825/F 题意:压缩字符串后求最小长度. 思路: d ...

随机推荐

  1. linux环境,hidraw设备自动加载时默认权限的设置方法

    在linux系统中,hidraw设备会自动加载并设置默认权限,但系统的默认只允许root用户访问,普通用户是不允许读写. 设置的方法是修改udev的配置,配置路径是/etc/udev/rules.d/ ...

  2. POJ1322Chocolate--概论DP

    题目在这里 每次从包装中取出一块巧克力并放在桌子上.如果桌子上有两个相同颜色的巧克力,则将这两个丢掉.如果包中有C种颜色的巧克力(颜色均匀分布),从包装中取出N个巧克力后,桌子上确实有M个巧克力的概率 ...

  3. Java 之 OutputStreamReader类

    OutputStreamReader类 1.概述 转换流 java.io.OutputStreamReader ,是Writer的子类,是从字符流到字节流的桥梁. 它使用指定的字符集将字符编码为字节. ...

  4. 部署vue项目到阿里云服务器(Ubuntu16.04 64位)

    上传文件 1.通过Xftp将vue项目文件上传至云服务器:由于node_modules这个依赖包体积较大,上传较慢,上传时跳过,在云服务器上重新进行npm install安装依赖包即可: 2.也可通过 ...

  5. 织梦dede:channelartlist调用排除指定typeid栏目

    在使用 dede:channelartlist 自动调用栏目内容时,经常会遇到某些栏目不需要调用,比如“关于我们”.“联系地址”等无持续更新的栏目.要想在 dede:channelartlist 调用 ...

  6. ORA-03113:通信通道的文件结尾 解决办法

    登录Oracle时出现错误:“ORA-03113:通信通道的文件结尾” 错误排查方法 Oracle出现错误,查看trace日志寻找问题根源:D:\oracle\diag\rdbms\orcl\orcl ...

  7. jquery中的ajax方法(备忘)

    参考:https://www.cnblogs.com/tylerdonet/p/3520862.html w3school:http://www.w3school.com.cn/jquery/ajax ...

  8. CUDA中确定你显卡的thread和block数

    CUDA中确定你显卡的thread和block数 在进行并行计算时, 你的显卡所支持创建的thread数与block数是有限制的, 因此, 需要自己提前确定够用, 再进行计算, 否则, 你需要改进你的 ...

  9. Log parser工具使用

    Windows日志存放于目录“C:\Windows\System32\winevt\Logs”中, 在目录中可以找到“System”.“Setup”.“Application”.“Security” ...

  10. 【loj-1055-Going Together-三个棋子推箱子走到目的地--讲预判的bfs】

    light oj 1055-Going Together 题目大致意思: 简单的三个棋子,每次可以下达一个命令,robots全部按照指令进行前进:若下一步不为空地则停留在原地. 特殊考虑: 1.例如A ...