2026 csp-j T3 [CSP-J 2026] 按位与 / and(民间数据)

https://www.luogu.com.cn/problem/P17829?contestId=369911

题目描述
小 R 有 n 个非负整数 a1,a2,…,an。
小 X 给了小 R 一个正整数 m,并要求小 R 从这 n 个非负整数中选出恰好 m 个,使得选出的 m 个数的二进制按位与的值最大。
你需要帮助小 R 求出,他能选出的 m 个数的按位与的最大值。

输入格式
从文件 and.in 中读入数据。

输入的第一行包含两个正整数 n,m。

输入的第二行包含 n 个非负整数 a1,a2,…,a n ,依次表示小 R 的 n 个非负整数。

输出格式
输出一行一个非负整数,表示小 R 能选出的 m 个数的按位与的最大值。

输入输出样例
输入 #1
4 2
3 15 12 12
输出 #1
12


输入 #2
5 3
31 3 1 15 7
输出 #2
7


说明/提示
样例 1 解释
小 R 有 4 个非负整数:3,15,12,12。
若小 R 选择 3,15,则按位与的值为 3and15=3;
若小 R 选择 3,12,则按位与的值为 3and12=0;
若小 R 选择 15,12,则按位与的值为 15and12=12;
若小 R 选择 12,12,则按位与的值为 12and12=12。
因此,小 R 能选出的 m 个数的按位与的最大值为 12。

样例 2 解释
若小 R 选择 31,15,7,则按位与的值为 31and15and7=7。可以证明,小 R 能选出的 m 个数的按位与的最大值为 7。


数据范围
对于所有测试数据,均有:
1≤m≤n≤2×10^5;
对于所有 i(1≤i≤n),均有 0≤ai<2^30 。

相与的规则就是 1&1=1 1&0=0 0&1=0 0&0=0
两位相与必须都是1 才能得到1
同时多数相与 按照贪心想法就是选择高位出现1最多的可能

那么如下几个数 选择两个数二进制按位与的值最大
1 1 0 1 0 0
1 1 0 0 0 1
1 1 1 1 1 1
0 1 1 1 0 1
0 0 0 0 1 1
从最高位开始有三个 1 可选,大于我们需要选择的两个数的要求,那么最高位是按位与的要求一定可以得到1, 那么最高位开头位为0的就不在我们的候选名单了
次高位同理
1 1 0 1 0 0
1 1 0 0 0 1
1 1 1 1 1 1
//////////////////////////////
以下不再考虑 不在候选名单中
0 1 1 1 0 1
0 0 0 0 1 1

第三位开始 是0 0 1 的候选,这时候选不出两个位1互与为1的组合,无法判断,则看第四位来判断如何选取。第三位不做任何判断
1 1 0 1 0 0
1 1 0 0 0 1
1 1 1 1 1 1
/////////////////////////////
以下不再考虑 不在候选名单中
0 1 1 1 0 1
0 0 0 0 1 1
第四位是1 0 1 的候选 ,大于我们需要选择的两个数的要求,那么当前位位0的则不在候选中 如图
1 1 0 1 0 0
1 1 1 1 1 1
////////////////////////////
以下不再考虑 不在候选名单中
1 1 0 0 0 1
0 1 1 1 0 1
0 0 0 0 1 1
最后得到答案 110100

总结就是我们从高位到低位遍历每个数的二进制位,如果当前能得到的1个数大于等于m ,我们就可以确认当前位可以互与组合出1,淘汰掉当前位为0的数组,不在考虑
如果如果当前能得到的1个数小于m,看次高位做判断。

#include <stdio.h>
#include <algorithm>
#include <memory.h>

using namespace std;

const int N = 200010;
int a[N];
int zero[N],zerocnt;
int one[N],onecnt;
int n,m;

int main(){
    scanf("%d %d",&n,&m);
    for(int i= 0;i<n;i++){
        scanf("%d",&a[i]);
    }
    int ans=0;
    for(int i = 29;i>=0;i--){
        onecnt=0; zerocnt=0;
        for(int j=0;j<n;j++){
            int t= a[j];
            
            //统计当前位1和0的数量
            if((t>>i)&1){
                one[onecnt]=t;onecnt++;
            }else{
                zero[zerocnt]=t;zerocnt++;
            }
        }
        if(onecnt>=m){
            ans += 1<<i;
            memcpy(a,one,sizeof one);
            n=onecnt;
            //printf("i=%d, onecnt=%d \n",i,onecnt);
        }
    }
    printf("%d",ans);
   
    return 0;
}

还有个更巧妙的利用按位与&的性质的代码
filter = ans|(1<<i); 遍历到第i位的时候 在之前高位的(29~i+1)位已经确定填0或者填1的数上,在第i位填上1 也就是代码 filter = ans|(1<<i);
这样这个filter和每个数相遇,只有(29~i+1)位符合条件且第i位为1的数才能被选中 if( (a[j]&filter) == filter)
我们统计这个数字. 前面说过了,如果这数字大于等m 才说明当前位可以填1 ans =filter
不满足则当前位填0 代码的表现就是什么都不做 继续检测下一位的01

#include <stdio.h>
#include <algorithm>
#include <memory.h>

using namespace std;

const int N = 200010;
int a[N];
int zero[N],zerocnt;
int one[N],onecnt;
int n,m;



int main(){
    scanf("%d %d",&n,&m);
    for(int i= 0;i<n;i++){
        scanf("%d",&a[i]);
    }
    int ans=0;
    for(int i = 29;i>=0;i--){
        int filter = ans|(1<<i);
        int cnt=0;
        for(int j=0;j<n;j++){
            if( (a[j]&filter) == filter){
                cnt++;
            }
            if(cnt==m) break;
        }
        if(cnt >=m){
            ans =filter;
        }
    }
    
    printf("%d",ans);
    
    
    return 0;
}

posted on 2026-11-01 12:06  itdef  阅读(73)  评论(0)    收藏  举报

导航