CodeForces - 702B Powers of Two

20人阅读 评论(0) 收藏 举报
分类:
B. Powers of Two
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
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).

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.

Examples
input
Copy
4
7 3 2 1
output
Copy
2
input
Copy
3
1 1 1
output
Copy
3
Note

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.

二分模拟

#include<iostream>
#include<algorithm>
#include<stdio.h>
using namespace std;
long long a[100050];
long long d[32];
int main()
{
	ios::sync_with_stdio(false);
	int n;
	cin>>n;
	int ans=1;
	for(int i=0;i<31;i++) {
		d[i]=ans;
		ans*=2;
	}
	long long maxn=0;
	for(int i=0;i<n;i++) {cin>>a[i];
	maxn=max(maxn,a[i]);
	}
	sort(a,a+n);
	long long ssd=0;
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<31;j++)
		{
			int p=d[j]-a[i];
			if(p>maxn) break;
			if(p<0) continue;
			long long l=i+1,r=n-1;
			long long id=-1;
		    int tp1 = lower_bound(a+i+1, a+n, p)-a;
            int tp2 = upper_bound(a+i+1, a+n, p)-a;
             if(tp1 == n)
              continue;
            int tmp = tp2 - tp1;
              ssd += tmp;
		}
	}
	cout<<ssd<<endl;
	return 0;
}

查看评论

彻底掌握 Javascript(十九)Iterator 迭代器【专家辅导】

-
  • 1970年01月01日 08:00

codeforces 702B - Powers of Two

You are given n integers a1, a2, ..., an. Find the number of pairs of indexes i, j (i j) that ai...
  • ShiWaiGaoRen12345
  • ShiWaiGaoRen12345
  • 2016-08-20 20:16:40
  • 184

CodeForces 305C Ivan and Powers of Two【set】

这个题啊,纠结了本公举一中午,知道如果出现了重复的,就要往后移,差点就把自己想的恶心的bool 数组付诸实现了,== 既然是思维的题,借助了STL的特性是再正常不过的了,既然想写数组,先应该想想Se...
  • zhou_yujia
  • zhou_yujia
  • 2015-12-30 15:34:28
  • 254

Codeforces 702B - Powers of Two

B. Powers of Two time limit per test 3 seconds memory limit per test 256 megabytes input stand...
  • nare123
  • nare123
  • 2016-07-30 12:47:19
  • 394

CodeForces 702B Powers of Two

2的幂只有那么多种情况
  • nameofcsdn
  • nameofcsdn
  • 2016-08-02 14:27:09
  • 1298

codeforces 305 C. Ivan and Powers of Two

题目链接       给出一个非减序的数组a[n], 然后得到s=2^a1+.……+2^an, 要使s为2^v -1,需要在数组中添加几个数。       我的思路是这样的,由2^a+2^a = ...
  • xindoo
  • xindoo
  • 2013-06-05 17:26:14
  • 766

CodeForces 702B Powers of Two(二分)

B. Powers of Two You are given n integers a1, a2, ..., an. Find the number of pairs of indexes...
  • qq_26658823
  • qq_26658823
  • 2016-08-02 21:32:15
  • 283

【CodeForces】702B - Powers of Two(二分)

点击打开题目 B. Powers of Two time limit per test 3 seconds memory limit per test 256 m...
  • wyg1997
  • wyg1997
  • 2016-08-06 09:04:37
  • 210

CodeForces 702B - Powers of Two(二分)

http://codeforces.com/problemset/problem/702/B题目链接You are given n integers a1, a2, ..., an. Find the...
  • zzzanj
  • zzzanj
  • 2018-04-15 23:05:05
  • 7

Codeforces 955C Sad powers 基础数论+二分

传送门:Sad powers分析: 先固定指数p,x^p &amp;lt;= 1e18 可以推导出 x &amp;lt;= 10^(18/p) [取对数推导]若 p == 2, 则有 10^9 那么多...
  • computer_user
  • computer_user
  • 2018-03-27 16:49:30
  • 189
    个人资料
    持之以恒
    等级:
    访问量: 524
    积分: 151
    排名: 110万+
    最新评论