C. Boboniu and Bit Operations(二进制,暴力,思维)

在这里插入图片描述
在这里插入图片描述
题面大意:
给你 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(c1c2c3...cn)
思路:
首先我们可以发现一个这样的性质,假设答案是 s s s。对于任意的 c i ci ci,都有 c i ∣ s = = s ci|s==s cis==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(1e9nm)暴力枚举答案。
我们从小到大枚举答案,对于所以的 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;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值