CF Edu 15 B 求和二的幂次
2016-07-30 11:28
387 查看
题目连接
http://codeforces.com/contest/702/problem/BDescription
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 = 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.Sample Input
47 3 2 1
Sample Output
2题意
给你一堆数,问在这堆数中两两组合,使他们的和是二的幂次的最多的组合数题解
开始我就想直接每个数依次和其他数求和然后用(x&x-1)判断是否是二的幂次,发现T了…果断就不知道怎么优化了QAQ…看了题解,发现求组合的时候没有一个一个挨个去求,2^32是大于1e9的,n^2的复杂度,内层用2的幂次单独判断,用map处理每个数出现的次数。最后+1,那开始时候的判断就只会判断已经加入map中的数,而不会出现一个数与其他所有数比较的情况。还有就是左移右移又记反了,x<
代码
#include<bits/stdc++.h> using namespace std; //long long a[100005]; bool fun(long long v) { bool flag = 0; if((v>0)&&(v&(v-1))==0) flag = 1; return flag; } map<long long ,long long>s; int main() { int n; scanf("%d",&n); long long ans=0; for(int i=0;i<n;i++) { // scanf("%I64d",&a[i]); long long t; scanf("%I64d",&t); for(int j=0;j<32;j++) { ans+=s[(2<<j)-t]; } s[t]++; } printf("%I64d",ans); return 0; }
相关文章推荐
- Assign directions to edges so that the directed graph remains acyclic
- Convolutional neural networks(CNN) (三) Sparse Autoencoder Exercise(Vectorization)
- 关于ios应用上线打包
- HDU 2476 String painter(动态规划)★
- POJ-1860-Currency Exchange
- Oracle中添加新用户并赋予权限
- 为什么资源文件名以类型开头
- JDBC-Result/Sets
- Spark1.5堆内存分配
- Query简单的一个小动画
- Ubuntu16.04TLS系统中chromium无法加载flash插件的问题
- C语言结构体结合malloc、free使用小例子
- 关于PhotoShop工具栏失灵
- Mean shift 算法
- RHEL/Centos7 安装图形化桌面
- Ubuntu更改MySQL字符集UTF-8,正确显示中文
- iOS 自适应label行高
- 李一男2003年在港湾给开发人员培训时的语录
- 近期虚拟机相关问题总结
- iOS开发UI基础—手写控件,frame,center和bounds属性