当前位置:网站首页>Linear basis property function code to achieve 3000 words detailed explanation, with examples
Linear basis property function code to achieve 3000 words detailed explanation, with examples
2022-07-26 04:04:00 【Qin xiaobaa】
Catalog
Find the maximum value of sequence XOR based on linear basis
Find the minimum XOR of linear basis
Linear basis to find the existence of an exclusive or value of the original sequence
The linear basis finds the K Minor difference or value
Linear basis properties
1 Any number in the original sequence can be obtained by some number XOR in the linear base
2 Linear basis can represent 0 outside , All XOR results of the original sequence
3 Linear base XOR does not appear 0, This property is not determined by the original sequence , Even if the original sequence has XOR 0 Results appear , But linear bases don't
4 Once the linear basis is determined , Is the only
5 The number of linear base XOR results is (1<<cnt)
Linear basis construction
void add(ll x)
{
for(ll i=60; i>=0; i--)
{
if(!(x&(1ll<<i)))
{
if(!p[i])
{
p[i]=x;
break;
}
else
x^=p[i];
}
}
}Find the maximum value of sequence XOR based on linear basis
ll getmax(ll x)
{
ll ans=0;
for(ll i=60;i>=0;i--)
{
if((ans^p[i])>ans)
{
ans^=p[i];
}
}
return ans;
}Find the minimum XOR of linear basis
Because the original sequence is slightly different from the linear base XOR minimum , So you need to pay attention .
Linear basis to find the existence of an exclusive or value of the original sequence
bool check(ll y)
{
for(int i=35;i>=0;i--)
{
if(y&(1ll<<i))
{
y^=x[i];
}
}
return y==0;
}The linear basis finds the K Minor difference or value
There are three steps
One , Reconstruct linear basis , Linear basis has i When a , For less than i All places of bits are exclusive or p[j]
Obtain the non-zero reconstructed linear basis and its number
Two , Judge whether the XOR result of the original sequence is 0 Of , If there is ,k-1
3、 ... and , Binary decomposition k, If k be not in 0--(1<<cnt)-1 within , There is no solution.
otherwise , If k Binary system i Who is on the 1, Then the result XOR reconstructs the linear basis
ll rebuild()
{
int cnt=0;
for(ll i=62;i>=0;i--)
{
for(ll j=i-1;j>=0;j--)
{
if(p[i]&(1ll<<j))
p[i]^=p[j];
}
}
for(ll i=0;i<=62;i++)
{
if(p[i])
d[cnt++]=p[i];
}
}
ll kth(int k)
{
if(k>=(1ll<<cnt))
return -1;
ll ans=0;
for(int i=62;i>=0;i--)
{
if(k&(1ll<<i))
ans^=d[i];
}
return ans;
}Example 1
【 Templates 】 Linear base - Luogu
Investigate the maximum query
#include <bits/stdc++.h>
#include <cstring>
#include <vector>
#include <algorithm>
#include <cstdio>
# include<set>
#include <iostream>
using namespace std;
typedef long long int ll;
ll a[110];
ll p[100];
void get(ll x)
{
for(ll i=62;i>=0;i--)
{
if(!(x&(1ll<<i)))
continue;
if(!p[i])
{
p[i]=x;
break;
}
x^=p[i];
}
}
int main()
{
ll n;
cin>>n;
ll ans=0;
for(int i=1;i<=n;i++)
{
cin>>a[i];
get(a[i]);
}
for(ll i=62;i>=0;i--)
{
if(ans<(ans^p[i]))
{
ans=ans^p[i];
}
}
cout<<ans;
return 0;
}
Example 2
Examine the judgment of the existing situation
| xor Sequence |
#include <bits/stdc++.h>
#include <cstring>
#include <vector>
#include <algorithm>
#include <cstdio>
# include<set>
#include <iostream>
using namespace std;
typedef long long int ll;
ll a[100000+10];
ll x[50];
void get(ll y)
{
for(ll i=35;i>=0;i--)
{
if(!(y&(1ll<<i)))
continue;
if(x[i]==0)
{
x[i]=y;
break;
}
else
y^=x[i];
}
}
bool check(ll y)
{
for(int i=35;i>=0;i--)
{
if(y&(1ll<<i))
{
y^=x[i];
}
}
return y==0;
}
int main()
{
int n;
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i];
get(a[i]);
}
int t;
cin>>t;
while(t--)
{
ll aa,bb;
cin>>aa>>bb;
bb^=aa;
if(check(bb))
{
cout<<"YES"<<'\n';
}
else
{
cout<<"NO"<<'\n';
}
}
return 0;
}
Example 3
[TJOI2008] coloured lights - Luogu
We regard the switch as the original sequence , And the opening and closing of the lamp is the result of its binary decomposition , The result of exclusive or of two or more switches is the final opening and closing of the lamp , Express all results with linear basis , Investigation nature 5
#include <bits/stdc++.h>
#include <cstring>
#include <vector>
#include <algorithm>
#include <cstdio>
# include<set>
#include <iostream>
using namespace std;
typedef long long int ll;
ll p[100];
void get(ll x)
{
for(int i=62;i>=0;i--)
{
if(!(x&(1ll<<i)))
continue;
if(p[i]==0)
{
p[i]=x;
break;
}
else
{
x^=p[i];
}
}
}
int main()
{
ll n,m;
cin>>n>>m;
for(int i=1;i<=m;i++)
{
string s;
cin>>s;
ll now=0;
for(int j=0;j<s.length();j++)
{
if(s[j]=='O')
now|=(1ll<<j);
}
get(now);
}
ll cnt=0;
for(int i=0;i<=62;i++)
{
cnt+=(p[i]!=0);
}
ll ans=1;
for(int i=1;i<=cnt;i++)
{
ans=ans*2%2008;
}
cout<<ans;
return 0;
}Example 4
| (Zero XOR Subset)-less |
Investigation nature 2,3. According to the meaning , We found that an XOR sub segment can be obtained by the difference or difference of prefix XOR array , So the linear basis can record all n The XOR condition of an XOR prefix array , nature 2 It ensures that they cannot exist XOR, and the result is 0 The situation of , nature 3 It ensures that the XOR values of these sub segments can be represented . And it's worth noting that , If all n Number XOR is not followed by 0, Then our linear basis must be able to transform this n All the numbers are included , Then use this large prefix XOR value to “ decompose ” The remaining small . But if n The number XOR is followed by 0, Then at least this result does not satisfy the property that linear basis represents non-zero , Thus cannot be added , Unable to participate in cutting , The rightmost endpoint is no longer n
#include <bits/stdc++.h>
#include <cstring>
#include <vector>
#include <algorithm>
#include <cstdio>
# include<set>
#include <iostream>
using namespace std;
typedef long long int ll;
ll a[1000000];
ll p[100];
void get(ll x)
{
for(ll i=62;i>=0;i--)
{
if(!(x&(1ll<<i)))
continue;
if(!p[i])
{
p[i]=x;
break;
}
x^=p[i];
}
}
int main()
{
ll n;
cin>>n;
ll ans=0;
ll now=0;
for(int i=1;i<=n;i++)
{
cin>>a[i];
now^=a[i];
get(now);
}
int cnt=0;
// There may be 0, But linear base XOR will not appear 0
// If fn No 0, Then the linear basis can express fn Exclusive or all remaining values ,
if(now==0)
{
cout<<-1;
return 0;
}
for(ll i=62;i>=0;i--)
{
if(p[i])
{
cnt++;
}
}
cout<<cnt;
return 0;
}
XOR set of
边栏推荐
- Educational Codeforces Round 132 (Rated for Div. 2) E. XOR Tree
- [Reading Notes - > data analysis] Introduction to BDA textbook data analysis
- (翻译)按钮位置约定能强化用户使用习惯
- Opencv learning notes - remapping
- How to build an enterprise level OLAP data engine for massive data and high real-time requirements?
- redux
- JS upload avatar (you can understand it after reading it, trust me)
- Synchronous FIFO based on shift register
- Testing is not valued? Senior: you should think in another position
- 【云原生】谈谈老牌消息中间件ActiveMQ的理解
猜你喜欢

