首页 技术 正文
技术 2022年11月16日
0 收藏 930 点赞 3,068 浏览 863 个字

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.

Examplesinput

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 ;
}

二分

上一篇: (0)git安装
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,490
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,905
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,738
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,491
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,129
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,292