Codeforces 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 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
4
7 3 2 1
Output
2
Input
3
1 1 1
Output
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.

 

题目链接:http://codeforces.com/contest/702/problem/B

 

2^32是大于1e9的,n^2的复杂度,内层用2的幂次单独判断,用map处理每个数出现的次数。最后+1,开始时候的判断只会判断已经加入map中的数,所以不会出现重复的情况。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <map>
 5 using namespace std;
 6 map<long long, long long>m;
 7 int main()
 8 {
 9     int n;
10     cin >> n;
11     long long t;
12     long long ans=0;
13     for(int i=0;i<n;i++)
14     {
15         cin >> t;
16         for(int j = 0; j < 32; j++)
17         {
18             ans+=m[(2<<j)-t];
19         }
20         m[t]++;
21      } 
22     cout << ans;
23     return 0;
24 }
查看代码

 

转载于:https://www.cnblogs.com/Mino521/p/5721996.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值