【題解】Codeforces 1395C. Boboniu and Bit Operations

【題目敘述】http://codeforces.com/contest/1395/problem/C

#include <iostream>
#include <cstring>
using namespace std;
 
int n, m, a[205], b[205], dp[205][2<<9];
 
int f(int pos, int mask){
    if (dp[pos][mask] != -1) return dp[pos][mask];
    if (pos == n){
        return dp[pos][mask] = mask;
    }
    int ret = 2<<10;
    for (int i = 0; i < m; i++){
        ret = min(ret, f(pos+1, mask|(a[pos]&b[i])));
    }
    return dp[pos][mask] = ret;
}
 
int main() {
    cin >> n >> m;
    memset(dp, -1, sizeof(dp));
    for (int i = 0; i < n; i++){
        cin >> a[i];
    }
    for (int i = 0; i < m; i++){
        cin >> b[i];
    }
    cout << f(0, 0) << "\n";
}
分享本文 Share with friends