HDU 4016 Magic Bitwise And Operation

题目大意:从N个数中选k个数使得&运算后结果最小

解题思路:经典的搜索+剪枝,抓住每次&运算之后得到的值肯定<=当前数,这就OK了~每次从小的先开始取,也是一个不错的剪枝

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const long long inf = (1LL<<62) - 1;
int n, k;
long long num[45], sum[45], ans;

void dfs(int idx, int tot, long long s)
{
     if (s < ans) ans = s;
     if (idx >= n || tot >= k) return;
     if ((s & sum[idx]) >= ans) return;
     dfs(idx+1, tot+1, s&num[idx]);
     dfs(idx+1, tot, s);
     }
int main()
{
    int ca = 0, T;
    scanf("%d", &T);
    while (T--)
    {
          scanf("%d%d", &n, &k);
          sum[n] = ans = inf;
          for (int i=0; i<n; i++) scanf("%I64d", &num[i]);
          sort(num, num+n);
          for (int i=n-1; i>=0; i--) sum[i] = sum[i+1] & num[i];
          dfs(0, 0, inf);       
          printf("Case #%d: %I64d\n", ++ca, ans);          
    }    
    return 0;
    }


你可能感兴趣的:(HDU 4016 Magic Bitwise And Operation)