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 ...
随机推荐
- svn的基本配置及安装
1.检查是否已安装 rpm -qa subversion 如果要卸载旧版本: yum remove subversion 2.安装 yum install subversion 3.检查是否安装成功 ...
- 基于jdk调用天气服务案例及问题
问题1:解析wsdl文件时出现 把网络上的wsdl保存到本地,把圈起来的那段删掉 代码: 当返回结果不是String类型时: 输入城市编码去查找 所以: 问题二:如果把本地wsdl文件删除的话需要 三 ...
- mysql 用户权限设置
windows使用的zip包的mysql.配置成功后,root用户没有权限,需要进行权限分配.管理员运行cmd,执行以下命令,进入mysql. mysql -u root -p Passwd Pass ...
- 【转】spring管理属性配置文件properties——使用PropertiesFactoryBean|spring管理属性配置文件properties——使用PropertyPlaceholderConfigurer
spring管理属性配置文件properties--使用PropertiesFactoryBean 对于属性配置,一般采用的是键值对的形式,如:key=value属性配置文件一般使用的是XXX.pr ...
- 数据库出现1045 access denied for user 'root'@'localhost' using password yes (转)
在mysql命令行中执行 SET PASSWORD FOR 'root'@'localhost' = PASSWORD('123456'); GRANT ALL PRIVILEGES ON *.* ...
- 第16 天 JavaWEB过滤器和监听器技术
Day16 JavaWEB过滤器和监听器技术 复习: 1.大结果集分页mysql的实现,是使用那个关键字,从user表中取第一页的数据,长度为10,sql语句怎么写? 2.分页查询的起始位置(star ...
- js--事件对象的理解3
实例2: 跟随鼠标的DIV-- <script> document.onmousemove=function (ev) { var oEvent=ev||event; var oDiv=d ...
- ecshop里的$_CFG从哪来的
以前经常有朋友问我, ecshop系统的$_CFG这个数组是从哪里来的,在哪里定义并赋值的. 下面就给大家说一下这个全局变量 $GLOBALS['_CFG']. ecshop里的 $_CFG数组主 ...
- java 邮件收发 (只能输入英文,中文需要转码)
//发件 package com.sun.mail;import java.io.UnsupportedEncodingException;import java.util.Properties;im ...
- Hibernate配置文件的hbm2ddl.auto属性
今天遇到一个有意思的问题,我目前做的一个网站采用Spring MVC + Spring + Hibernate的架构,我通过页面插入了一些数据到数据库,可是每次重启tomcat之后,数据都莫名其妙地丢 ...