A sequence a1,a2,…,ana1,a2,…,an is called good if, for each element aiai, there exists an element ajaj (i≠ji≠j) such that ai+ajai+aj is a power of two (that is, 2d2d for some non-negative integer dd).
For example, the following sequences are good:
- [5,3,11][5,3,11] (for example, for a1=5a1=5 we can choose a2=3a2=3. Note that their sum is a power of two. Similarly, such an element can be found for a2a2 and a3a3),
- [1,1,1,1023][1,1,1,1023],
- [7,39,89,25,89][7,39,89,25,89],
- [][].
Note that, by definition, an empty sequence (with a length of 00) is good.
For example, the following sequences are not good:
- [16][16] (for a1=16a1=16, it is impossible to find another element ajaj such that their sum is a power of two),
- [4,16][4,16] (for a1=4a1=4, it is impossible to find another element ajaj such that their sum is a power of two),
- [1,3,2,8,8,8][1,3,2,8,8,8] (for a3=2a3=2, it is impossible to find another element ajaj such that their sum is a power of two).
You are given a sequence a1,a2,…,ana1,a2,…,an. What is the minimum number of elements you need to remove to make it good? You can delete an arbitrary set of elements.
The first line contains the integer nn (1≤n≤1200001≤n≤120000) — the length of the given sequence.
The second line contains the sequence of integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109).
Print the minimum number of elements needed to be removed from the given sequence in order to make it good. It is possible that you need to delete all nn elements, make it empty, and thus get a good sequence.
6 4 7 1 5 4 9
1
5 1 2 3 4 5
2
1 16
1
4 1 1 1 1023
0
In the first example, it is enough to delete one element a4=5a4=5. The remaining elements form the sequence [4,7,1,4,9][4,7,1,4,9], which is good..
汉译版:
序列a 1,a 2,... ,a n一个1,一个2,...,一个ñ如果,对于每个元素a i,被称为好一个一世,有一个元素a j一个Ĵ(我≠ j一世≠Ĵ)这样一个i + a j一个一世+一个Ĵ是2的幂(即2 d2d对于一些非负整数dd)。
例如,以下序列是好的:
- [ 5 ,3 ,11 ][五,3,11](例如,对于一个1 = 5一个1=五我们可以选择一个2 = 3一个2=3。请注意,它们的总和是2的幂。类似地,这样的元件可以发现对于一个2一个2和一个3一个3)
- [ 1 ,1 ,1 ,1023 ][1,1,1,1023],
- [ 7 ,39 ,89 ,25 ,89 ][7,39,89,25,89],
- [ ][]。
请注意,根据定义,为空序列(长度为00) 很好。
例如,以下序列不好:
- [ 16 ][16](对于一个1 = 16一个1=16,这是不可能找到另一个元素一个Ĵ一个Ĵ 这样他们的总和是2的幂,)
- [ 4 ,16 ][4,16](对于一个1 = 4一个1=4,这是不可能找到另一个元素一个Ĵ一个Ĵ 这样他们的总和是2的幂,)
- [ 1 ,3 ,2 ,8 ,8 ,8 ][1,3,2,8,8,8](对于一个3 = 2一个3=2,这是不可能找到另一个元素一个Ĵ一个Ĵ 这样他们的总和是2的幂。
给你一个序列a 1,a 2,... ,a n一个1,一个2,...,一个ñ。为了使其良好,您需要删除的最小元素数是多少?您可以删除任意元素集。
第一行包含整数nñ(1 ≤ Ñ ≤ 1200001≤ñ≤120000) - 给定序列的长度。
第二行包含整数序列a 1,a 2,... ,a n一个1,一个2,...,一个ñ(1 ≤ 一个我 ≤ 10 91≤一个一世≤109)。
打印从给定序列中删除所需的最少元素数量,以使其良好。您可能需要删除所有nñ 元素,使其为空,从而获得良好的序列。
6 4 7 1 5 4 9
1
5 1 2 3 4 5
2
1 16
1
4 1 1 1 1023
0
在第一个例子中,删除一个元素a 4 = 5就足够了一个4=五。剩余的元件形成的序列[ 4 ,7 ,1 ,4 ,9 ][4,7,1,4,9],这是好序列。
在本题中,我们需要排除出不能与其他数构成2 的幂次方的数。于是,首先打表将2 ~ 2^31全部统计在VIS数组中,然后输入数据,将数据排序后再进行二分,注意标记掉已经找到的符合条件的数据,最后统计不符合条件的数据有多少个
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN = 1000005;
int num[MAXN];
bool book[MAXN];
int vis[32];
void first()
{
vis[0] = 1;
for(int i = 1; i <= 30; i++)
vis[i] = vis[i - 1] << 1;
}
int main()
{
int n;
first();
memset(book, false, sizeof(book));
scanf("%d", &n);
for(int i = 1; i <= n; i++)
scanf("%d", &num[i]);
if(n == 1)
printf("1\n");
else
{
sort(num + 1, num + n + 1);
for(int i = 1; i <= n; i++)
{
if(book[i] == true)
continue;
for(int j = 1; j <= 30; j++)
{
bool flag = false;
int s = vis[j] - num[i];
int l = lower_bound(num + 1, num + n + 1, s) - num;
int r = upper_bound(num + 1, num + n + 1, s) - num;
for(int k = l; k <= r; k++)
{
if(num[k] == s && k != i)
{
book[k] = true;
flag = true;
}
}
if(flag)
book[i] = true;
}
}
int ans = 0;
for(int i = 1; i <= n; i++)
if(book[i] == false)
ans++;
printf("%d\n", ans);
}
return 0;
}