CodeForces 2018-6-1 div3 D. Points and Powers of Two

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
6
3 5 4 7 10 12
Output
Copy
3
7 3 5
Input
Copy
5
-1 2 5 8 11
Output
Copy
1
8
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个点,

扫描二维码关注公众号,回复: 1477924 查看本文章

不妨设最小的数 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<iostream>
#include<cstring>
#include<cstdio>
#include<map>
#include<set>
#include<cstdlib>
#include<stdio.h>
#include<cmath>
#include<string>
#include<algorithm>
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;
	}

猜你喜欢

转载自blog.csdn.net/tianweidadada/article/details/80550241