Codeforces1005C——Summarize to the Power of Two

A sequence a1,a2,…,an is called good if, for each element ai, there exists an element aj (i≠j) such that ai+aj is a power of two (that is, 2d for some non-negative integer d).
For example, the following sequences are good:
[5,3,11] (for example, for a1=5 we can choose a2=3. Note that their sum is a power of two. Similarly, such an element can be found for a2 and a3),
[1,1,1,1023],
[7,39,89,25,89],
[].
Note that, by definition, an empty sequence (with a length of 0) is good.
For example, the following sequences are not good:
[16] (for a1=16, it is impossible to find another element aj such that their sum is a power of two),
[4,16] (for a1=4, it is impossible to find another element aj such that their sum is a power of two),
[1,3,2,8,8,8] (for a3=2, it is impossible to find another element aj such that their sum is a power of two).
You are given a sequence a1,a2,…,an. What is the minimum number of elements you need to remove to make it good? You can delete an arbitrary set of elements.
Input
The first line contains the integer n (1≤n≤120000) — the length of the given sequence.
The second line contains the sequence of integers a1,a2,…,an (1≤ai≤109).
Output
Print the minimum number of elements needed to be removed from the given sequence in order to make it good. It is possible that you need to delete all n elements, make it empty, and thus get a good sequence.
Examples
Input
6
4 7 1 5 4 9
Output
1
Input
5
1 2 3 4 5
Output
2
Input
1
16
Output
1
Input
4
1 1 1 1023
Output
0

跟leetcode里找两个数的和那几道题差不多 要用到map 如果暴力遍历就超时了

代码:

#include <cstdio>
#include <algorithm>
#include <set>
#include <map>
using namespace std;
int n;
const int MAXN=120050;
int a[MAXN];
int vis[MAXN];
long long p[]={
  1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768,65536,131072,262144,524288,1048576,2097152,4194304,8388608,16777216,33554432,67108864,134217728,268435456,536870912,1073741824,2147483648

};
map<long long,long long> mp;
int main(void){
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%d",&a[i]);
        mp[a[i]]++;
    }
    sort(a,a+n);
    for(int i=0;i<n;i++){
        if(vis[i]){
            continue;
        }
        for(int j=31;j>=0;j--){
            int t=p[j]-a[i];
            if(mp[t]>0){
                //find
                if(t==a[i]){
                    if(mp[t]!=1){
                        vis[i]=1;
                    }
                }
                else{
                    vis[i]=1;
                }
            }
        }
    }
    int ans=0;
    for(int i=0;i<n;i++){
        if(vis[i]==0){
            //printf("%d\n",i);
            ans++;
        }
    }
    printf("%d\n",ans);
    return 0;
}
全部评论

相关推荐

03-12 15:35
嘉应学院 Python
快说谢谢牛牛精灵:说不定就是下一个寒武纪!
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
正在热议
更多
# 春招至今,你的战绩如何? #
12178次浏览 107人参与
# 你的实习产出是真实的还是包装的? #
2117次浏览 43人参与
# 巨人网络春招 #
11410次浏览 223人参与
# 军工所铁饭碗 vs 互联网高薪资,你会选谁 #
7781次浏览 43人参与
# 简历第一个项目做什么 #
31855次浏览 345人参与
# 重来一次,我还会选择这个专业吗 #
433680次浏览 3926人参与
# 米连集团26产品管培生项目 #
6385次浏览 216人参与
# 当下环境,你会继续卷互联网,还是看其他行业机会 #
187363次浏览 1122人参与
# 牛客AI文生图 #
21472次浏览 238人参与
# 不考虑薪资和职业,你最想做什么工作呢? #
152600次浏览 888人参与
# 研究所笔面经互助 #
119000次浏览 577人参与
# 简历中的项目经历要怎么写? #
310590次浏览 4230人参与
# AI时代,哪些岗位最容易被淘汰 #
64117次浏览 839人参与
# 面试紧张时你会有什么表现? #
30533次浏览 188人参与
# 你今年的平均薪资是多少? #
213296次浏览 1039人参与
# 你怎么看待AI面试 #
180364次浏览 1268人参与
# 高学历就一定能找到好工作吗? #
64352次浏览 620人参与
# 你最满意的offer薪资是哪家公司? #
76697次浏览 374人参与
# 我的求职精神状态 #
448246次浏览 3129人参与
# 正在春招的你,也参与了去年秋招吗? #
363811次浏览 2638人参与
# 腾讯音乐求职进展汇总 #
160736次浏览 1114人参与
# 校招笔试 #
471781次浏览 2964人参与
牛客网
牛客网在线编程
牛客网题解
牛客企业服务