用一个bool数组表示每个数是否能被表示,初始化把Q所有元素设为1,然后跑一个高维前缀和算出所有的子集,类似多源最短路,这个过程可以同时多源进行
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int N = 1e7 + 10, M = 2e6 + 10, inf = (1 << 20) - 1;
inline int read() {
bool sym = 0; int res = 0; char ch = getchar();
while (!isdigit(ch)) sym |= (ch == '-'), ch = getchar();
while (isdigit(ch)) res = (res << 3) + (res << 1) + (ch ^ 48), ch = getchar();
return sym ? -res : res;
}
int n, m, cnt, vis[N];
int main() {
n = read(); m = read();
for (int i = 1; i <= n; i++) {int x = read(); vis[x] = 1;}
for (int i = 0; i < 20; i++) {
for (int j = 0; j <= inf; j++) {
if (!(j >> i & 1)) vis[j] |= vis[j | 1 << i];
}
}
for (int i = 1; i <= m; i++) {
int x = read(); printf(vis[x] ? "yes\n" : "no\n");
}
return 0;
}
```</queue></algorithm></cstring></cmath></cstdio></iostream>