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 nn 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 |xij−xik|=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 (1≤n≤2⋅1051≤n≤2⋅105) — the number of points.
The second line contains nn pairwise distinct integers x1,x2,…,xnx1,x2,…,xn (−109≤xi≤109−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 |7−3|=4=22|7−3|=4=22, |7−5|=2=21|7−5|=2=21 and |3−5|=2=21|3−5|=2=21. You can't find a subset having more points satisfying the required property.
选取最大子集集合,满足两两相减为2^n
集合元素最多为3个且满足x,x+2^j,x+2^(j+1)
证明如下:
证明一:集合{a,b,c},存在任意一对相减为2^n (2的幂)。
证:设|a-b|=2^x , |b-c|=2^y , |a-c|=2^x+2^y=2^t 。
由条件可知,要使2^x+2^y=2^t ,则
2^x*(1+2^(y-x))=2^t,分析只有y-x=0满足,即y=x;
y=x说明了两两之间差值一样,即x,x+2^j,x+2^(j+1)。
证明二:
假设集合有四个元素
集合{a,b,c,d……} 存在任意一对相减为2的幂当且仅当差为2^n。
易知:|a-b|=2^x , |b-c|=2^y , |c-d|=2^u。
|a-c|=2^x+2^y=2^t1 , …………(1)
|b-d|=2^y+2^u=2^t2 , ………… (2)
|a-d|=2^x+2^y+2^u=2^t3。
2^x(1+2^(y-x)+2^(u-x))=2^t3
无论怎么样,都不可能等于。
四个都不行了,五个。。。。。。更不行(因为五个里面肯定包含四个)
实现过程:直接枚举3个,2个,1个,一开始用vector保存的超时,改用set,删除了些重复的。
#include<bits/stdc++.h>
using namespace std;
long long v[200005];
int n;
set<long long > s;
int main()
{
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%lld",&v[i]);
s.insert(v[i]);
}
for(int i=1;i<=n;i++)
{
for(long long j=1;j<=1e10;j*=2)
{
if(s.find(v[i]+j)!=s.end()&&s.find(v[i]+2*j)!=s.end())
{
printf("3\n");
printf("%lld %lld %lld\n",v[i],v[i]+j,v[i]+2*j);
return 0;
}
}
}
for(int i=1;i<=n;i++)
{
for(long long j=1;j<=1e10;j*=2)
{
if(s.find(v[i]+j)!=s.end())
{
printf("2\n") ;
printf("%lld %lld\n",v[i],v[i]+j);
return 0;
}
}
}
printf("1\n") ;
printf("%lld\n",v[1]);
}