Educational Codeforces Round 15 Powers of Two

Powers of Two

题意:

让求ai+aj=2的x次幂的数有几对,且i < j。

题解:

首先要知道,排完序对答案是没有影响的,比如样例7 1一对,和1 7一对是样的,所以就可以排序之后二分找2的x次幂相减的那个数就好了,注意:打表时2的x次幂不能只小于1e9,因为有可能是2个5e8相加,之后就超出了1e9,但是你打表的时候又没有超出1e9的那个2的x次幂,所以答案总是会少几个。所以尽量就开ll 能多打表就多打。

代码:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const int INF=0x3f3f3f3f;
const ll LINF=0x3f3f3f3f3f3f3f3f;
#define PU puts("");
#define PI(A) cout<<A<<endl
#define SI(N) cin>>N
#define SII(N,M) cin>>N>>M
#define cle(a,val) memset(a,(val),sizeof(a))
#define rep(i,b) for(int i=0;i<(b);i++)
#define Rep(i,a,b) for(int i=(a);i<=(b);i++)
#define reRep(i,a,b) for(int i=(a);i>=(b);i--)
#define dbg(x) cout <<#x<<" = "<<x<<endl
#define PIar(a,n) rep(i,n)cout<<a[i]<<" ";cout<<endl;
#define PIarr(a,n,m) rep(aa,n){rep(bb, m)cout<<a[aa][bb]<<" ";cout<<endl;}
const double EPS= 1e-9 ;

/*  /////////////////////////     C o d i n g  S p a c e     /////////////////////////  */

const int MAXN= 100000 + 9 ;

int a[MAXN];
int N;


int main()
{

    iostream::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    ll pow2[50]={1};
    int cnt=1;
    Rep(i,1,35)
    {
        pow2[cnt++]=2*pow2[i-1];
    }
    while(SI(N))
    {
        rep(i,N) SI(a[i]);
        sort(a,a+N);
        ll ans=0;
        rep(i,N-1)
        {
            Rep(j,0,35)
            {
                ll d=pow2[j]-a[i];
                if (d<=0) continue;
                ll num=lower_bound(a+i+1,a+N,d+1)-lower_bound(a+i+1,a+N,d);
                ans+=num;
            }
        }
        PI(ans);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/s1124yy/p/5720483.html