欢迎使用优快云-markdown编辑器

本文介绍了一种通过位运算寻找给定整数集合中选择特定数量元素进行按位与操作后得到最小值的问题。该文详细阐述了如何利用递归搜索结合剪枝策略来高效求解,并提供了一个具体的实现示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

标题 ##Magic Bitwise And Operation

Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 1551 Accepted Submission(s): 610

Problem Description
Given n integers, your task is to pick k out of them so that the picked number are minimum when do bitwise “AND” among all of them.
For example, there are three integers 5, 6 and 7. You are asked to pick two of them. Your possible strategy is (5, 6), (5, 7) or (6, 7). The values when do bitwise and all the picked numbers together are as follows:
5 and 6 = 4
5 and 7 = 5
6 and 7 = 6
The smallest one is 4.

Input
There are multiple test cases for this problem. The first line of the input contains an integer denoting the number of test cases.
For each test case, there are two integers in the first line: n and k, denoting the number of given integers and the number of integers you are asked to pick out. n <= 40
The second line contains the n integers. You may assume that all integers are small than 2^60.

Notes: There are about one thousand randomly generated test cases. Fortunately 90% of them are relatively small.

Output
For each test case, output only one integer is the smallest possible value.

Sample Input
3
3 2
5 6 7
8 2
238 153 223 247 111 252 253 247
40 10
1143632830316675007 558164877202423550 1152356080752164603 1143911006781551605 1132655005501751263
1152919305583327167 1141662230660382702 862439259920596463 1151777428397603327 1008771132016295871
855666336963428351 1151795583225167807 1152634943314572791 1071856693060561407 1132650872803426303
1124211056982081471 1152917106425982911 1152815392070041535 1080863910568853481 288230371856350975
1080720560532488126 864686455262281727 576460673919991167 574191342855241589 1152233760050118651
1152921504605798263 1152912708241186815 1079738008506187487 1075796261476483027 1080854478820730879
1152885219917823999 1151725162940854259 1147529498501577715 571956602920235519 1134545630643616248
1152921218991521790 1152921496000052703 1142788250826440703 1151654831778151421 1152780747522637695

Sample Output
Case #1: 4
Case #2: 9
Case #3: 36028797086245424

Source
The 36th ACM/ICPC Asia Regional Shanghai Site —— Warmup

Recommend
lcy

  1. 题意
    给出n个数字要求从中间挑选出m个数字,使得这m个数字与运算的结果最小。
  2. 思路
    直接搜索即可。注意要剪枝,因为1数位是越与越少的,所以可以将当前所得到的数字和剩下的所有数字进行与运算,如果比ans要小,那么才继续进行搜索,否则直接终止当前的搜索。
#include<cstdio>
#include<iostream>
#include<algorithm>

using namespace std;
typedef long long ll;
int n,m;
ll a[100];
ll pre[100];

ll ans=0;


void dfs(int pos,int num,ll x)
{

    if(pos==n)
    {
        if(num==m)
        {
            ans=min(ans,x);
        }
        return ;
    }
    if(num==m)
    {
        ans=min(ans,x);
        return ;
    }
    if(num>m)
        return ;
    //printf("%lld   %lld\n",pre[pos+1],pre[pos]);
    if(pos+1<n&&(x&pre[pos])>=ans)
        return ;

    dfs(pos+1,num+1,x&a[pos]);
    dfs(pos+1,num,x);
}
int main()
{

    int T;
    scanf("%d",&T);
    int cas=1;
    while(T--)
    {
        scanf("%d%d",&n,&m);
        for(int i=0;i<n;i++)
        {
            scanf("%lld",&a[i]);
           // printf("%lld\n",a[i]);
        }
        sort(a,a+n);
        ans=((ll)1<<63)-1;

        for(int i=0;i<m;i++)
        {
            ans=ans&a[i];
          //  printf("%lld\n",ans);
        }
        pre[n-1]=a[n-1];
        for(int i=n-2;i>=0;i--)
            pre[i]=pre[i+1]&a[i];
       // printf("ans:%lld\n",ans);
        dfs(0,0,((ll)1<<63)-1);

        printf("Case #%d: %lld\n",cas++,ans);

    }

    return 1;
}
内容概要:文章阐述了构建安全教育体系以应对2025年挑战的目标、原则、内容设计、实施路径、预期成效及保障措施。面对日益复杂的社会安全形势,文章提出通过系统化、科学化、人性化的安全教育体系提升全民安全意识与应急能力。该体系涵盖知识普及、技能实训、文化培育三个模块,采用沉浸式学习工具、模块化训练、跨领域协作演练等方式。实施路径分为体系构建(2023-2024年)、试点推广(2024-2025年)、全面覆盖(2025年及以后)三个阶段。预期成效包括提升公众安全素养、降低事故发生率、增强社会韧性。保障措施涉及政策、资源、技术和评估四个方面,确保体系的有效运行。 适合人群:社会各界人士,特别是教育工作者、应急管理从业者、政策制定者以及关注公共安全的个人和组织。 使用场景及目标:①适用于各级学校、企业及社区的安全教育规划与实施;②为政策制定者提供构建安全教育体系的参考框架;③帮助教育工作者设计和优化安全教育课程与活动;④提升公众的安全意识与应急能力,降低安全事故的发生率。 其他说明:本文不仅提供了详细的构建方案,还强调了科学性、系统性、人本性和预见性的核心原则,旨在通过维度、层次的安全教育实践,推动安全文化深入人心,为社会的可持续发展奠定坚实基础。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值