bitmasks brute force dp greedy *1600

Please click on ads to support us..

Python Code:

n,m = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
for i in range(512):
    flag1 = 0
    for ai in a:
        flag2 = 0
        for bi in b:
            if (ai & bi)|i == i:
                flag2 = 1
                break
        if flag2==0:
            flag1 = 1
            break
    if flag1==0:
        print(i)
        break

C++ Code:

// codex by freakingflux
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define fast()                        \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);                    \
    cout.tie(NULL)
#define loop(i, ini, n) for (ll i = ini; i < (ll)n; i++)
#define pool(i, n, last) for (ll i = (ll)n; i > last; i--)
#define all(x) x.begin(), x.end()
#define tc     \
    ll tt;     \
    cin >> tt; \
    while (tt--)
#define inf INT_MAX
#define ninf INT_MIN
#define pb push_back
#define po pop_back
#define sz size()
#define ed end()
#define be begin()
#define mi(x) min_element(all(x))
#define mx(x) max_element(all(x))
#define am accumulate
#define rv(x) reverse(all(x))
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define vl vector<ll>
#define pl pair<ll, ll>
#define vll vector<vector<ll>>
#define vp vector<pl>
#define ml map<ll, ll>
#define sl set<ll>
#define msl multiset<ll>
#define mml multimap<ll, ll>
#define pi 3.1415926536
#define read  \
    ll n;     \
    cin >> n; \
    vl v(n);  \
    loop(i, 0, n) cin >> v[i];
ll mod = 1e9 + 7;

void rotate(vl &v, ll k)
{
    ll n = v.sz;
    k = k % n;
    reverse(v.be, v.be + k);
    reverse(v.be + k, v.ed);
    rv(v);
}

bool is_prime(ll n)
{
    // T.C sqrt(n)
    if (n == 0 || n == 1)
        return false;

    for (ll i = 2; i * i <= n; i++)
    {
        if (n % i == 0)
            return false;
    }

    return true;
}

vl prime_upto_n(ll n)
{
    // T.C n*log(log(n))
    vector<ll> isPrime(n + 1, 1), lp(n + 1, 0), hp(n + 1, 0);
    isPrime[0] = isPrime[1] = 0;

    for (ll i = 2; i <= n; i++)
    {
        if (isPrime[i])
        {
            lp[i] = hp[i] = i;
            for (ll x = i * i; x <= n; x += i)
            {
                isPrime[x] = 0;
                hp[x] = i;
                if (lp[x] == 0)
                    lp[x] = i;
            }
        }
    }

    return isPrime;
}

sl allFactors(ll n)
{
    // T.C sqrt(n)
    sl factors;
    for (ll i = 1; i * i <= n; i++)
    {
        if (n % i == 0)
        {
            factors.insert(i);
            if (n / i != i)
                factors.insert(n / i);
        }
    }

    return factors;
}

ml allPrimefactors(ll n)
{
    // T.C sqrt(n)
    ml primeFactors;
    for (ll i = 2; i * i <= n; i++)
    {
        while (n % i == 0)
        {
            primeFactors[i]++;
            n /= i;
        }
    }
    if (n > 1)
        primeFactors[n]++;

    return primeFactors;
}

pair<double, ll> sumCnt_of_divisors(ll n)
{
    // T.C sqrt(n)
    ll v = 1;
    double sum = 1;
    ml x = allPrimefactors(n);

    for (auto x : x)
    {
        v *= (x.se + 1);
        sum *= ((pow(x.fi, x.se + 1) - 1) / (x.fi - 1));
    }

    return {sum, v};
}

ll binpow(ll x, ll b)
{
    // T.C log(n)
    if (b == 0)
        return 1;

    ll ans = binpow(x, b / 2);

    if (b % 2)
        return ((ans % mod * ans % mod) % mod * x % mod) % mod;
    else
        return (ans % mod * ans % mod) % mod;
}

ll gcd(ll x, ll b)
{
    // T.C log(min(x, b));
    while (b)
    {
        x %= b;
        swap(x, b);
    }

    return x;
}

void setIO(string s)
{
    freopen((s + ".in").c_str(), "r", stdin);
    freopen((s + ".out").c_str(), "w", stdout);
}

ll msb_finder(ll n)
{
    ll k = __builtin_clzll(n);
    return 1 << (64 - k);
}

bool check(ll a, ll b, ll ha, ll hb)
{
    if (a == 0 && ha == 0)
        return false;
    if (ha == 0 && hb == 0)
        return false;
    if (b == 0 && hb == 0)
        return false;
    if (a == 0 && b == 0)
        return false;
    return true;
}

void solve()
{
    ll n, m;
    cin>>n>>m;
    vl a(n);
    loop(i,0,n) cin>>a[i];
    set<ll>b;
    while(m--){
        ll x;
        cin>>x;
        b.insert(x);
    }

    ll ans=inf;
    loop(i,0,(1<<9)){
        ll cnt=0;
        loop(j,0,n){
            ll flag=0;
            for(auto x:b){
                if(((a[j]&x)|i)==i){
                    flag=1;
                    break;
                }
            }

            if(!flag) break;
            else cnt++;
        }
        if(cnt==n) ans=min(ans, i);
    }

    cout<<ans<<"\n";
}

int main()
{
    // setIO("notlast");
    fast();
    //tc
    {
        solve();
    }
}


Comments

Submit
0 Comments
More Questions

507B - Amr and Pins
379A - New Year Candles
1154A - Restoring Three Numbers
750A - New Year and Hurry
705A - Hulk
492B - Vanya and Lanterns
1374C - Move Brackets
1476A - K-divisible Sum
1333A - Little Artem
432D - Prefixes and Suffixes
486A - Calculating Function
1373B - 01 Game
1187A - Stickers and Toys
313B - Ilya and Queries
579A - Raising Bacteria
723A - The New Year Meeting Friends
302A - Eugeny and Array
1638B - Odd Swap Sort
1370C - Number Game
1206B - Make Product Equal One
131A - cAPS lOCK
1635A - Min Or Sum
474A - Keyboard
1343A - Candies
1343C - Alternating Subsequence
1325A - EhAb AnD gCd
746A - Compote
318A - Even Odds
550B - Preparing Olympiad
939B - Hamster Farm