Common XOR Algorithms: Linear Basis

text

Linear Basis Theory

1.0 What is a linear basis?

A linear basis is a set of numbers, and each sequence has at least one linear basis. Taking several numbers in the linear basis to XOR can get any number in the original sequence.

2.0 Three Properties of Linear Basis

  • Any number in the original sequence can be obtained by XORing some numbers in the linear basis
  • Any number in the linear basis cannot be XORed to 0
  • The number of numbers in the linear base is unique, and under the premise of maintaining property one, the number of numbers is the least

3.0 Construction of Linear Basis

So how is it constructed?

Let the array d dd represent the linear basis of the sequence a aa , and the subscripts start from 0 00 . (For ease of understanding, let x ( 2 ) be the binary number of x

void insert(ll x)
{
    for(int i=60;i>=0;i--)
    {
        if(x&(1ll<<i))//注意,如果i大于31,前面的1的后面一定要加ll
        {
            if(d[i])x^=d[i];
            else
            {
                d[i]=x;
                break;//插入成功就退出
            }
        }
    }
}

According to this, we can get a property about the d array: if d [ i ] is not 0, then the i + 1 i+1i+1th bit of d[i] (2) must be 1, and d [ i ] The highest bit of (2) is the i + 1th bit.

Personal understanding is to decompose a number into binary, and then perform continuous XOR. If it becomes 0, it proves that it can be obtained by XOR of other numbers in the linear basis, otherwise it can be successfully inserted.

Linear basis property proof

1.0 Proof Properties 1

After we know the construction method of the linear basis, we can actually easily think of how to prove the property 1 11. Let's assume that there is a number x xx in the original sequence, and we try to use it to construct the linear basis, then there will be two results - -1. The linear basis cannot be successfully inserted; 2. The linear basis is successfully inserted.
insert image description here

2.0 Proving Properties 2

insert image description here

3.0 Proof properties 3

insert image description here

Linear Basis Properties Applications

How to find the maximum value?

How to find in a sequence, take several numbers so that their XOR sum is the largest?

First construct the linear basis of this sequence, and then start from the highest bit of the linear basis. If the element of the current answer XOR linear basis can become larger, then XOR it, and the initial value of the answer is 0 00.
code show as below:

ll ans()
{
    ll anss=0;
    for(int i=60;i>=0;i--)//记得从线性基的最高位开始
    if((anss^d[i])>anss)anss^=d[i];
    return anss;
 }   

Why is solving a greedy process?

As mentioned earlier, the i + 1th bit of d[i] (2) must be 1. If you think about it here, it is nothing more than two cases:

  • The i + 1 i+1i+1 bit of ans(2) is 0.
    In this case, ans will definitely become larger after XORing d[i], so we choose to XOR it.
    Some people may ask, although the i+1 bit becomes 1 after ans XOR d[i], the following 1 ~ i bits may be affected. No matter how it changes later, even if the following 1 11 ~ i ii bits can become 1, the contribution is not as great as the contribution of the i + 1 i+1i+1 th bit becoming 1.
  • The i + 1th bit of ans(2) is 1.
    If ans ansans XOR d [ i ], then the i+1th bit will become 0, and the i+1th bit will become 0. According to the above idea, it is necessary to make the highest bit as large as possible, so at this time, ans cannot XOR d[i].

How to find the minimum value?

Note that this refers to the minimum value that can be XORed with the elements in the linear basis.

Obviously it is the smallest d[i], because the smallest d[i] will be larger no matter who XOR is.

If you want to find the minimum value that can be XORed by the entire sequence instead of the minimum value that can be XORed by the linear basis of this sequence, you need to check if there are any elements that cannot be inserted into the linear basis. If there is, then the minimum value is 0. Otherwise it is still the smallest d[i].

long long query_min()
{
    for (int i=0;i<=60;i++)
        if (d[i])
            return d[i];
    return 0;
}

How to find the kth smallest value ?

insert image description here

void work()//处理线性基
{
	for(int i=1;i<=60;i++)
	for(int j=1;j<=i;j++)
	if(d[i]&(1ll<<(j-1)))d[i]^=d[j-1];
}

if(d[i]&(1ll<<(j-1)))d[i]^=d[j-1];, the highest bit of d[i] is i+1, so it is d[j- 1]


ll k_th(ll k)
{
	if(k==1&&tot<n)return 0;//特判一下,假如k=1,并且原来的序列可以异或出0,就要返回0,tot表示线性基中的元素个数,n表示序列长度
	if(tot<n)k--;//类似上面,去掉0的情况,因为线性基中只能异或出不为0的解
	work();
	ll ans=0;
	for(int i=0;i<=60;i++)
	if(d[i]!=0)
	{
		if(k%2==1)ans^=d[i];
		k/=2;
	}
}

The linear basis is processed so that the size can be discriminated by binary! ! !

Epilogue


"If you are undecided, you can ask the spring breeze, and if the spring breeze does not speak, you will follow your heart" means: if you are hesitant about something, ask the spring breeze how to do it. . "If you are undecided, you can ask the spring breeze. If the spring breeze does not speak, you will follow your heart." The sentence comes from the "Jianlai" written by the Internet writer "Fenghuo Opera Princes". The original text is: "If you are undecided, you can ask the spring breeze. Follow your heart".

insert image description here


Guess you like

Origin blog.csdn.net/weixin_46627433/article/details/123800475