题目链接: 传送门

Devu and Partitioning of the Array

time limit per test:3 second     memory limit per test:256 megabytes

Description

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 = 2^x).

Input

The first line contains the single positive integer n (1 ≤ n ≤ 10^5) — the number of integers.
The second line contains n positive integers a1, a2, ..., an (1 ≤ ai ≤ 10^9).

Output

Print the number of pairs of indexes i, j (i < j) that ai + aj is a power of 2.

Sample Input

4
7 3 2 1

3
1 1 1

Sample Output

2

3

解题思路

注意到2^31次方就能达到10^9级别,所以枚举的时候枚举2的幂次方而不是枚举N,枚举N果断超时。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<map>
using namespace std;
typedef __int64 LL;
int main()
{
    int N;
    while (~scanf("%d",&N))
    {
        LL tmp,res = 0;
        map<LL,LL>mp;
        for (int i = 0;i < N;i++)
        {
            scanf("%I64d",&tmp);
            for (int j = 1;j < 32;j++)
            {
                if (mp.find((1<<j)-tmp) != mp.end())
                {
                    res += mp[(1<<j)-tmp];
                }
            }
            mp[tmp]++;
        }
        printf("%I64d\n",res);
    }
    return 0;
}

CF 702B Powers of Two(暴力)的更多相关文章

  1. CodeForces 702B Powers of Two (暴力,优化)

    题意:给定 n 个数,问你从有多少下标 i < j,并且 ai + aj 是2的倍数. 析:方法一: 从输入开始暴力,因为 i < j 和 i > j 是一样,所以可以从前面就开始查 ...

  2. CF 1095C Powers Of Two(二进制拆分)

    A positive integer xx is called a power of two if it can be represented as x=2y, where y is a non-ne ...

  3. CodeForces 702B Powers of Two【二分/lower_bound找多少个数/给出一个数组 求出ai + aj等于2的幂的数对个数】

    B. Powers of Two   You are given n integers a1, a2, ..., an. Find the number of pairs of indexes i,  ...

  4. CF 1095C Powers Of Two

    题目连接:http://codeforces.com/problemset/problem/1095/C 题目: C. Powers Of Two time limit per test 4 seco ...

  5. cf 702B

    You are given n integers a1, a2, ..., an. Find the number of pairs of indexes i, j (i < j) that a ...

  6. CodeForces 702B Powers of Two

    简单题. 开一个$map$记录一下每个数字出现了几次,那么读入的时候$f[a[i]]+1$. 计算$a[i]$做出的贡献的时候,先把$f[a[i]]-1$,然后再枚举$x$,答案加上$f[{2^x} ...

  7. CF 305A——Strange Addition——————【暴力加技巧】

    A. Strange Addition time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  8. CF 568A(Primes or Palindromes?-暴力推断)

    A. Primes or Palindromes? time limit per test 3 seconds memory limit per test 256 megabytes input st ...

  9. 洛谷P4117 [Ynoi2018]五彩斑斓的世界 [分块,并查集]

    洛谷 Codeforces 又是一道卡常题-- 思路 YNOI当然要分块啦. 分块之后怎么办? 零散块暴力,整块怎么办? 显然不能暴力改/查询所有的.考虑把相同值的用并查集连在一起,这样修改时就只需要 ...

随机推荐

  1. HTML5+JS 《五子飞》游戏实现(二)路线分析和资源准备

    上一节 里沃特与我们分享了<五子飞>的下棋规则,可能有些伙伴看得不清楚,像我们码农还是看到代码比较靠谱.下面就把可以走棋的路线跟大家说一下. 假设从左上角开始,以0开始编号,往右数(没看第 ...

  2. 为Visual Studio更换皮肤和背景图

    最近在博客上别人在老早之前就有加了背景图,想想怎么能这么out,于是乎,百度一下,能否知道? 咳咳,资源还是有滴!可惜都是比较旧的跟新版本的配置方法可能有点不一样, 可能会有很多人会觉得不适应,不过没 ...

  3. JSON返回DateTime/Date('123123123')/解决办法

    Date.prototype.format = function (format) //author: meizz    {        var o = {            "M+& ...

  4. nios II--实验5——定时器软件部分

    软件开发 首先,在硬件工程文件夹里面新建一个software的文件夹用于放置软件部分:打开toolsàNios II 11.0 Software Build Tools for Eclipse,需要进 ...

  5. [ZOJ2760]How Many Shortest Path(floyd+最大流)

    题目:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1760 题意:给你一个一个n*n(n<=100)的有向图,问你从s到 ...

  6. 【JavaEE企业应用实战学习记录】getConnListener

    Listener:当Web应用在Web容器中运行时,Web应用内部会不断地发生各种事件,如Web应用被启动.Web应用被停止,用户Session开始,用户session结束.用户请求到达等,这些对We ...

  7. 一次神奇的WCF的404错误解决

    现象:浏览器中可以访问元数据,但是运行的时候却报404的异常,说目标地址找不到. 折腾了一下午. 引用服务后config中的client的address是这样的http://host/aspx/Ser ...

  8. 十天冲刺---Day7

    站立式会议 站立式会议内容总结: 燃尽图 照片 两个人编码其实效率挺高的.但是在一些方面,比如说页面UI的编写,会非常吃力,很难达到自己的效果. 由于埋头在编码,所以issues的增加随之停止. 有点 ...

  9. Value cannot be null or empty. 参数名: contentPath

    代码:<img src="@Url.Content(item.ThumbPath)" width="160" height="250" ...

  10. Java 构建器

    假如我们的一个实体类有很多的属性值,但是这些属性值又是可选的.如果我们遇到这样的是类,如何设计出方便的实体类呢? 通常解决办法一: 重叠构造器 public class User { private ...