1. 程式人生 > 實用技巧 >Luogu3812 【模板】線性基

Luogu3812 【模板】線性基

https://www.luogu.com.cn/problem/P3812

參考\(blog\):https://www.cnblogs.com/vb4896/p/6149022.html

線性基

首先,對於序列\(a,b,c\),它與\(a,b,b\quad xor \quad c\)能異或出的數的種類相同

所以我們插入一個數時,可以與原序列中的數瞎異或

我們可以使序列中的數的二進位制最高位不同(這種情況是唯一的)

然後貪心取就好了

\(C++ Code:\)

#include<iostream>
#include<cstdio>
#include<cmath>
#define ll long long
#define N 55
using namespace std;
int n;
ll a[N],g[N];
void ins(ll x)
{
    for (int i=55;i>=0;i--)
    {
        if (x&(1ll << i))
        {
            if (g[i])
                x^=g[i]; else
                {
                    g[i]=x;
                    return;
                }
        }
    }
}
int main()
{
    scanf("%d",&n);
    for (int i=1;i<=n;i++)
        scanf("%lld",&a[i]),ins(a[i]);
    ll ans=0;
    for (int i=55;i>=0;i--)
        ans=max(ans,ans^g[i]);
    cout << ans << endl;
    return 0;
}