Codeforces #664 (Div. 2) C. Boboniu and Bit Operations(暴力)

C. Boboniu and Bit Operations (暴力)

time limit per test: 1 second
memory limit per test:256 megabytes
input:standard input
output:standard output

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(1in), you’re asked to choose a j ( 1 ≤ j ≤ m ) j (1≤j≤m) j(1jm) 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 c1c2cn, 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) (1n,m200).

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(0ai<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(0bi<29).

Output
Print one integer: the minimum possible c 1 ∣ c 2 ∣ … ∣ c n c_1|c_2|…|c_n c1c2cn.

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 c1c2c3c4=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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值