cf702B 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).
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个数字,问有多少对数字加起来刚好是2的k次方。
这还用说?枚举个k再枚举个a[i]然后看看有没有2^k-a[i]这个数就好了
这里我为了防被x没用hash用了二分
不过要考虑一个数字出现很多次的情况,或者你要找的刚好就是这个数的情况
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<set>
#include<map>
#include<ctime>
#define LL long long
#define inf 0x7ffffff
#define pa pair<int,int>
#define pi 3.1415926535897932384626433832795028841971
using namespace std;
inline LL read()
{
LL x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
inline void write(LL a)
{
if (a<){printf("-");a=-a;}
if (a>=)write(a/);
putchar(a%+'');
}
inline void writeln(LL a){write(a);printf("\n");}
int n,sav;
LL ans;
int a[];
int rep[];
int bin[];
inline bool bsearch(int l,int r,int x,int dat)
{
if (dat<=)return ;
if (l>r)return ;
int ans=-;
while (l<=r)
{
int mid=(l+r)>>;
if (a[mid]==dat){ans=mid;break;}
if (a[mid]>dat)r=mid-;
if (a[mid]<dat)l=mid+;
}
sav=ans;
return (ans!=x||ans==x&&rep[x]>)&&a[ans]==dat;
}
int main()
{
n=read();
bin[]=;
for (int i=;i<;i++)bin[i]=bin[i-]*;
for(int i=;i<=n;i++)a[i]=read();
sort(a+,a+n+);
int cur=;
for(int i=;i<=n;i++)
{
if (a[i]!=a[i-])a[++cur]=a[i],rep[cur]=;
else rep[cur]++;
}
n=cur;
for(int i=;i<=n;i++)
{
for (int j=;j<;j++)
if (bsearch(i,n,i,bin[j]-a[i]))
{
if (sav==i)ans+=(LL)rep[i]*(rep[i]-)/;
else ans+=(LL)rep[i]*rep[sav];
}
}
printf("%lld\n",ans);
}
cf702B
cf702B Powers of Two的更多相关文章
- CodeForces 404C Ivan and Powers of Two
Ivan and Powers of Two Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & % ...
- Educational Codeforces Round 15 Powers of Two
Powers of Two 题意: 让求ai+aj=2的x次幂的数有几对,且i < j. 题解: 首先要知道,排完序对答案是没有影响的,比如样例7 1一对,和1 7一对是样的,所以就可以排序之后 ...
- Educational Codeforces Round 7 F - The Sum of the k-th Powers 拉格朗日插值
The Sum of the k-th Powers There are well-known formulas: , , . Also mathematicians found similar fo ...
- (Problem 29)Distinct powers
Consider all integer combinations ofabfor 2a5 and 2b5: 22=4, 23=8, 24=16, 25=32 32=9, 33=27, 34=81, ...
- UVA 10622 - Perfect P-th Powers(数论)
UVA 10622 - Perfect P-th Powers 题目链接 题意:求n转化为b^p最大的p值 思路:对n分解质因子,然后取全部质因子个数的gcd就是答案,可是这题有个坑啊.就是输入的能够 ...
- UVALive 6472 Powers of Pascal
标题手段: 他给了一个无限Pascal阵,定义了powers,然后询问power为P的pascal矩阵里面的第R行C列的元素是多少. 最開始读错题意了...然后 就成了一个神得不得了的题了.后来请教的 ...
- Educational Codeforces Round 15_B. Powers of Two
B. Powers of Two time limit per test 3 seconds memory limit per test 256 megabytes input standard in ...
- The Super Powers
The Super Powers Time Limit: 1000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu [Subm ...
- Perfect Pth Powers poj1730
Perfect Pth Powers Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 16383 Accepted: 37 ...
随机推荐
- Arcgis 9.3升级Arcgis10.1需要注重的一点
在项目启动时绑定一个证书文件: 在 Global.asax里面添加 void Application_Start(object sender, EventArgs e) { // Code that ...
- listView中的button控件获取item的索引
在listview中的listitem设置事件响应,如果listitem中有button控件,这时候listitem就不会捕获到点击事件,而默认的是listitem中的button会捕获点击事件.那么 ...
- 安装SQL Server2008时 检测时有“重启计算机”失败
第一种解决方案: 在学校的时候 遇到这种问题的解决办法是: 卸载VS,先安装SQL Server 2008 再安装VS 就行了: 第二种解决方案: 如果已经安装过VS,在安装SQL Server200 ...
- winows8.1或winows7 64bit 安装Itunes 64bit 11.1.3 无法打开一直停止工作的解决办法
winows8.1或winows7 64bit 安装Itunes 64bit 11.1.3 无法打开一直停止工作的解决办法 系统环境变量里的Path追加 ;C:\program files (x86) ...
- oracle官方文档- length篇
一.首先介绍下单字节字符集和 多字节字符集 2.2字符编码方案 2.2.1 单字节编码 (1)单字节7位字符集,可以定义128个字符,最常用的字符集为 US7ASCII (2)单字节8 ...
- [转]PHP echo, print, printf, sprintf函数的区别和使用
1. echo函数: 输出函数,是命令,不能返回值.echo后面可以跟很多个参数,之间用分号隔开,如: echo $myvar1; echo 1,2,$myvar,"<b>bol ...
- 记一个问题的AC
今天突然做一道LCT的染色问题的时候突然想到一个两个月前一道没有AC的题目. 链接 大意是,给一个长度为10^4的序列,最多有255个不同的数字,有最多10^5次方个询问,对于每个询问 l,r 输出[ ...
- Mobile开发之meta篇
Mobile开发之meta篇 <meta name="viewport" content="width=device-width, initial-scale=1, ...
- Magento 编译 php5.6.21 命令
./configure '--prefix=/alidata/server/php' '--enable-opcache' '--with-config-file-path=/alidata/ser ...
- Why is 0[0] syntactically valid in javascript?
Why is 0[0] syntactically valid in javascript? 原文链接 偶然在一篇帖子中看到了这个问题,所以打算记录一下. var a = 0[0]; console. ...