题目链接:https://ac.nowcoder.com/acm/contest/7607/B
题意:定义一个集合若包含y,则存在x,满足x&y=y。给定这个集合内的数,m次询问每个数是否被这个集合包含。
题解:m次询问,所以我们肯定预处理好这个集合。根据题意简单分析可得:y的二进制位1是x的子集。所以我们对每一个集合内的数,都做一次状压的方法循环遍历二进制子集上的数,并把他们标记起来,最后m次询问就只需要看标记数组就行。
#include <bits/stdc++.h> using namespace std; typedef long long ll; inline ll rd(){ ll x=0;char o,f=1; while(o=getchar(),o<48)if(o==45)f=-f; do x=(x<<3)+(x<<1)+(o^48); while(o=getchar(),o>47); return x*f; } const int maxn=1e5+5; int n,m,x; int a[maxn]; int vis[1000005]; void insert(int x){ if (vis[x])return; for (int j=x;j;j=(j-1)&x){ vis[j]=1; } } int main() { n=rd();m=rd(); for (int i=1;i<=n;i++)a[i]=rd(); sort(a+1,a+n+1); for (int i=n;i>=1;i--)insert(a[i]); while (m--){ x=rd(); if (vis[x])puts("yes"); else puts("no"); } return 0; }