[Codeforces 1013B] And

Posted evenbao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Codeforces 1013B] And相关的知识,希望对你有一定的参考价值。

[题目链接]

          http://codeforces.com/problemset/problem/1013/B

[算法]

        不难发现,答案只有0,1,2,-1,共4种取值

        分类讨论即可,计算时可以使用STL-map

[代码]

        

#include<bits/stdc++.h>
using namespace std;
#define MAXN 100010

int i,n,x;
int a[MAXN];
map<int,int> mp;

int main() 
{
        
        scanf("%d%d",&n,&x);
        for (i = 1; i <= n; i++) 
        {
                scanf("%d",&a[i]);
                mp[a[i]]++;
        }
        for (i = 1; i <= n; i++)
        {
                if (mp[a[i]] >= 2)
                {
                        printf("0
");
                        return 0;
                }
        }
        for (i = 1; i <= n; i++)
        {
                if ((a[i] & x) == a[i]) continue;
                if (mp[a[i] & x] >= 1)
                {
                        printf("1
");
                        return 0;
                }
        }
        mp.clear();
        for (i = 1; i <= n; i++) a[i] &= x;
        for (i = 1; i <= n; i++) mp[a[i]]++;
        for (i = 1; i <= n; i++)
        {
                if (mp[a[i]] >= 2)
                {
                        printf("2
");
                        return 0;
                }
        }
        printf("-1
");
        
        return 0;
    
}

 

以上是关于[Codeforces 1013B] And的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces Round #437 (Div. 2, based on MemSQL Start[c]UP 3.0 - Round 2) E. Buy Low Sell High(代码片

[Vue + TS] Use Dependency Injection in Vue Using @Inject and @Provide Decorators with TypeScript(代码片

[原创][Synth 8-2543] port connections cannot be mixed ordered and named ["*_Top.v":1151](代码片

LeetCode-面试算法经典-Java实现106-Construct Binary Tree from Inorder and Postorder Traversal(构造二叉树II)(示例(代码片

codeforces 7 A. Kalevitch and Chess

[Codeforces 519B] A and B and Compilation Errors