B. Powers of Two
 

You are given n integers a1, a2, ..., an. Find the number of pairs of indexes i, j (i < j) that ai + aj is a power of 2 (i. e. some integer xexists so that ai + aj = 2x).

Input

The first line contains the single positive integer n (1 ≤ n ≤ 105) — the number of integers.

The second line contains n positive integers a1, a2, ..., an (1 ≤ ai ≤ 109).

Output

Print the number of pairs of indexes i, j (i < j) that ai + aj is a power of 2.

Examples
input
4
7 3 2 1
output
2
input
3
1 1 1
output
3
【分析】:仔细读题后发现,ai不会超过10 ^ 9,约等于2 ^ 30,所以我们可以先打出一张2 ^ k的表,然后对于ai,用2 ^ k减去ai,利用二分搜索得到aj的数量,累加即得结果。时间复杂度为O(N)。
【代码】:
#include<bits/stdc++.h>

using namespace std;
#define ll long long
#define maxn 100010
ll a[maxn];
int n;
/*
给出一个数组,求出ai + aj等于2的幂的数对个数
*/
int main()
{ while(cin >> n){
ll ans = ;
for(int i=; i<n; i++)
cin >> a[i];
sort(a,a+n);
for(ll i=; i<n; i++){
for(ll j=; j<; j++){
ll t = (<<j) - a[i];
ans += upper_bound(a+i+,a+n,t)-lower_bound(a+i+,a+n,t);
}
}
cout << ans << endl;
}
return ;
}

二分

CodeForces 702B Powers of Two【二分/lower_bound找多少个数/给出一个数组 求出ai + aj等于2的幂的数对个数】的更多相关文章

  1. 给出一个数组A,找出一对 (i, j)使得A[i] <= A[j] (i < j)并且j-i最大

    题目:给出一个数组A,找出一对 (i, j)使得A[i] <= A[j] (i <= j)并且j-i最大 ,若有多个这样的位置对,返回i最小的那一对. 最直接的想法就是对于每一个 i 从数 ...

  2. 笔试题&amp;面试题:找出一个数组中第m小的值并输出

    题目:找出一个数组中第m小的值并输出. 代码: #include <stdio.h> int findm_min(int a[], int n, int m) //n代表数组长度,m代表找 ...

  3. CodeForces 702B Powers of Two (暴力,优化)

    题意:给定 n 个数,问你从有多少下标 i < j,并且 ai + aj 是2的倍数. 析:方法一: 从输入开始暴力,因为 i < j 和 i > j 是一样,所以可以从前面就开始查 ...

  4. CodeForces 702B Powers of Two

    简单题. 开一个$map$记录一下每个数字出现了几次,那么读入的时候$f[a[i]]+1$. 计算$a[i]$做出的贡献的时候,先把$f[a[i]]-1$,然后再枚举$x$,答案加上$f[{2^x} ...

  5. 368. Largest Divisible Subset -- 找出一个数组使得数组内的数能够两两整除

    Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of ...

  6. Problem A: 零起点学算法91——找出一个数组中出现次数最多的那个元素

    #include<stdio.h> int main() { ],b[]={}; while(scanf("%d",&n)!=EOF) { ;i<n;i+ ...

  7. codeforces 702B B. Powers of Two(水题)

    题目链接: B. Powers of Two time limit per test 3 seconds memory limit per test 256 megabytes input stand ...

  8. Codeforces 484B Maximum Value(高效+二分)

    题目链接:Codeforces 484B Maximum Value 题目大意:给定一个序列,找到连个数ai和aj,ai%aj尽量大,而且ai≥aj 解题思路:类似于素数筛选法的方式,每次枚举aj,然 ...

  9. Codeforces Round #404 (Div. 2) A,B,C,D,E 暴力,暴力,二分,范德蒙恒等式,树状数组+分块

    题目链接:http://codeforces.com/contest/785 A. Anton and Polyhedrons time limit per test 2 seconds memory ...

随机推荐

  1. tomcat8+idea远程调试

    window下 setenv.bat增加 set JPDA_OPTS=-Xrunjdwp:transport=dt_socket,address=8787,server=y,suspend=n lin ...

  2. Kinect安装

    在连接kinect机器前,需要先安装两个软件,而在安装这两个软件前需要有vs2010(专业版本和快速版),因为需要包含.net framework 4.0 kinect sdk http://www. ...

  3. TCP/IP网络编程之地址族与数据序列

    分配IP地址和端口号 IP是Internet Protocol(网络协议)的简写,是为收发网络数据而分配给计算机的值.端口号并非赋予计算机的值,而是为区分程序中创建的套接字而分配给套接字的序号 网络地 ...

  4. 68、TSPL指令集(标签打印机)

    0.开发者pdf.sdk等资料详见: http://download.csdn.net/detail/kunyashaw/9376694 1.测试代码 包含文字打印.条形码打印.二维码打印 packa ...

  5. JWT实现token的生成和认证demo

    上篇写到对JWT的理解,这篇写一个小的demo来实践下 Github:https://github.com/wuhen152033/token/tree/dev 简介 本次的demo是基于Spring ...

  6. MySQL5.7(三)数据表操作

    概念在数据库中,数据表是数据库中最重要.最基本的操作对象,是数据存储的基本单位.数据表被定义为列的集合,数据在表中是按照行和列的格式来存储的.每一行代表一条唯一的记录,每一列代表记录中的一个域.1.创 ...

  7. centos php环境搭建

    CentOS 6.5系统安装配置LAMP(Apache+PHP5+MySQL)服务器环境 准备篇: 1.配置防火墙,开启80端口.3306端口vi /etc/sysconfig/iptables-A ...

  8. 关于C++ STL标准库中map 的多元素应用

    map的特性是,所有的元素会根据键值自动排序.map的所有元素都是pair,同时拥有实值(value)和键值(key).pair的第一个元素被视为键值,第二个被视为实质piar 的定义 templat ...

  9. poj3009 Curling 2.0 (DFS按直线算步骤)

    Curling 2.0 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14563   Accepted: 6080 Desc ...

  10. 【bzoj3083】遥远的国度 树链剖分+线段树

    题目描述 描述zcwwzdjn在追杀十分sb的zhx,而zhx逃入了一个遥远的国度.当zcwwzdjn准备进入遥远的国度继续追杀时,守护神RapiD阻拦了zcwwzdjn的去路,他需要zcwwzdjn ...