【bzoj4976】宝石镶嵌

题解:

比较水

注意k<=100这个条件

当n-k比较大的时候

我们显然会把它有的位都给取了

不然的话我们可以考虑dp

暴力状压就可以了

代码:

 

#include <bits/stdc++.h>
using namespace std;
#define rint register int
#define IL inline
#define rep(i,h,t) for (rint i=h;i<=t;i++)
#define dep(i,t,h) for (rint i=t;i>=h;i--)
const int N=2e5;
int a[N];
bool t[20];
vector<int> ve[20];
bool f[20][200000];
int main()
{
  freopen("1.in","r",stdin);
  freopen("1.out","w",stdout);
  ios::sync_with_stdio(false);
  int n,k;
  cin>>n>>k; k=n-k;
  rep(i,1,n) cin>>a[i];
  if (k>=18)
  {
    rep(i,1,n)
      dep(j,17,0) t[j]|=(a[i]>>j)&1;
    int ans=0;
    rep(i,0,17)
      if (t[i]) ans+=1<<i;
    cout<<ans<<endl;
    exit(0);
  }
  f[0][0]=1;
  rep(i,0,k-1)
    rep(j,0,(1<<17)-1)
      if (f[i][j])
        rep(l,1,n)
          f[i+1][j|a[l]]=1;
  dep(j,(1<<17)-1,0)
    if (f[k][j])
    {
      cout<<j<<endl;
      exit(0); 
    }
  return 0;
}

 

posted @ 2018-09-11 00:05  尹吴潇  阅读(188)  评论(0编辑  收藏  举报