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 ...
随机推荐
- NIO和IO(转)
java NIO由以下几个核心部分组成: Channels(通道) Buffers(缓冲区) Selectors(选择器) 其他 Channel和Buffer: 所有的IO再NIO中都从一个Chann ...
- SQL Server 2008 定时作业的制定
--SQL Server 2008 定时作业的制定 --1.打开[SQL Server Management Studio],在[对象资源管理器]列表中选择[SQL Server 代理]: --2.鼠 ...
- python 基础学习3-函数
1. 函数参数-默认参数 python函数也可以跟C语言一样,在函数的形参中设定默认值. >>> def test(flag, port = 8080) ... print port ...
- Android中帧动画的创建
帧动画,实质上就是快速播放多张连接效果的图片,现在一般可用于下拉刷新时候的headView 实现步骤: 1.首先应该准备一组连接效果的图片 2.在res>drawable目录下创建xml文件,将 ...
- 模仿快递路线图的html, css 样式
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- 转:LoadRunner响应时间与用户体验时间不一致问题的深入分析
在新一代一期项目非功能测试过程中,我们发现了LoadRunner测试响应时间与客户端实际用户体验时间不一致的现象.例如员工渠道上线后,客户体验时间远远超过了LoadRunner测试响应时间.本文针对这 ...
- openstack私有云布署实践【14.1 登录页dashboard-controller(科兴环境)】
2台kxcontroller安装组件 # yum install openstack-dashboard -y 修改一样的配置 vi /etc/openstack-dashboard/local_ ...
- Java多线程:线程同步与关键字synchronized
一.同步的特性1. 不必同步类中所有的方法, 类可以同时拥有同步和非同步方法.2. 如果线程拥有同步和非同步方法, 则非同步方法可以被多个线程自由访问而不受锁的限制. 参见实验1:http://blo ...
- 2016WHD.china世界云计算日·北京站即将召开
WHD.china自进驻中国以来,已在上海.北京成功举办多届,2015年于北京举办的会议更是盛况空前,注册参会者逾800人次,汇聚了国内外众多知名云服务商.IDC商.电子商务企业.电信运营商.ISP等 ...
- gsl安装(Linux系统)
1. 在gnu的ftp站点http://ftp.gnu.org/gnu/gsl/ 上, 下载最新的gsl-2.x.tar.gz 2. 解压下载好的gsl-2.x.tar.gz 压缩包,$tar -zx ...