D. Points and Powers of Two
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).
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.
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.
6
3 5 4 7 10 12
3
7 3 5
5
-1 2 5 8 11
1
8
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个。
证明如下:
证明一:集合{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 成立,必有x=y。
反证法:假设x!=y (x<y)。(x>y也类似)
那么有x+u=y。->u=y-x
故2^x+2^y=2^x+2^x*2^u=2^x(1+2^u)。
显然1+2^u不为2^n (2的幂)。
故2^x+2^y!=2^t。
结论有悖,故x=y,证明一成立。
证明二:集合{a,b,c,d……},(大于4个) 存在任意一对相减为2的幂当且仅当差为2^n。
拿4个来举例子好吧。
易知:|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。
由证明一和(1) (2) 可得,x=y=u,
故|a-d|=2^x+2^y+2^u= 3*2^x != 2^t3 。
故证明二不成立,不存在集合{a,b,c,d……},(大于4个) 存在任意一对相减为2^n (2的幂)。
5个元素以上也是不成立的,为什么呢?你想啊,5个以上,那必然有要满足4个元素的条件。a,b,c,d,e,必然会出现|a-d|,所以,你懂的。
参考网上代码如下:
https://www.cnblogs.com/xingkongyihao/p/9124635.html
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5+10;
int a[N], n;
map<int,int> mp;
int main()
{
scanf("%d",&n);
for(int i = 0; i < n; i ++)
{
scanf("%d",&a[i]);
mp[a[i]] = 1;
}
sort(a,a+n);
for(int i = 0; i < n; i ++)
{
for(int j = 0; j < 32; j ++)
{
int x = 1 << j;
if(mp[a[i]+x] && mp[a[i]+2*x])
{
return 0*printf("3\n%d %d %d\n",a[i],a[i]+x,a[i]+x*2);
}
if(a[i]+x > a[n-1] || a[i]+2*x > a[n-1]) break;
}
}
for(int i = 0; i < n; i ++)
{
for(int j = 0; j < 32; j ++)
{
int x = 1 << j;
if(mp[a[i]+x])
{
return 0*printf("2\n%d %d\n",a[i],a[i]+x);
}
if(mp[a[i]+x*2])
{
return 0*printf("2\n%d %d\n",a[i],a[i]+2*x);
}
if(a[i]+x > a[n-1] || a[i]+2*x > a[n-1]) break;
}
}
printf("1\n%d\n",a[0]);
return 0;
}
我的标签:做个有情怀的程序员。