CF1005C Summarize to the Power of Two 暴力 map

Posted l609929321

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CF1005C Summarize to the Power of Two 暴力 map相关的知识,希望对你有一定的参考价值。

Summarize to the Power of Two
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

A sequence a1,a2,,ana1,a2,…,an is called good if, for each element aiai, there exists an element ajaj (iji≠j) such that ai+ajai+aj is a power of two (that is, 2d2d for some non-negative integer dd).

For example, the following sequences are good:

  • [5,3,11][5,3,11] (for example, for a1=5a1=5 we can choose a2=3a2=3. Note that their sum is a power of two. Similarly, such an element can be found for a2a2 and a3a3),
  • [1,1,1,1023][1,1,1,1023],
  • [7,39,89,25,89][7,39,89,25,89],
  • [][].

Note that, by definition, an empty sequence (with a length of 00) is good.

For example, the following sequences are not good:

  • [16][16] (for a1=16a1=16, it is impossible to find another element ajaj such that their sum is a power of two),
  • [4,16][4,16] (for a1=4a1=4, it is impossible to find another element ajaj such that their sum is a power of two),
  • [1,3,2,8,8,8][1,3,2,8,8,8] (for a3=2a3=2, it is impossible to find another element ajaj such that their sum is a power of two).

You are given a sequence a1,a2,,ana1,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 nn (1n1200001≤n≤120000) — the length of the given sequence.

The second line contains the sequence of integers a1,a2,,ana1,a2,…,an (1ai1091≤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 nn 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=5a4=5. The remaining elements form the sequence [4,7,1,4,9][4,7,1,4,9], which is good.

 

 题意:给你n个数,问去掉几个数后,每个数都被用上与另一个数和为2的幂数

分析:直接暴力枚举每个二的幂数减去当前数的差是否存在于这个数列中,用map存下每个数

#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
#define debug(a) cout << #a << " " << a << endl
using namespace std;
const int maxn = 2e5 + 10;
const int mod = 1e9 + 7;
typedef long long ll;
ll a[maxn], vis[maxn];
int main() {
    ll n;
    while( cin >> n ) {
        map<ll,ll> mm;
        for( ll i = 0; i < n; i ++ ) {
            cin >> a[i];
            mm[a[i]] ++;
        }
        ll cnt = 0;
        for( ll i = 0; i < n; i ++ ) {
            bool flag = false;
            for( ll j = 1<<30; j >= 1; j /=2 ) {
                if( j > a[i] ) {
                    if( ( a[i] == j/2 && mm[j-a[i]] > 1 ) || ( mm[j-a[i]] > 0 && a[i] != j/2 ) ) {
                        flag = true;
                        break;
                    }
                }
            }
            if( !flag ) {
                //debug(a[i]), debug(i);
                cnt ++;
            }
        }
        cout << cnt << endl;
    }
    return 0;
}

 





以上是关于CF1005C Summarize to the Power of Two 暴力 map的主要内容,如果未能解决你的问题,请参考以下文章

Summarize to the Power of Two(map+思维)

[CF1065F]Up and Down the Tree

并不对劲的CF1239B&C&D Programming Task in the Train to Catowice City

R语言使用skimr包的skim函数查看dataframe特定数据列的summary信息统计汇总信息(Select specific columns to summarize)

部分词汇

CF1037D Valid BFS?