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 ...
随机推荐
- Python之(matplotlib、numpy、pandas)数据分析
一.Matplotlib 是一个 Python 的 2D绘图库,它以各种硬拷贝格式和跨平台的交互式环境生成出版质量级别的图形. 它主要用来回事图形,用来展现一些数据,更加直观的展示,让你第一眼就只要数 ...
- OpenCvSharp 图像旋转
/// <summary> /// 图像旋转 /// </summary> private Mat MatRotate(Mat src, float angle) { Mat ...
- .NetCore/ .NetFramework 机制
1.每来一个请求,会启动一个线程. 可以通过下面代码打印出来. 这个线程是主线程,如果用异步,会等待异步线程执行完毕才会返回. 这有个现象,用stmp 发邮件的时候,即使用异步,也会比较卡(选用的邮件 ...
- Asp.netCore 是用的Socket 吗?
Asp.netCore 是用的Socket 的krestrel 用的是Socket! public static IWebHostBuilder CreateDefaultBuilder(string ...
- iOS音频学习笔记二:iOS SDK中与音频有关的相关框架
上层: Media Player Framework: 包含MPMoviePlayerController.MPMoviePlayerViewController.MPMusicPla ...
- STM8 LED
时钟分频寄存器(CLK_CKDIVR) 举例 int main() { CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1); //高速内部时钟1分频 GPIO_ ...
- 类中变量私有化和调用:__x和getx/setx或者property
__xx:双前置下划线,子类不可继承属性.方法,父类私有. 详见:https://www.cnblogs.com/andy9468/p/8299448.html 例子1:隐藏数据:私有化后,用get和 ...
- 工程代码不编译src的java目录下的xml文件问题及解决
IDEA的maven项目中,默认源代码目录下(src/main/java目录)的xml等资源文件并不会在编译的时候一块打包进classes文件夹,而是直接舍弃掉.如果使用的是Eclipse,Eclip ...
- about微信小程序
1.<text>文本标签</text> ps:只有用text包裹的文字才能在微信里长按选中 2.单位 px变成rpx rpx是逻辑分变率 px=2rpx, rpx可以实 ...
- linux网络编程之socket编程(十一)
今天继续学习socket编程,这次主要是学习超时方法的封装,内容如下: ①.alarm[不常用,了解既可] 它的实现思路是这样的: 但是这种方案有一定的问题,因为闹钟可能会作为其它的用途,这时所设置的 ...