zoukankan      html  css  js  c++  java
  • CF 1005C Summarize to the Power of Two 【hash/STL-map】

    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
    Note
    In the first example, it is enough to delete one element a4=5. The remaining elements form the sequence [4,7,1,4,9], which is good.

    #include<cstdio>
    #include<string>
    #include<cstdlib>
    #include<cmath>
    #include<iostream>
    #include<cstring>
    #include<set>
    #include<queue>
    #include<algorithm>
    #include<vector>
    #include<map>
    #include<cctype>
    #include<stack>
    #include<sstream>
    #include<list>
    #include<assert.h>
    #include<bitset>
    #include<numeric>
    #define debug() puts("++++")
    #define gcd(a,b) __gcd(a,b)
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define fi first
    #define se second
    #define pb push_back
    #define sqr(x) ((x)*(x))
    #define ms(a,b) memset(a,b,sizeof(a))
    #define sz size()
    #define be begin()
    #define pu push_up
    #define pd push_down
    #define cl clear()
    #define lowbit(x) -x&x
    #define all 1,n,1
    #define rep(i,x,n) for(int i=(x); i<(n); i++)
    #define in freopen("in.in","r",stdin)
    #define out freopen("out.out","w",stdout)
    using namespace std;
    typedef long long ll;
    typedef unsigned long long ULL;
    typedef pair<int,int> P;
    const int INF = 0x3f3f3f3f;
    const ll LNF = 1e18;
    const int N = 1e3 + 20;
    const int maxm = 1e6 + 10;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    const int dx[] = {-1,1,0,0,1,1,-1,-1};
    const int dy[] = {0,0,1,-1,1,-1,1,-1};
    int dir[4][2] = {{0,1},{0,-1},{-1,0},{1,0}};
    const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    int n;
    int a[maxm];
    map<int,int> m;
    /*
    */
    int main()
    {
        while(cin>>n)
        {
            int f,c=0;
            m.clear();
            ms(a,0);
    
            rep(i,0,n)
            {
                scanf("%d",&a[i]);
                m[a[i]]++;
            }
            rep(i,0,n)
            {
                f=0;
                rep(j,0,31)
                {
                    int t = 1<<j;
                    int tt = t - a[i];
                    if(m[tt])
                    {
                        if(t - a[i] == a[i]) //找到相同的比如4 4/8 8
                        {
                            if(m[tt] > 1) //相同的不止有一个
                            {
                                f = 1;
                                break;
                            }
                        }
                        else
                        {
                            f = 1;
                            break;
                        }
                    }
                }
                if(!f) c++;
            }
            cout<<c<<endl;
        }
    }
    /*
    1 2 4 8 16 32 64
    1 3 5
    2 4
    
    4 7 1 5 4 9
    1 4 4 5 7 9
    
    */
    
    
  • 相关阅读:
    RMAN主要命令 show,list,crosscheck,delete详解
    CentOS6.2上安装Oracle10g报ins_emdb.mk错误处理方法
    CentOS 6.3(x86_32)下安装Oracle 10g R2
    关于约束ENABLE NOVALIDATE的一个疑问
    mongodb drop不释放磁盘空间
    Drop a database in MongoDB
    linux 中使用iptables 防止ddocs及cc攻击配置 。
    MySQL用Load Data local infile 导入部分数据后中文乱码
    微信小程序开发:学习笔记[7]——理解小程序的宿主环境
    软考解析:2016年下半年下午试卷
  • 原文地址:https://www.cnblogs.com/Roni-i/p/9348164.html
Copyright © 2011-2022 走看看