Codeforces Round #486 (Div. 3) D. Points and Powers of Two

xiaoxiao2021-02-28  6

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 xijxijxikxik 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).


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.


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.


题解:设a,b,c在这个数组里b-a=2^x, c-b=2^y ,所有c-a=2^x+2^y,那么x=y,当存在d也在这个数组里d-c = 2^z;,所以d-a = 2^x+2^y+2^z不可能是2的n次,所以数组最多只能有三个,知道这个后,遍历一下所以的元素就好了,每个元素有1<<0 到1<<32种


#include <bits/stdc++.h> using namespace std; set<long long> s; set<long long>::iterator iter; vector<long long> v; int n; int main() { cin>>n; for(int i=0;i<n;i++) { long long t; cin>>t; s.insert(t); } for(iter=s.begin();iter!=s.end();iter++) { for(int i=0;i<33;i++) { //cout<<*iter-1<<i<<" "<<*iter-(1<<i)<<endl; int flag1 = s.count(*iter-(1<<i)); int flag2 = s.count(*iter+(1<<i)); if(flag1&&flag2) { cout<<3<<endl; cout<<(*iter-(1<<i))<<" "<<*iter<<" "<<(*iter+(1<<i))<<endl; return 0; } else if(flag1) { if(!v.size()) { v.push_back(*iter); v.push_back(*iter-(1<<i)); } } else if(flag2) { if(!v.size()) { v.push_back(*iter); v.push_back(*iter+(1<<i)); } } } } if(v.size()) { cout<<2<<endl; cout<<v[0]<<" "<<v[1]<<endl; } else cout<<1<<endl<<*s.begin()<<endl; }