Codeforces ~ 988D ~ Points and Powers of Two (思维)

题意: 给你N个数字(任意两个数字都不同),挑出一些数字,使得这些数字中任意两个数字的差都为2的次幂,输出这些数字,如果有多解输出数字个数最多的那个,还有多解任意输出。

思路: 关键点在于发现这种数字最多有三个。
三个:
a b = 2 x a c = 2 y b c = 2 z + a c = 2 x + 2 z = 2 y x = z = y 1

同理四个数时有: 2 x + 2 y + 2 z = 2 d ,明显式子不成立,所以最多有三个。
那我们依次判断三个数,两个数,一个数的时候即可。三个数的时候假设从数组中选一个数 x x + 2 d , x + 2 d + 1 2 d 1 e 9 都在数组中出现了,输出这三个数即可。两个数的时候同理。一个数直接随意输出一个即可。

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200005;
typedef long long ll;
ll n, a[MAXN];
set<ll> s;
int main()
{
    scanf("%lld", &n);
    for (int i = 0; i < n; i++)
    {
        scanf("%lld", &a[i]);
        s.insert(a[i]);
    }
    for (int i = 0; i < n; i++)
    {
        for (ll j = 1; j < 1e10; j *= 2)
        {
            if (s.count(a[i]+j) && s.count(a[i]+2*j))
            {
                printf("3\n");
                printf("%lld %lld %lld\n", a[i], a[i]+j, a[i]+2*j);
                return 0;
            }
        }
    }
    for (int i = 0; i < n; i++)
    {
        for (ll j = 1; j < 1e18; j *= 2)
        {
            if (s.count(a[i]+j))
            {
                printf("2\n");
                printf("%lld %lld\n", a[i], a[i]+j);
                return 0;
            }
        }
    }
    printf("1\n");
    printf("%lld\n", a[0]);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/ZscDst/article/details/80566411