codeforces C. Boboniu and Bit Operations

在这里插入图片描述

题目

题意:

给你两个序列 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(1in,1jm),我们需要让最后的 c 1 ∣ c 2 ∣ . . . . ∣ c n c_1|c_2|....|c_n c1c2....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=c1c2....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 cians=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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值