码迷,mamicode.com
首页 > 其他好文 > 详细

[Codeforces 1013B] And

时间:2018-07-30 23:32:09      阅读:318      评论:0      收藏:0      [点我收藏+]

标签:for   code   c++   com   scanf   space   tin   scan   ace   

[题目链接]

          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\n");
                        return 0;
                }
        }
        for (i = 1; i <= n; i++)
        {
                if ((a[i] & x) == a[i]) continue;
                if (mp[a[i] & x] >= 1)
                {
                        printf("1\n");
                        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\n");
                        return 0;
                }
        }
        printf("-1\n");
        
        return 0;
    
}

 

[Codeforces 1013B] And

标签:for   code   c++   com   scanf   space   tin   scan   ace   

原文地址:https://www.cnblogs.com/evenbao/p/9393366.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!