B. Powers of Two
time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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 x exists 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
Note

In the first example the following pairs of indexes include in answer: (1, 4) and (2, 4).

In the second example all pairs of indexes (i, j) (where i < j) include in answer.

题意:

给你n个数,问你有多少对满足a[i]+a[j]为2的次方.

题解:

首先,我们考虑他们能加起来的sum最多也就32个,0~2^31,所以我们对每一个a[i]都枚举sum-a[i],然后二分找这个数集里面有多少个满足条件,这里要考虑特殊的情况,如果sum-a[i]=a[i],那么你找到满足条件的个数要减1,然后最后ans要除2,因为sum-a[i]=a[j],sum-a[j]=a[i],算了2次

 #include<bits/stdc++.h>
#define F(i,a,b) for(int i=a;i<=b;++i)
using namespace std;
typedef long long ll; const int N=1e5+;
int a[N];
int main(){
int n;
ll ans=;
scanf("%d",&n);
F(i,,n)scanf("%d",a+i);
sort(a+,a++n);
F(i,,n)
{
for(int j=;j>=;j--){
int now=<<j;
if(now<a[i])break;
int tmp=now-a[i];
int pos1=lower_bound(a+,a++n,tmp)-a;
int pos2=upper_bound(a+,a++n,tmp)-a;
if(a[pos1]==tmp){
ans+=pos2-pos1;
if(tmp==a[i])ans--;
}
}
}
ans/=;
printf("%I64d\n",ans);
return ;
}

Educational Codeforces Round 15_B. Powers of Two的更多相关文章

  1. Educational Codeforces Round 15 Powers of Two

    Powers of Two 题意: 让求ai+aj=2的x次幂的数有几对,且i < j. 题解: 首先要知道,排完序对答案是没有影响的,比如样例7 1一对,和1 7一对是样的,所以就可以排序之后 ...

  2. [Educational Codeforces Round 16]E. Generate a String

    [Educational Codeforces Round 16]E. Generate a String 试题描述 zscoder wants to generate an input file f ...

  3. [Educational Codeforces Round 16]D. Two Arithmetic Progressions

    [Educational Codeforces Round 16]D. Two Arithmetic Progressions 试题描述 You are given two arithmetic pr ...

  4. [Educational Codeforces Round 16]C. Magic Odd Square

    [Educational Codeforces Round 16]C. Magic Odd Square 试题描述 Find an n × n matrix with different number ...

  5. [Educational Codeforces Round 16]B. Optimal Point on a Line

    [Educational Codeforces Round 16]B. Optimal Point on a Line 试题描述 You are given n points on a line wi ...

  6. [Educational Codeforces Round 16]A. King Moves

    [Educational Codeforces Round 16]A. King Moves 试题描述 The only king stands on the standard chess board ...

  7. Educational Codeforces Round 6 C. Pearls in a Row

    Educational Codeforces Round 6 C. Pearls in a Row 题意:一个3e5范围的序列:要你分成最多数量的子序列,其中子序列必须是只有两个数相同, 其余的数只能 ...

  8. Educational Codeforces Round 9

    Educational Codeforces Round 9 Longest Subsequence 题目描述:给出一个序列,从中抽出若干个数,使它们的公倍数小于等于\(m\),问最多能抽出多少个数, ...

  9. Educational Codeforces Round 37

    Educational Codeforces Round 37 这场有点炸,题目比较水,但只做了3题QAQ.还是实力不够啊! 写下题解算了--(写的比较粗糙,细节或者bug可以私聊2333) A. W ...

随机推荐

  1. 8. Shell 文件包含

    1. 语法 . filename # 注意点号(.)和文件名中间有一空格 或 source filename ### test.sh #!/bin/bash url="www.baidu.c ...

  2. ios9 新关键字 __kindof 等(etc) 小结

    首先__kindof:规定参数为UITableViewCell(举例)这个类或者其子类.比如说一个NSArray<UIView *>*,如果不加__kindof,这个数组只能有UIView ...

  3. mac下配置gdb调试golang

    mac下配置gdb调试golang 原文链接 https://sourceware.org/gdb/wiki/BuildingOnDarwin Building GDB for Darwin Crea ...

  4. javascript screen对象

    1.screen对象用于获取用户的屏幕信息 2. window.screen.属性 3.对象属性

  5. java.lang.OutOfMemoryError: Java heap space错误及处理办

    默认方式启动Eclipse时,有关启动时JVM参数是在Eclipse安装目录下的eclipse.ini文件中指定的.在命令行下,也可以通过参数-vmargs来达到此目的.其命令格式为:eclipse ...

  6. 【成长之路】【python】python基础2

    1.位运算 &(与) | (或) ~ (非) ^(异或) 2.三元运算 c = a+b if a>b else a-b if (a>b): a+b else: a-b 3.小知识( ...

  7. EconomicIndoor集成测试

    加密时序列号相同引发的呼叫功能异常 现象描述: 配置完房间号一次性呼叫问题 问题描述: 两台室内机升级后配置到同一门口机, 各种配置正确. 作为主叫呼不出去, 作为被叫可以接听, 每修改一次房间号, ...

  8. 《JS权威指南学习总结--7.8 数组方法》

    内容要点: 一.join() Array.join()方法将数组中所有元素都转化为字符串并连接在一起,返回最后生产的字符串. 可以指定一个可选的字符串在生产的字符串中来分隔数组的各个元素.如果不指定分 ...

  9. Effective JavaScript :第五章

    1.使用Object的直接实例构造轻量级的字典 字典就是可变长的字符串与值得映射集合.JavaScript甚至提供了枚举一个对象属性名的利器——for...in循环. var dict = { ali ...

  10. Dokan官方说明文档

    Dokan 库Copyright(c) Hiroki Asakawa http://dokan-dev.net/en 什么是Dokan库================================ ...