博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CodeForces 2018-6-1 div3 D. Points and Powers of Two
阅读量:4217 次
发布时间:2019-05-26

本文共 3196 字,大约阅读时间需要 10 分钟。

time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

There are n distinct points on a coordinate line, the coordinate of ii-th point equals to xixi. Choose a subset of the given set of points such that the distance between each pair of points in a subset is an integral power of two. It is necessary to consider each pair of points, not only adjacent. Note that any subset containing one element satisfies the condition above. Among all these subsets, choose a subset with maximum possible size.

In other words, you have to choose the maximum possible number of points xi1,xi2,,ximxi1,xi2,…,xim such that for each pair xijxij, xikxik it is true that |xijxik|=2d|xij−xik|=2d where dd is some non-negative integer number (not necessarily the same for each pair of points).

Input

The first line contains one integer nn (1n21051≤n≤2⋅105) — the number of points.

The second line contains nn pairwise distinct integers x1,x2,,xnx1,x2,…,xn (109xi109−109≤xi≤109) — the coordinates of points.

Output

In the first line print mm — the maximum possible number of points in a subset that satisfies the conditions described above.

In the second line print mm integers — the coordinates of points in the subset you have chosen.

If there are multiple answers, print any of them.

Examples
Input
Copy
63 5 4 7 10 12
Output
Copy
37 3 5
Input
Copy
5-1 2 5 8 11
Output
Copy
18
Note

In the first example the answer is [7,3,5][7,3,5]. Note, that |73|=4=22|7−3|=4=22, |75|=2=21|7−5|=2=21 and |35|=2=21|3−5|=2=21. You can't find a subset having more points satisfying the required property.

这题主要难点在于数学推理,得出 最多只有3个点,

不妨设最小的数 x , x + 2^d1, x+2^d1+2^d2  (d1 <= d2)

那么 x + 2^d3 =  x+2^d1+2^d2   ->  2^d3 = 2^d1 +2^d2 提取 2^d1 可得 2^d1*(2^(d2-d1)+1) = 2^d3

可得d2-d1必须为0 即d1 == d2 所以 d2 = d1 = d3 - 1

这是证明了可以满足3个点的,可以类似的证明 当有四个点的时候 2^d1( 2^(d2-d1) +2^(d3-d1)  +1 ) = 2^d4

也就是 需要 内部的 2^(d2-d1) +2^(d3-d1)  +1 为2的幂次又 (假设d1 <= d2 <= d3) 则 2^(d2-d1) +2^(d3-d1)  +1式始终为奇数,不是2的幂次,也就是上式永远不会满足,故最多只可能是三个点满足题意。

实现:

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;typedef long long LL;const int N = 200000+5;int a[N];int f[40];int pow2(int a,int b){ int ans = 1; while(b&1 != 1){ b >>= 1; a *= a; } while(b){ if(b&1 == 1){ ans *= a; } b >>= 1; a *= a; } return ans;}bool bin_ser(int a[],int key, int n){ int l = 1, r = n; while(l <= r){ int m = l + (r-l)/2; if(a[m] < key) l = m+1; else if(a[m] > key) r = m-1; else return true; } return false;}int main() { int n; f[0] = 1; for(int i = 1; i < 31; ++i){ f[i] = f[i-1]*2; } scanf("%d",&n); for(int i = 1; i <= n; ++i){ scanf("%d",&a[i]); } bool flag = false; sort(a+1,a+n+1); if(!flag) for(int i = 1; i <= n;++i){ for(int j = 0; j < 30; ++j){ if(bin_ser(a,a[i]+f[j],n) && bin_ser(a,a[i]+f[j+1],n)){ flag = true; printf("3\n%d %d %d\n",a[i],a[i]+f[j],a[i]+f[j+1]); break; } } if(flag) break; } if(!flag) for(int i = 1; i <= n;++i){ for(int j = 0; j < 31; ++j){ if(bin_ser(a,a[i]+f[j],n)){ flag = true; printf("2\n%d %d\n",a[i],a[i]+f[j]); break; } } if(flag) break; } if(!flag) printf("1\n%d\n",a[1]); return 0; }

转载地址:http://sgimi.baihongyu.com/

你可能感兴趣的文章
【一天一道LeetCode】#30. Substring with Concatenation of All Words
查看>>
【一天一道LeetCode】#60. Permutation Sequence.
查看>>
【一天一道LeetCode】#118. Pascal's Triangle
查看>>
JAVA实现文件树
查看>>
ebay api GetMyMessages 函数
查看>>
手动12 - 安装php加速器 Zend OPcache
查看>>
set theme -yii2
查看>>
yii2 - controller
查看>>
yii2 - 增加actions
查看>>
php图像处理函数大全(缩放、剪裁、缩放、翻转、旋转、透明、锐化的实例总结)
查看>>
magento url中 uenc 一坨编码 base64
查看>>
强大的jQuery焦点图无缝滚动走马灯特效插件cxScroll
查看>>
Yii2.0 数据库查询
查看>>
yii2 db 操作
查看>>
mongodb group 有条件的过滤组合个数。
查看>>
关于mongodb的 数组分组 array group
查看>>
MongoDB新的数据统计框架介绍
查看>>
mongodb 增加全文检索索引
查看>>
QC数据库表结构
查看>>
测试工具厂商的编程语言什么时候“退休”?
查看>>