题目
题意:
给你两个序列 a , b a,b a,b,然后对于让 c i = a i & b j ( 1 ≤ i ≤ n , 1 ≤ j ≤ m ) c_i=a_i \& b_j(1\leq i\leq n,1\leq j\leq m) ci=ai&bj(1≤i≤n,1≤j≤m),我们需要让最后的 c 1 ∣ c 2 ∣ . . . . ∣ c n c_1|c_2|....|c_n c1∣c2∣....∣cn最小。
思路:
因为题目的 a , b a,b a,b的范围是 2 9 2^9 29,所以我们可以直接 c i c_i ci的最大也不可能超过 2 9 2^9 29,那么我们就可以直接枚举 a n s ans ans,因为 a n s = c 1 ∣ c 2 ∣ . . . . ∣ c n ans=c_1|c_2|....|c_n ans=c1∣c2∣....∣cn,那么只要求出是否存在这样的 c 1 , c 2 , . . . . , c n c_1,c_2,....,c_n c1,c2,....,cn,因为 a n s ans ans是或的总和,所以就有 c i ∣ a n s = a n s c_i | ans=ans ci∣ans=ans,那么转换一下就是满足 ∃ i j , c i = a i & b j \exists ij, \ \ c_i=a_i\&b_j ∃ij, ci=ai&bj也就是 ( a i & b j ) ∣ a n s = a n s (a_i\&b_j )|ans=ans (ai&bj)∣ans=ans。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
#include <string>
#include <cmath>
#include <set>
#include <map>
#include <deque>
#include <stack>
#include <cctype>
using namespace std;
typedef long long ll;
typedef vector<int> veci;
typedef vector<ll> vecl;
typedef pair<int, int> pii;
template <class T>
inline void read(T &ret) {
char c;
int sgn;
if (c = getchar(), c == EOF) return ;
while (c != '-' && (c < '0' || c > '9')) c = getchar();
sgn = (c == '-') ? -1:1;
ret = (c == '-') ? 0:(c - '0');
while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0');
ret *= sgn;
return ;
}
inline void outi(int x) {if (x > 9) outi(x / 10);putchar(x % 10 + '0');}
inline void outl(ll x) {if (x > 9) outl(x / 10);putchar(x % 10 + '0');}
const int maxn = 210;
int a[maxn], b[maxn];
int n, m;
bool check(int s) {
for (int i = 0; i < n; i++) {
bool flag = false;
for (int j = 0; j < m; j++) {
if ((a[i] & b[j] | s) == s) {
flag = true;
break;
}
}
if (!flag) return false;
}
return true;
}
int main() {
read(n), read(m);
for (int i = 0; i < n; i++) read(a[i]);
for (int i = 0; i < m; i++) read(b[i]);
for (int s = 0; s < (1 << 9); s++) {
if (check(s)) {
printf("%d", s);
return 0;
}
}
return 0;
}