合法整数集(51Nod-1315)

题目

一个整数集合S是合法的,指S的任意子集subS有Fun(SubS)!=X,其中X是一个固定整数,Fun(A)的定义如下:
A为一个整数集合,设A中有n个元素,分别为a0,a1,a2,...,an-1,那么定义:Fun(A)=a0 or a1 or ... or an-1;Fun({}) = 0,即空集的函数值为0.其中,or为或操作。
现在给你一个集合Y与整数X的值,问在集合Y至少删除多少个元素能使集合Y合法?

例如:Y = {1,2,4},X=7;显然现在的Y不合法,因为 1 or 2 or 4 = 7,但是删除掉任何一个元素后Y将合法。所以,答案是1.

输入

第一行两个整数N,X,其中N为Y集合元素个数,X如题所述,且1<=N<=50,1<=X<=1,000,000,000.
之后N行,每行一个整数yi,即集合Y中的第i个元素,且1<=yi<=1,000,000,000.

输出

一个整数,表示最少删除多少个元素。

输入样例

5 7
1
2
4
7
8

输出样例

2

思路:

为了让集合的所有子集不等于所给的值,所以要去算这个原集合会对这个数二进制所在位的贡献

如果一个数|所给的数大于所给数时,由于其|任何数不会等于所给数,那么这个数是不需要删除的

之后计算所有 (x|y)<=y 的数,然后选择最小的那个

源程序

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#define EPS 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
const int MOD = 1E9+7;
const int N = 1000000+5;
const int dx[] = {-1,1,0,0};
const int dy[] = {0,0,-1,1};
using namespace std;


LL a[N];
int num[N];
int judge(LL n,LL x) {
    while(n) {
        if(x%2==0&&n%2==1)
            return 1;

        n=n/2;
        x=x/2;
    }
    return 0;
}
void calculate(LL n) {
    for(int i=1; i<=35&&n!=0; i++) {
        if(n%2==1)
            num[i]++;
        n/=2;
    }
}
int main() {
    int n;
    LL x;
    scanf("%d%lld",&n,&x);
    int sum=0;
    for(int i=1; i<=n; i++) {
        scanf("%lld",&a[i]);
        sum^=a[i];
        if(a[i]<=x&&judge(a[i],x)==0)
            calculate(a[i]);
    }
    int res=INF;
    for(int i=1; i<=35; i++) {
        if(x%2==1)
            res=min(res,num[i]);
        x/=2;
    }

    printf("%d\n",res);
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值