Educational Codeforces Round 15_B. Powers of Two
3 seconds
256 megabytes
standard input
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).
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).
Print the number of pairs of indexes i, j (i < j) that ai + aj is a power of 2.
4
7 3 2 1
2
3
1 1 1
3
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的更多相关文章
- Educational Codeforces Round 15 Powers of Two
Powers of Two 题意: 让求ai+aj=2的x次幂的数有几对,且i < j. 题解: 首先要知道,排完序对答案是没有影响的,比如样例7 1一对,和1 7一对是样的,所以就可以排序之后 ...
- [Educational Codeforces Round 16]E. Generate a String
[Educational Codeforces Round 16]E. Generate a String 试题描述 zscoder wants to generate an input file f ...
- [Educational Codeforces Round 16]D. Two Arithmetic Progressions
[Educational Codeforces Round 16]D. Two Arithmetic Progressions 试题描述 You are given two arithmetic pr ...
- [Educational Codeforces Round 16]C. Magic Odd Square
[Educational Codeforces Round 16]C. Magic Odd Square 试题描述 Find an n × n matrix with different number ...
- [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 ...
- [Educational Codeforces Round 16]A. King Moves
[Educational Codeforces Round 16]A. King Moves 试题描述 The only king stands on the standard chess board ...
- Educational Codeforces Round 6 C. Pearls in a Row
Educational Codeforces Round 6 C. Pearls in a Row 题意:一个3e5范围的序列:要你分成最多数量的子序列,其中子序列必须是只有两个数相同, 其余的数只能 ...
- Educational Codeforces Round 9
Educational Codeforces Round 9 Longest Subsequence 题目描述:给出一个序列,从中抽出若干个数,使它们的公倍数小于等于\(m\),问最多能抽出多少个数, ...
- Educational Codeforces Round 37
Educational Codeforces Round 37 这场有点炸,题目比较水,但只做了3题QAQ.还是实力不够啊! 写下题解算了--(写的比较粗糙,细节或者bug可以私聊2333) A. W ...
随机推荐
- MyBatis面对Oracle数据库如何实现主键列自增长
因为Oracle数据库而言 不能够像SqlServer和MySql一样主键自增 而且MyBatis也没有提供直接的主键自增方法 所以我们自己使用查询语句来实现自增 实现代码: <insert i ...
- 蓝桥杯—盾神与条状项链(C++实现)
思路: 直接使用STL的list容器. 分两种情况: 1.DEL Q.直接调用void remove( const TYPE &val )函数即可. 2.ADD P Q.首先找出P所在的位置, ...
- dump报文转换为wrieshark报文
我们开发中经常会出原始的报文,如下所示: 45 00 01 3d 8e 6a 00 00 80 11 ab 46 00 00 00 00 ff ff ff ff 00 44 00 43 01 29 6 ...
- Oracle查询和解锁表
一些ORACLE中的进程被杀掉后,状态被置为"killed",但是锁定的资源很长时间不释放,有时实在没办法,只好重启数据库.现在提供一种方法解决这种问题,那就是在ORACLE中杀不 ...
- 强制改变IE中的文本模式
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
- 计算机网络课程优秀备考PPT之第四章介质访问控制层(四)
为了记录自己从2016.9~2017.1的<计算机网络>助教生涯,也为了及时梳理和整写笔记! 前期博客是, 计算机网络课程优秀备考PPT之第一章概述(一) 计算机网络课程优秀备考PPT之第 ...
- html5权威指南:设置文本样式
css部分---设置文本与字体样式:http://blog.csdn.net/hierarch_lee/article/details/52611250 补充---设置网页文本选中样式:http:// ...
- PAT 团体程序设计天梯赛-练习集 L1-005. 考试座位号
每个PAT考生在参加考试时都会被分配两个座位号,一个是试机座位,一个是考试座位.正常情况下,考生在入场时先得到试机座位号码,入座进入试机状态后,系统会显示该考生的考试座位号码,考试时考生需要换到考试座 ...
- HTTP Status 400 - Required request part 'file' is not present
今天使用Spring MVC做一个文件上传的功能,在提交表单的时候出现了如下错误:
- Java 中值传递和引用传递 区分
详细参见 http://blog.csdn.net/zzp_403184692/article/details/8184751