题面大意:
给你
n
n
n个序列
a
i
ai
ai,
m
m
m个序列
b
i
bi
bi,现在要你构造一个
c
c
c数组,其中
c
i
=
a
i
ci=ai
ci=ai&
b
i
bi
bi,求
m
i
n
(
c
1
∣
c
2
∣
c
3
∣
.
.
.
∣
c
n
)
min(c1|c2|c3|...|cn)
min(c1∣c2∣c3∣...∣cn)。
思路:
首先我们可以发现一个这样的性质,假设答案是
s
s
s。对于任意的
c
i
ci
ci,都有
c
i
∣
s
=
=
s
ci|s==s
ci∣s==s。
因为
s
=
[
0
,
1
e
9
]
s=[0,1e9]
s=[0,1e9],
n
,
m
n,m
n,m都很小,我们可以
O
(
1
e
9
∗
n
∗
m
)
O(1e9*n*m)
O(1e9∗n∗m)暴力枚举答案。
我们从小到大枚举答案,对于所以的
i
i
i,都至少存在一个
a
i
ai
ai&
b
i
bi
bi==s,说明这个答案可行,直接输出即可。
代码:
#include<iostream>
#include<string>
#include<algorithm>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<cstring>
#include<vector>
using namespace std;
const int maxn = 1e3 + 10;
typedef long long int ll;
int a[maxn],b[maxn];
void solved(){
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];
for(int s = 0; s < (1 << 9); s++){
bool flag = false;
for(int i = 1; i <= n; i++){
bool f = false;
for(int j = 1; j <= m; j++){
if(((a[i] & b[j]) | s) == s){
f = true;break;
}
}
if(!f){
flag = true;break;
}
}
if(!flag){
cout<<s<<endl;return ;
}
}
}
int main(){
solved();
return 0;
}