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. vs2008编译FileZilla服务端源码

    vs2008编译FileZilla服务端源码 FileZilla服务端下载地址:https://download.filezilla-project.org/server/.FileZilla服务端源 ...

  2. Android Game Examples

    Game2 using UnityEngine; using System.Collections; public class Game2_Player : MonoBehaviour { publi ...

  3. MAC下安装automake autoconf工具

    I noticed today that while Mac OS 10.6 (specifically, 10.6.2) comes with automake and autoconf, the ...

  4. zookeeper 安装 windows环境

    1.zookeeper下载地址: http://mirrors.cnnic.cn/apache/zookeeper/ 2.单点配置 把下载的zookeeper的文件解压到指定目录 修改conf下增加一 ...

  5. js变量,语句

  6. Idea1.5使用Maven搭建Apache Spark1.6源码阅读环境

    1.插件安装,在Idea界面依次:File->settings->plugins,安装Maven 2.下载Spark1.6.2源码,这个在GitHub上下载,具体流程自己百度,很简单 3. ...

  7. java中的词汇

    java中的词汇: 空白符:空格,制表符,换行符.他们的存在使得代码变得很美观. 标识符:由大小写字母,数字,下划线,美元符号组成.且数字不能用于标识符第一个字符. 字面值:变量的值通常使用表示常量的 ...

  8. Struts2-3.struts.xml的action可以简写

    如果只是跳转到某个页面的话,可以这样写 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE s ...

  9. /tmp 和 /var/tmp 的区别

    /tmp is meant as fast (possibly small) storage with a short time to live (TTL). Many systems clean / ...

  10. HDU 1253 胜利大逃亡(BFS)

    题目链接 Problem Description Ignatius被魔王抓走了,有一天魔王出差去了,这可是Ignatius逃亡的好机会.魔王住在一个城堡里,城堡是一个A*B*C的立方体,可以被表示成A ...