题目链接:https://codeforces.com/problemset/problem/1395/C
CSDN食用链接:https://blog.csdn.net/qq_43906000/article/details/107973093
Boboniu likes bit operations. He wants to play a game with you.
Boboniu gives you two sequences of non-negative integers (a_1,a_2,…,a_n) and (b_1,b_2,…,b_m).
For each (i (1≤i≤n)), you're asked to choose (a_j (1≤j≤m)) and let (c_i=a_i&b_j), where & denotes the bitwise AND operation. Note that you can pick the same j for different i's.
Find the minimum possible (c_1|c_2|…|c_n), where | denotes the bitwise OR operation.
Input
The first line contains two integers n and m (1≤n,m≤200).
The next line contains n integers (a_1,a_2,…,a_n (0≤a_i<29)).
The next line contains m integers (b_1,b_2,…,b_m (0≤b_i<29)).
Output
Print one integer: the minimum possible (c_1|c_2|…|c_n).
input
4 2
2 6 4 0
2 4
output
2
input
7 6
1 9 1 9 8 1 0
1 1 4 5 1 4
output
0
input
8 5
179 261 432 162 82 43 10 38
379 357 202 184 197
output
147
Note
For the first example, we have (c1=a1&b2=0, c2=a2&b1=2, c3=a3&b1=0, c4=a4&b1=0).Thus (c1|c2|c3|c4=2), and this is the minimal answer we can get.
题目大意:给你数列a,b,必须对每一个(a_i)从(b_j)中任选一个数得(c_i=a_i&b_j),最后使得(c_1|c_2|...|c_n)最小
emmmm,刚开始的思路就是暴力,也写对了,就是出了点小BUG,后面结果改成DP了。。。然后赛后就被fst了。。。写了个假DP。。。
它的数据范围很小,所以我们可以直接考虑枚举答案,不难看出答案的区间为([0,1<<2^9)),然后我们就来检查答案了,两层for过去,然后取一下位运算与得(x),接下来就是对二进制下的(x)的每一位是否符合答案,如果(x)的某一位存在1,而答案不存在,那么就说明了这个(a_i&b_j)是不合法的。那么只要有一个(a_i&b_j)是合法的,那么(c_i)就是合法的。于是我们很容易写出代码了。
以下是AC代码:
#include <bits/stdc++.h>
using namespace std;
int a[300],b[300];
int main(int argc, char const *argv[])
{
int n,m;
scanf ("%d%d",&n,&m);
for (int i=1; i<=n; i++)
scanf ("%d",&a[i]);
for (int i=1; i<=m; i++)
scanf ("%d",&b[i]);
int ans=0;
for (int i=0; i<(1<<9); i++){
int mk=0;
for (int j=1; j<=n; j++){
int flag=0;
for (int k=1; k<=m; k++){
int x=a[j]&b[k];
for (int g=0; g<=9; g++){
if ((x&(1<<g)) && !(i&(1<<g))) {flag++; break;}
}
}
if (flag==m) {mk=1; break;}
}
if (!mk) {ans=i; break;}
}
printf("%d
",ans);
return 0;
}