CF #1395C. Boboniu and Bit Operations

题目

题目链接:1395C
参考视频:Bilibili

思路

c 1 c_1 c1| c 2 c_2 c2 | c 3 c_3 c3 | … | c n c_n cn所得的x, 其中 c i c_i ci = = = a i a_i ai ^ b j b_j bj.
可以看出题目的数据范围并不大, 所以我们可以枚举所有的答案。
对于所有的 a i a_i ai, 若 x x x | ( a i (a_i (ai & b j ) b_j) bj) == x x x 成立,则 x x x 为所求答案。

AC代码

#include<bits/stdc++.h>

using namespace std;

int main() {
    int n, m;
    scanf("%d%d", &n, &m);
    int a[210], b[210];
    for (int i = 0; i < n; i++) scanf("%d", &a[i]);
    for (int j = 0; j < m; j++) scanf("%d", &b[j]);
    for (int x = 0; x < (1 << 9); x++) {
        int f = 0;
        for (int i = 0; i < n; i++) {
            int ff = 0;
            for (int j = 0; j < m; j++) {
                if (((a[i] & b[j]) | x) == x) {
                    ff = 1; // 表示当前i与j满足等式
                    break;
                }
            }
            // 当没有满足等式时,当前x不符合题意,枚举下一个
            if (ff == 0) {
                f = 1;
                break;
            }
        }
        if (f == 0) {
            printf("%d\n", x);
            return 0;
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_45934120/article/details/107983400