1 条题解

  • 1
    @ 2025-1-18 19:52:08
    #include <bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    const int N = 1e5 + 2;
    const int M = 52;
    ll n, m, cnt, p[M], d[M];
    bool flag;
    void insert(ll x) 
    {
        for (int i = M - 1; i >= 0; i--)
            if (((x >> i) & 1)) 
            {
                if (p[i] == 0) 
                {
                    p[i] = x;
                    cnt++;
                    return;
                } 
                else x ^= p[i];
            }
    
        flag = true;
    }
    void rebuild() 
    {
        for (int i = 0; i < M; i++)
            for (int j = i - 1; j >= 0; j--)
                if (((p[i] >> j) & 1) && p[j])
                    p[i] ^= p[j];
    }
    ll ask(ll x) 
    {
        x -= flag;
    
        if ((1ll << cnt) <= x)
            return -1;
    
        ll ret = 0, t = cnt - 1;
    
        for (int i = M - 1; i >= 0; i--) 
        {
            if (p[i]) 
            {
                if ((x >> t) & 1)
                    ret ^= p[i];
    
                t--;
            }
        }
    
        return ret;
    }
    int main() 
    {
        cin >> n;
        ll u;
    
        for (int i = 1; i <= n; i++) 
        {
            cin >> u;
            insert(u);
        }
    
        rebuild();
        cin >> m;
    
        for (int i = 1; i <= m; i++) 
        {
            cin >> u;
            cout << ask(u) << endl;
        }
    }
    
    
    • 1

    信息

    ID
    5545
    时间
    1000ms
    内存
    256MiB
    难度
    10
    标签
    递交数
    1
    已通过
    1
    上传者