• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
ACM s1124yy
守りたいものが 強くさせること
博客园    首页    新随笔    联系   管理     

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;
}
posted @ 2016-07-30 10:58  s1124yy  阅读(159)  评论(1)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3