目录

CF1395C-Boboniu and Bit Operations

CF1395C-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,\ldots,a_n $ and $ b_1,b_2,\ldots,b_m $ .

For each $ i $ ( $ 1\le i\le n $ ), you’re asked to choose a $ j $ ( $ 1\le j\le 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 | \ldots | c_n $ , where $ | $ denotes the bitwise OR operation.

输入格式:

The first line contains two integers $ n $ and $ m $ ( $ 1\le n,m\le 200 $ ).

The next line contains $ n $ integers $ a_1,a_2,\ldots,a_n $ ( $ 0\le a_i < 2^9 $ ).

The next line contains $ m $ integers $ b_1,b_2,\ldots,b_m $ ( $ 0\le b_i < 2^9 $ ).

输出格式:

Print one integer: the minimum possible $ c_1 | c_2 | \ldots | c_n $ .

样例:

样例输入 1:

1
2
3
4 2
2 6 4 0
2 4

样例输出 1:

1
2

样例输入 2:

1
2
3
7 6
1 9 1 9 8 1 0
1 1 4 5 1 4

样例输出 2:

1
0

样例输入 3:

1
2
3
8 5
179 261 432 162 82 43 10 38
379 357 202 184 197

样例输出 3:

1
147

思路:

实现:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <bits/stdc++.h>
using namespace std;
int a[1001];
int b[1001];
int n, m;
int main()
{
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
        cin >> a[i];
    for (int i = 1; i <= m; i++)
        cin >> b[i];
    int ans1 = (1 << 9) - 1, ans2 = 0;
    for (int i = 8; i >= 0; i--)
    {
        for (int j = 1; j <= n; j++)
        {
            int ans = 1;
            for (int k = 1; k <= m; k++)
            {
                if ((a[j] & b[k] & (ans1)) >> i == 0)
                {
                    ans = 0;
                    break;
                }
            }
            if (ans == 1)
            {
                ans1 = ans1 ^ (1 << i);
                ans2 = ans2 | (1 << i);
            }
        }
    }
    cout << ans2 << endl;
}