Number of cattle and cattle [linear base + two points]

Link: https://ac.nowcoder.com/acm/contest/10845/E
Source : Niuke.com

insert image description here
Guaranteed answer is within long long.

solution

This question can be said to be a template question on a linear basis. Let's learn about linear basis first:
Linear basis video
After processing the linear basis, you can use its property 4:

  1. Find the maximum xor of any subset: xor all elements in the linear basis
  2. Find the smallest xor of any subset: equal to the smallest pivot
  3. Query whether x is in the range: if x can be inserted into a linear basis, then x cannot be xored out by the current linear basis
  4. Query the k-th smallest value: decompose k into binary, and xor the pivot of the position corresponding to 1; note that the 0th smallest here is 0
  5. Find the maximum value of xor between any subset and x: from high -> low greedy, if a[j] on xor can become larger, then xor

Then perform bisection, and bisection to the first number greater than k can be ranked in linear space. If there are n pivots in the linear basis, then there are 2 n 2^{n}2There are n different numbers in linear space. Then the answer isthe last rank ( 2 n − 1 ) − the first rank greater than k + 1 the last rank (2^{n}-1) - the first rank greater than k + 1last ranking ( 2 _n1)first rank greater than k _+1

code

#include <stdio.h>
#include <cstring>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <iostream>
#include <map>
#include <cmath>
#include <set>

#define go(i, l, r) for(int i = (l), i##end = (int)(r); i <= i##end; ++i)
#define god(i, r, l) for(int i = (r), i##end = (int)(l); i >= i##end; --i)
#define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define debug_in  freopen("in.txt","r",stdin)
#define debug_out freopen("out.txt","w",stdout);
#define pb push_back
#define all(x) x.begin(),x.end()
#define fs first
#define sc second
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> pii;
const ll maxn = 1e6+10;
const ll maxM = 1e6+10;
const ll inf_int = 1e8;
const ll inf_ll = 1e17;

template<class T>void read(T &x){
    
    
    T s=0,w=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){
    
    if(ch=='-')w=-1;ch=getchar();}
    while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
    x = s*w;
}
template<class H, class... T> void read(H& h, T&... t) {
    
    
    read(h);
    read(t...);
}

void pt(){
    
     cout<<'\n';}
template<class H, class ... T> void pt(H h,T... t){
    
     cout<<" "<<h; pt(t...);}

//--------------------------------------------

int N,L = 62;
ll K;
ll a[maxn];
int zero = 0;
bool insert(ll x){
    
     //线性基插入模板
    for(int j = L;j>=0;j--){
    
    
        if((x>>j & 1LL) == 0) continue;
        if(a[j]){
    
    
            x ^= a[j];
            continue;
        }
        for(int k = j - 1;k>=0;k--){
    
    
            if((x>>k & 1LL)){
    
    
                x ^= a[k];
            }
        }
        for(int k = L;k>j;k--){
    
    
            if((a[k]>>j & 1LL)){
    
    
                a[k] ^= x;
            }
        }
        a[j] = x;
        return 1;
    }
    return 0;
}
ll judge(ll mid){
    
    
    ll ans = 0;
    int tag = 1;
    for(int j = 0;j<=L;j++){
    
    
        if(a[j] == 0) continue;
        if(mid & 1LL){
    
    
            ans ^= a[j];
        }
        mid/=2;
    }
    return ans;
}
void solve(int cnt){
    
    
    ll total = 1LL<<cnt;
    ll l = 0,r = (1LL<<cnt)-1,ans = total+1;
    while(l<=r){
    
    
        ll mid = (l+r)>>1;
        if(judge(mid) > K) {
    
    
            r = mid-1,ans = mid;
        }
        else l = mid+1;
    }
    if(ans >= total) puts("0");
    else{
    
    
        printf("%lld\n",total-1 - ans + 1);
    }

}

int main() {
    
    
//    debug_in;
//    debug_out;


    read(N,K);
    int cnt = 0;
    go(i,1,N) {
    
    
        ll x;read(x);
        if(insert(x)) cnt++;
    }
    solve(cnt);

    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326207505&siteId=291194637