Codeforces 2018.7.9 C. Summarize to the Power of Two

C. Summarize to the Power of Two
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

A sequence a1,a2,,ana1,a2,…,an is called good if, for each element aiai, there exists an element ajaj (iji≠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.

Input

The first line contains the integer nn (1n1200001≤n≤120000) — the length of the given sequence.

The second line contains the sequence of integers a1,a2,,ana1,a2,…,an (1ai1091≤ai≤109).

Output

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.

Examples
input
Copy
6
4 7 1 5 4 9
output
Copy
1
input
Copy
5
1 2 3 4 5
output
Copy
2
input
Copy
1
16
output
Copy
1
input
Copy
4
1 1 1 1023
output
Copy
0
Note

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

汉译版:

C.总结两个人的力量
每次测试的时间限制
3秒
每次测试的内存限制
256兆字节
输入
标准输入
产量
标准输出

序列a 1a 2... a n一个1一个2...一个ñ如果,对于每个元素a i,被称为好一个一世,有一个元素a j一个Ĵj一世Ĵ)这样一个i + a j一个一世+一个Ĵ是2的幂(即2 d2d对于一些非负整数dd)。

例如,以下序列是好的:

  • [ 5 3 11 ][311](例如,对于一个1 = 5一个1=我们可以选择一个2 = 3一个2=3请注意,它们的总和是2的幂。类似地,这样的元件可以发现对于一个2一个2一个3一个3
  • [ 1 1 1 1023 ][1111023]
  • [ 7 39 89 25 89 ][739892589]
  • [ ][]

请注意,根据定义,为空序列(长度为00) 很好。

例如,以下序列不好:

  • [ 16 ][16](对于一个1 = 16一个1=16,这是不可能找到另一个元素一个Ĵ一个Ĵ 这样他们的总和是2的幂,)
  • [ 4 16 ][416](对于一个1 = 4一个1=4,这是不可能找到另一个元素一个Ĵ一个Ĵ 这样他们的总和是2的幂,)
  • [ 1 3 2 8 8 8 ][132888](对于一个3 = 2一个3=2,这是不可能找到另一个元素一个Ĵ一个Ĵ 这样他们的总和是2的幂。

给你一个序列a 1a 2... a n一个1一个2...一个ñ为了使其良好,您需要删除的最小元素数是多少?您可以删除任意元素集。

输入

第一行包含整数nñ1 Ñ 1200001ñ120000) - 给定序列的长度。

第二行包含整数序列a 1a 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 ][47149],这是好序列。

在本题中,我们需要排除出不能与其他数构成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;
}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值