codeforces 702B 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 xexists 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

题意:
问有多少对a[i]+a[j]是2的次幂;

思路:

map搞一搞;

AC代码:

/************************************************ 
┆  ┏┓   ┏┓ ┆    
┆┏┛┻━━━┛┻┓ ┆ 
┆┃       ┃ ┆ 
┆┃   ━   ┃ ┆ 
┆┃ ┳┛ ┗┳ ┃ ┆ 
┆┃       ┃ ┆  
┆┃   ┻   ┃ ┆ 
┆┗━┓    ┏━┛ ┆ 
┆  ┃    ┃  ┆       
┆  ┃    ┗━━━┓ ┆ 
┆  ┃  AC代马   ┣┓┆ 
┆  ┃           ┏┛┆ 
┆  ┗┓┓┏━┳┓┏┛ ┆ 
┆   ┃┫┫ ┃┫┫ ┆ 
┆   ┗┻┛ ┗┻┛ ┆       
************************************************ */  


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <bits/stdc++.h>
#include <stack>

using namespace std;

#define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss));

typedef  long long LL;

template<class T> void read(T&num) {
    char CH; bool F=false;
    for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
    for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
    F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
    if(!p) { puts("0"); return; }
    while(p) stk[++ tp] = p%10, p/=10;
    while(tp) putchar(stk[tp--] + '0');
    putchar('
');
}

const LL mod=1e9+7;
const double PI=acos(-1.0);
const int inf=1e9;
const int N=1e5+10;
const int maxn=(1<<8);
const double eps=1e-8;

int a[N],cnt=0,b[50];

map<int,int>mp,vis;

inline void Init()
{
    LL temp=2;
    while(temp<=2e9+100)
    {
        b[++cnt]=(int)temp;
        temp=temp*2;
    }
}
int main()
{       Init();
        int n;
        read(n);
        For(i,1,n)read(a[i]),mp[a[i]]++,vis[a[i]]=1;
        LL ans=0;
        For(i,1,n)
        {
            For(j,1,cnt)
            {
                if(vis[b[j]-a[i]])
                {
                    int x=b[j]-a[i];
                    if(x==a[i])
                    {
                        ans=ans+mp[x]-1;
                    }
                    else 
                    {
                        ans=ans+mp[x];
                    }
                }
            }
        }
        cout<<ans/2<<endl;
        
        return 0;
}

  

原文地址:https://www.cnblogs.com/zhangchengc919/p/5720273.html