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

Input

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

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

64 7 1 5 4 9

output

Copy

1

input

Copy

51 2 3 4 5

output

Copy

2

input

Copy

116

output

Copy

1

input

Copy

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

用加法,用暴力一个个找能匹配的元素,并标记找到的元素,三重循环,超时

#include<bits/stdc++.h>
using namespace std;
int a[120005];
map<int,int>vis;
int main()
{int n;int i,j,k;for(i=0; i<n; i++){cin>>a[i];}int flag=0;for(i=0; i<n; i++){flag=0;if(vis[a[i]]!=1){for(j=0; j<n; j++){if(flag==1)break;for(k=1; k<34; k++){if(a[i]+a[j]==pow(2,k)){flag=1;vis[a[i]]=1;vis[a[j]]=1;break;}}}}}
int sum=0;
for(i=0;i<n;i++)if(vis[a[i]]!=1)sum++;
cout<<sum;
}

只是判断对应的匹配元素存不存在,不用管是多少,只需要两重循环,还是用map,用减法

#include<bits/stdc++.h>
using namespace std;
int a[120005];
map<int,int >vis;
int main()
{int n;scanf("%d",&n);int i,j,k;for(i=0; i<n; i++){scanf("%d",&a[i]);vis[a[i]]++;}int sum=0;for(i=0;i<n;i++){for(j=1;j<32;j++){if(vis[pow(2,j)-a[i]]>1||(vis[pow(2,j)-a[i]]==1&&pow(2,j)!=2*a[i])){sum++;break;}}}printf("%d",n-sum);
}

加法要遍历两遍所有元素,那就用减法,只遍历一遍即可(思想)相关推荐

  1. python 遍历两个列表_python 如何在一个for循环中遍历两个列表

    利用python自带的zip函数可同时对两个列表进行遍历,代码如下: >>> list1 = ['a', 'b', 'c', 'd'] >>> list2 = [' ...

  2. Python遍历列表时删除元素的几种方法

    问题来源: 删除列表中的指定元素 描述: 输入一个整数列表,并输入一个整数x,删除列表中所有值为x的数,再输出列表. 问题代码: a = list(eval(input())) x = eval(in ...

  3. 将N个整数按从小到大排序的冒泡排序法是这样工作的:从头到尾比较相邻两个元素,如果前面的元素大于其紧随的后面元素,则交换它们。通过一遍扫描,则最后一个元素必定是最大的元素。然后用同样的方法对前N−1个元

    7-3 冒泡法排序 将N个整数按从小到大排序的冒泡排序法是这样工作的:从头到尾比较相邻两个元素,如果前面的元素大于其紧随的后面元素,则交换它们.通过一遍扫描,则最后一个元素必定是最大的元素.然后用同样 ...

  4. Python语法糖——遍历列表时删除元素

    Python的for可以遍历一个List,但是在遍历的过程中删除元素常常会得到意想不到的结果甚至程序出现异常,例如: lst = [1, 1, 0, 2, 0, 0, 8, 3, 0, 2, 5, 0 ...

  5. scala使用reduce和fold方法遍历集合的所有元素

    Problem 你想要遍历有序集合的所有元素,并且随着你对集合元素的遍历,对比两个相邻的元素 Solution 使用reduceLeft, foldLeft, reduceRight, foldRig ...

  6. python 如何在一个for循环中遍历两个列表

    是我在看<笨方法学python>过程中发现有一行代码看不懂--" for sentence in snippet, phrase:",所以研究了半天,感觉挺有收获的.所 ...

  7. 【Linux】Shell脚本中如何使用“循环”遍历“数组”中的元素(包括MySQL的常用指令介绍)

    一.背景 实习过程中,今天mentor突然让我拉取一下远端园区数据库中的部分信息,因为包含很多不同园区的数据信息,而且要以园区为单位生成文件来对数据进行存放,因此自然是需要使用shell脚本来自动生成 ...

  8. python 如何同时遍历两个列表

    问题 我们在数据处理过程中可能会遇到两个列表之间的元素有着一对一的关系,列表之间的数据有联系,我们需要同时遍历两个列表进行相关的操作,下面展示同时遍历两个列表的方法,上代码!!! 方法 使用zip() ...

  9. List集合遍历时如何删除元素

    背景:在业务中遍历某个集合时涉及到排除某个属性为0的情况,即遍历时删除. 一.3种错误方式演示三种: 1.foreach List<String> list = new ArrayList ...

  10. python中的enumerate函数用于遍历序列中的元素以及它们的下标

    enumerate 函数用于遍历序列中的元素以及它们的下标: >>> for i,j in enumerate(('a','b','c')):  print i,j 0 a 1 b ...

最新文章

  1. Unix环境高级编程(一)文件I/O
  2. python判断对象是否有属性
  3. ElementNotVisible exception in Selenium WebDriver
  4. CentOS7.4安装PyQt5.6
  5. 【python】dist-packages和site-packages的区别
  6. java excel 操作 poi_Java使用apache poi进行excel相关操作
  7. 一条SQL语句的执行过程
  8. 【Python爬虫】网络爬虫的“盗亦有道”
  9. 华为音量键只能调通话_手机音量键还在以为只能调音量?别傻了,赶紧看看下文绝对涨知识!...
  10. 尚学堂java培训_送给 Java 自学者或者初学者的最全知识清单,2020 年 Java 就该这么学...
  11. vi/vim使用进阶: quickfix
  12. MySQL架构体系(从一条语句出发了解MySQL各部分的作用)
  13. 中冠百年|到了30岁应该如何增加收入?
  14. 51单片机学习 基础知识总结
  15. 淘宝上买东西,怎么买最便宜?
  16. 做网站开发设计注意字体是否侵权
  17. 基于 SPICE 协议的硬编推流整合方案在云游戏中的应用
  18. 最新版Nginx安装教程来了,快来看看
  19. 中国信保总经理唐若疑卷入ST宏盛合同诈骗案昕下马
  20. Docker - volume、-v 区别

热门文章

  1. 1主2从基于GKE搭建k8s集群-无需科学上网
  2. python和java哪个更有潜力-Python和JAVA的就业前景哪个好点?
  3. Linux系统下管理员账号root忘记密码怎么找回
  4. js基础练习题(1)
  5. IDEA(或Android Studio)CamelCase插件的使用(解释了CamelCase插件无法使用问题)
  6. Python内置函数(知识总结篇)
  7. 实训计算机硬盘分区的心得体会,计算机实训报告
  8. 手动安装Python库或安装第三方库的方法
  9. jsDOM编程-小球在盒子里来回撞击
  10. Amazon Glue ETL 作业调度工具选型初探