[CF988D] Points and Powers of Two 题解

· · 题解

[CF988D] Points and Powers of Two 题解

思路分析

显然,最多存在 3 个数,证明如下:

a < b < c < d \\ a + 2^i = b, b + 2^j = c \\ \because c - a = 2^k, k \in \mathbb{Z} \\ \therefore i = j \\ \therefore d = c + 2^{i + 1} = a + 2^{i + 2} \\ \therefore \neg l \in \mathbb{Z}, d - b = 2 ^ l \\

所以最多只能有 3 个数。

代码实现

#include <cstdio>
#include <map>
constexpr int MaxN = 2e5 + 5;
int n;
long long p[MaxN];
std::map<long long, bool> map;
int main()
{
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
    {
        scanf("%lld", &p[i]);
        map[p[i]] = true;
    }
    for (int i = 1; i <= n; i++)
    {
        for (long long j = 1; j <= 2e9; j *= 2)
        {
            if (map.count(p[i] + j) && map.count(p[i] + 2 * j))
            {
                printf("3\n");
                printf("%lld %lld %lld\n", p[i], p[i] + j, p[i] + 2 * j);
                return 0;
            }
        }
    }
    for (int i = 1; i <= n; i++)
    {
        for (long long j = 1; j <= 2e9; j *= 2)
        {
            if (map.count(p[i] + j))
            {
                printf("2\n");
                printf("%lld %lld\n", p[i], p[i] + j);
                return 0;
            }
        }
    }
    printf("1\n");
    printf("%lld\n", p[1]);
    return 0;
}