这个题是相或为k,考察相或的性质,用俩个数举例子,011001和011101后面的数不管和哪个数相或都不可能变成前面的数,所以利用这个性质我们可以用相与运算来把和k对应位置的1都积累起来,看最后能不能拼起来k如果能拼起来k那就是,否则不是,上代码。
package com.js.datastructure.recursion.蓝桥;
import java.util.Scanner;
public class 相或为k_位运算 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int t = scanner.nextInt();
for (int i = 0; i < t; i++) {
int n = scanner.nextInt();
int k = scanner.nextInt();
int ans = 0;
for (int j = 0; j < n; j++) {
int x = scanner.nextInt();
if((x & k) == x){
ans = ans | x;
}
}
if(ans == k){
System.out.println("Yes");
}else{
System.out.println("No");
}
}
}
}