题目链接: 传送门

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. ios蓝牙开发(五)BabyBluetooth蓝牙库介绍

    BabyBluetooth 是一个最简单易用的蓝牙库,基于CoreBluetooth的封装,并兼容ios和mac osx. 特色: 基于原生CoreBluetooth框架封装的轻量级的开源库,可以帮你 ...

  2. Java 的世界,我不懂:奇葩的 json 序列化

    先上张图,代表我心中的十万头草泥马: 写这么长的代码,头回见数组和单个实体共用同一个 json 节点的! 恐怕只有 java 社区的大牛B 才能做出这等事.. 由 Apache 发布: http:// ...

  3. .net程序员转行做手游开发经历(二)

    上篇主要介绍自己个人的经历,这篇主要讲下学习新语言的过程. 上次说到最终选择的语言是swift,框架用spritekit,上次有网友对为什么选择用这俩呢,为什么不用cocos和unity呢,cocos ...

  4. nios II--实验7——数码管IP软件部分

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

  5. python列表下标用法

    python中的列表下标实在太灵活了,要根据表象来分析它的内在机理,这样用起来才能溜.下标可以为负数有利有弊,好处是使用起来更简便,坏处是当我下表越界了我也不知道反倒发生奇奇怪怪的错误. print ...

  6. 1118sync_binlog innodb_flush_log_at_trx_commit 浅析

    转自 http://blog.itpub.net/22664653/viewspace-1063134/  innodb_flush_log_at_trx_commit和sync_binlog 两个参 ...

  7. Java--笔记(5)

    41.面向对象的五大基本原则 (1)单一职责原则(SRP) (2)开放封闭原则(OCP) (3)里氏替换原则(LSP) (4)依赖倒置原则(DIP) (5)接口隔离原则(ISP) 单一职责原则(SRP ...

  8. Java学习之ConcurrentHashMap实现一个本地缓存

    ConcurrentHashMap融合了Hashtable和HashMap二者的优势. Hashtable是做了线程同步,HashMap未考虑同步.所以HashMap在单线程下效率较高,Hashtab ...

  9. idea 新建web项目

  10. sql-truncate,delete,drop删除表数据的区别

    TRUNCATE TABLE 删除表中的所有行,而不记录单个行删除操作. 语法 TRUNCATE TABLE name 参数 name 是要截断的表的名称或要删除其全部行的表的名称. 注释 TRUNC ...