D. Points and Powers of Two ——codeforces

D. Points and Powers of Two

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



题意:选取最大子集集合,满足两两相减为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;
}
我的标签:做个有情怀的程序员。


猜你喜欢

转载自blog.csdn.net/ljd201724114126/article/details/80551678