当前位置 博文首页 > 文章内容

    div2-664 C. Boboniu and Bit Operations

    作者: 栏目:未分类 时间:2020-08-13 11:00:45

    本站于2023年9月4日。收到“大连君*****咨询有限公司”通知
    说我们IIS7站长博客,有一篇博文用了他们的图片。
    要求我们给他们一张图片6000元。要不然法院告我们

    为避免不必要的麻烦,IIS7站长博客,全站内容图片下架、并积极应诉
    博文内容全部不再显示,请需要相关资讯的站长朋友到必应搜索。谢谢!

    另祝:版权碰瓷诈骗团伙,早日弃暗投明。

    相关新闻:借版权之名、行诈骗之实,周某因犯诈骗罪被判处有期徒刑十一年六个月

    叹!百花齐放的时代,渐行渐远!



    C. Boboniu and Bit Operations
    time limit per test1 second
    memory limit per test256 megabytes
    inputstandard input
    outputstandard output
    Boboniu likes bit operations. He wants to play a game with you.

    Boboniu gives you two sequences of non-negative integers ?1,?2,…,?? and ?1,?2,…,??.

    For each ? (1≤?≤?), you're asked to choose a ? (1≤?≤?) and let ??=??&??, where & denotes the bitwise AND operation. Note that you can pick the same ? for different ?'s.

    Find the minimum possible ?1|?2|…|??, where | denotes the bitwise OR operation.

    Input
    The first line contains two integers ? and ? (1≤?,?≤200).

    The next line contains ? integers ?1,?2,…,?? (0≤??<29).

    The next line contains ? integers ?1,?2,…,?? (0≤??<29).

    Output
    Print one integer: the minimum possible ?1|?2|…|??.

    Examples
    inputCopy
    4 2
    2 6 4 0
    2 4
    outputCopy
    2
    inputCopy
    7 6
    1 9 1 9 8 1 0
    1 1 4 5 1 4
    outputCopy
    0
    inputCopy
    8 5
    179 261 432 162 82 43 10 38
    379 357 202 184 197
    outputCopy
    147
    Note
    For the first example, we have ?1=?1&?2=0, ?2=?2&?1=2, ?3=?3&?1=0, ?4=?4&?1=0.Thus ?1|?2|?3|?4=2, and this is the minimal answer we can get.

    /*
     * 题意: 给你两个数组a长度为n(1~200), b长度为m(1~200),定义数组c为c[i] = a[i] & b[j],一个b[h]可以和多个a[i]配对,然后让你求c[0] | c[1] ... | c[n - 1]的最小值
     *  数组a,b的元素范围(0~2^9-1)
     *
     * 解决: 之前老是想着贪心,但是贪心是没法满足最优解的。换个思路,数组元素最大是2^9 - 1,刚好是二进制(1111 1111),所以最后的c[0] | c[1] ... | c[n - 1]的范围
     *  也只能是[0~2^9-1],所以只需要从0到2^9-1遍历即可,找到一个解就是最后结果
     */
    
    #include <bits/stdc++.h>
    
    using namespace std;
    
    int n, m;
    int a[234], b[234];
    
    bool check(int x) {
      bool ok;
      for (int i = 0; i < n; i++) {
        ok = false;
        for (int j = 0; j < m; j++) {
          if (((a[i] & b[j]) | x) == x) {
            ok = true;
            break;
          }
        }
        if (!ok) return false;
      }
      return true;
    }
    
    int main() {
      ios::sync_with_stdio(false);
      cin.tie(0);
    
      cin >> n >> m;
      for (int i = 0; i < n; i++) cin >> a[i];
      for (int i = 0; i < m; i++) cin >> b[i];
    
      for (int res = 0; res < 512; res++) {
        if (check(res) == true) {
          cout << res << "\n";
          break;
        }
      }
    
      return 0;
    }