Cod sursa(job #2850750)

Utilizator ArdeleanOficialAlexandru ArdeleanOficial Data 17 februarie 2022 15:07:19
Problema A+B Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.69 kb
#include <iostream>
#include <map>

using namespace std;

const int N = 5e4 + 7;

int a[N];
map < int, int > m;

int n, k;

bool check(int mask) {
    m.clear();
    for (int i = 1; i <= n; ++i)
        m[a[i] & ((1<<30) - mask)]++;
    for (auto i : m)
        if (i.second > k)
            return 0;
    return 1;
}

int cb() {
    int pas = (1<<29), r(0);
    while (pas) {
        if (check(r + pas))
            r += pas;
        pas >>= 1;
    }
    return r;
}

int main()
{
    int t;
    cin >> t;
    while (t--) {
        cin >> n >> k;
        for (int i = 1; i <= n; ++i)
            cin >> a[i];
        cout << cb() << '\n';
    }
    return 0;
}