codeforces#1257 F. Make Them Similar ( 经典中间相遇问题 )
题目链接:
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 ( 经典中间相遇问题 )的更多相关文章
- Codeforces 959 F. Mahmoud and Ehab and yet another xor task
\(>Codeforces\space959 F. Mahmoud\ and\ Ehab\ and\ yet\ another\ xor\ task<\) 题目大意 : 给出一个长度为 \ ...
- Codeforces 835 F. Roads in the Kingdom
\(>Codeforces\space835 F. Roads in the Kingdom<\) 题目大意 : 给你一棵 \(n\) 个点构成的树基环树,你需要删掉一条环边,使其变成一颗 ...
- Codeforces 731 F. Video Cards(前缀和)
Codeforces 731 F. Video Cards 题目大意:给一组数,从中选一个数作lead,要求其他所有数减少为其倍数,再求和.问所求和的最大值. 思路:统计每个数字出现的个数,再做前缀和 ...
- Educational Codeforces Round 76 (Rated for Div. 2)F - Make Them Similar
题意: 给你n个数字(<230),求出一个数字使得所有数字^这个数字之后,二进制状态下的1的个数相同. 解析: 因为最大数字二进制数有30位,所以分为前15位和后15位,分别枚举0-1<& ...
- Codeforces 797 F Mice and Holes
http://codeforces.com/problemset/problem/797/F F. Mice and Holes time limit per test 1.5 ...
- 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 ...
- Codeforces 379 F. New Year Tree
\(>Codeforces \space 379 F. New Year Tree<\) 题目大意 : 有一棵有 \(4\) 个节点个树,有连边 \((1,2) (1,3) (1,4)\) ...
- Codeforces 538 F. A Heap of Heaps
\(>Codeforces \space 538 F. A Heap of Heaps<\) 题目大意 :给出 \(n\) 个点,编号为 \(1 - n\) ,每个点有点权,将这些点构建成 ...
- codeforces 825F F. String Compression dp+kmp找字符串的最小循环节
/** 题目:F. String Compression 链接:http://codeforces.com/problemset/problem/825/F 题意:压缩字符串后求最小长度. 思路: d ...
随机推荐
- Go context 介绍和使用
context 上下文管理 context 翻译过来就是上下文管理,主要作用有两个: 控制 goroutine 的超时 保存上下文数据 WithTimeout 通过下面的一个简单的 http 例子进行 ...
- jQuery的淡入和淡出简单介绍
在jQuery中的一些特效中,可以通过四个方法来实现元素的淡入淡出,这四个方法分别是:fadeIn().fadeOut().fadeToggle() 以及 fadeTo(),下面为分别为大家介绍各个方 ...
- 前端知识总结--ES6新特性
ECMAScript 6.0(以下简称 ES6)是 JavaScript 语言的下一代标准,已经在 2015 年 6 月正式发布了.它的目标,是使得 JavaScript 语言可以用来编写复杂的大型应 ...
- 理解JVM之类加载机制
类完整的生命周期包括加载,验证,准备,解析,初始化,使用,卸载,七个阶段.其中验证,准备,解析统称为连接,类的卸载在前面的关于垃圾回收的博文中已经介绍. 加载,验证,准备,初始化,卸载这五个阶段的顺序 ...
- [LeetCode] 392. 判断子序列 ☆(动态规划)
https://leetcode-cn.com/problems/is-subsequence/solution/java-dp-by-zxy0917-5/ 描述 给定字符串 s 和 t ,判断 s ...
- HTML的BODY内标签介绍
一.基本标签 <body> <b>加粗</b> <i>斜体</i> <u>下划线</u> <s>删除线& ...
- C++——引用 reference
转载请注明出处:https://www.cnblogs.com/kelamoyujuzhen/p/9427555.html pass by value vs. pass by reference (t ...
- 【HCIA Gauss】学习汇总-数据库管理(数据库设计 范式 索引 分区)-7
zsql user/pasword@ip:port -c "show databases" # 展示一条sql语句 spool file_path 指定输出文件 可以为相对路径 s ...
- jmeter+jenkins 配置过程(很详细)
一.安装jmeter 第一步 安装JDK,配置JDK路径.注:jdk下载地址,推荐使用jdk1.8版本 http://www.oracle.com/technetwork/java/javase/d ...
- LoggerFactory is not a Logback LoggerContext but Logback is on the classpath. Either remove Logback or the competing implementation
2019-06-26 22:19:57.408642475 java.lang.IllegalArgumentException: LoggerFactory is not a Logback Log ...