How to build an enterprise level OLAP data engine for massive data and high real-time requirements?

Asemi rectifier bridge gbu1510 parameters, gbu1510 specifications, gbu1510 package
![[programmers must] Tanabata confession strategy:](/img/55/0b43dd18c8682250db13ad94cd2c2c.png)
[programmers must] Tanabata confession strategy: "the moon meets the cloud, the flowers meet the wind, and the night sky is beautiful at night". (with source code Collection)

Moco V2: further upgrade of Moco series

MySQL索引失效场景以及解决方案

Connect external MySQL databases in istio Service Grid

What is the problem of the time series database that has been developed for 5 years?

Can't the container run? The Internet doesn't have to carry the blame

Verilog implementation of key dithering elimination

Leetcode: 102. Sequence traversal of binary tree
随机推荐
2.9.4 Boolean object type processing and convenient methods of ext JS
软考 系统架构设计师 简明教程 | 案例分析解题技巧
UFS Clk Gate介绍
Seat / safety configuration upgrade is the administrative experience of the new Volvo S90 in place
PHP <=> 太空船运算符(组合比较符)
苹果在其产品中拿掉了最后一颗Intel芯片
How does redis implement persistence? Explain the AOF trigger mechanism and its advantages and disadvantages in detail, and take you to quickly master AOF
【读书笔记->数据分析】BDA教材《数据分析》书籍介绍
What are the differences between vite and wenpack?
【数字IC/FPGA】热独码检测
Implementation of distributed lock
2.9.4 Ext JS的布尔对象类型处理及便捷方法
Go plus security: an indispensable security ecological infrastructure for build Web3
MySQL index failure scenarios and Solutions
【单片机仿真项目】外部中断0和1控制两位数码管进行计数
容器跑不动?网络可不背锅
STM32 state machine programming example - full automatic washing machine (Part 2)
【Unity3d Shader】角色投影与倒影
在 Istio 服务网格内连接外部 MySQL 数据库
如何构建面向海量数据、高实时要求的企业级OLAP数据引擎?