51nod 2级算法题-1315
来源:互联网 发布:windows 7 字体大小 编辑:程序博客网 时间:2024/06/06 07:05
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.
Input
第一行两个整数N,X,其中N为Y集合元素个数,X如题所述,且1<=N<=50,1<=X<=1,000,000,000.之后N行,每行一个整数yi,即集合Y中的第i个元素,且1<=yi<=1,000,000,000.
Output
一个整数,表示最少删除多少个元素。
Input示例
5 712478
Output示例
2
思路:
注意到or 操作的性质,第一,1会累积下来;其次,如果a>b 那么不管对a 怎么进行or 操作都不会得到b。只要保证对于x 的某个位置上的1 无法在子序列中得到满足,那就就无法构成x。所以只要求出在小于x的所有元素中 对应的1的个数最少的量。
#include <iostream>#include <cstring>#include <string>#include <cstdio>#include <cstdlib>#include <cmath>#include <algorithm>#include <queue>#include <stack>using namespace std;#define endl "\n"const int maxn = 55;int bx[maxn];//Xint t[maxn];int by[maxn];int main (){ std::ios::sync_with_stdio(false); memset(bx,0,sizeof(bx)); memset(t,0,sizeof(t)); int n; long long X; cin>>n>>X; long long cnt=X; int i=0; while(cnt){ bx[i++]=cnt%2; cnt/=2; } long long y; for(int j=0;j<n;j++){ cin>>y; if((y|X)!=X){ continue; } cnt=y; i=0; while(cnt){ by[i]=cnt%2; if( by[i]==bx[i] && bx[i]==1){ t[i]++; } i++; cnt/=2; } } int ans=maxn; for(int i=0;i<maxn;i++){ if(bx[i]>0 && ans>t[i]){ ans=t[i]; } } if(ans==maxn){ cout<<0<<endl; }else{ cout<<ans<<endl; } return 0;}
0 0
- 51nod 2级算法题-1315
- 51nod 2级算法题-1007
- 51nod 2级算法题-1010
- 51nod 2级算法题-1014
- 51nod 2级算法题-1050
- 51nod 2级算法题-1062
- 51nod 2级算法题-1092
- 51nod 2级算法题-1095
- 51nod 2级算法题-1119
- 51nod 2级算法题-1126
- 51nod 2级算法题-1133
- 51nod 2级算法题-1279
- 51nod 2级算法题-1428
- 【模拟】1432 独木舟【51nod】【难度:2级算法题】
- 51nod 1级算法题
- 51nod 3级算法题-1065
- 51nod 5级算法题
- 【贪心+优先队列】1428 活动安排问题【51nod】【难度:2级算法题】
- layer.js弹出其他页面源代码
- \r,\n,\r\n的区别
- 逻辑回归学习--转
- 我的博客
- JavaScript 1.10 RegExp的方法和属性
- 51nod 2级算法题-1315
- MyBatis学习(1):Mybatis使用详解和入门案例
- group by
- Java调用命令截取图片
- Android自定义View-刮刮卡效果
- 机器学习-周志华-个人练习4.3
- 正则表达式
- SQL查询--列出所有员工的姓名及其直接上级的姓名
- Android读SD卡文件