C. Boboniu and Bit Operations (暴力)
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 a_1,a_2,…,a_n a1,a2,…,an and b 1 , b 2 , … , b m b_1,b_2,…,b_m b1,b2,…,bm.
For each i ( 1 ≤ i ≤ n ) i (1≤i≤n) i(1≤i≤n), you’re asked to choose a j ( 1 ≤ j ≤ m ) j (1≤j≤m) j(1≤j≤m) and let c i = a i ci=ai ci=ai & b j bj bj, where & denotes the bitwise AND operation(按位与运算). Note that you can pick the same j j j for different i’s.
Find the minimum possible c 1 ∣ c 2 ∣ … ∣ c n c1|c2|…|cn c1∣c2∣…∣cn, where | denotes the bitwise OR operation(按位或运算).
Input
The first line contains two integers
n
n
n and
m
m
m
(
1
≤
n
,
m
≤
200
)
(1≤n,m≤200)
(1≤n,m≤200).
The next line contains n n n integers a 1 , a 2 , … , a n ( 0 ≤ a i < 29 ) a_1,a_2,…,a_n (0≤a_i<29) a1,a2,…,an(0≤ai<29).
The next line contains m m m integers b 1 , b 2 , … , b m ( 0 ≤ b i < 29 ) b_1,b_2,…,b_m (0≤b_i<29) b1,b2,…,bm(0≤bi<29).
Output
Print one integer: the minimum possible
c
1
∣
c
2
∣
…
∣
c
n
c_1|c_2|…|c_n
c1∣c2∣…∣cn.
Examples
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 KaTeX parse error: Expected 'EOF', got '&' at position 6: c1=a1&̲b2=0, c2=a2&b1=…&
b
1
=
0
,
c
4
=
a
4
b1=0, c4=a4
b1=0,c4=a4&
b
1
=
0
b1=0
b1=0.Thus
c
1
∣
c
2
∣
c
3
∣
c
4
=
2
c1|c2|c3|c4=2
c1∣c2∣c3∣c4=2, and this is the minimal answer we can get.
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define N 1024
int a[N], b[N], visit[N][N];
int main()
{
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; ++i)
cin >> a[i];
for (int i = 1; i <= m; ++i)
cin >> b[i];
visit[0][0] = 1;
for (int i = 1; i <= n;++i)
{
for (int j = 1; j <= m;++j)
{
int c = a[i] & b[j];
for (int k = 0; k < 512; ++k)
{
if (visit[i - 1][k])
visit[i][k | c] = 1;
}
}
}
for (int i = 0; i < 512; ++i)
{
if (visit[n][i])
{
printf("%d",i);
return 0;
}
}
return 0;
}