A summary of solving the XOR problem with Gauss elimination element

Source: Internet
Author: User

I think the XOR thing is particularly magical, and the most amazing thing is this nature. A XOR B xor b=a

So you don't have to worry about repeating things.

On the question of XOR you can go to the MO team's "Gaussian elimination of the Xor Equation Group", which is written in detail

I'm going to pull two examples of Bzoj on the road.

bzoj2115, 1-n the longest XOR path, according to that magical nature, we'll find a 1-n path as the standard path.

Any 1-n path is equivalent to the XOR of a standard path and some rings

How to find the ring? Very simple, BFS down, set D[x] represents a path XOR value of 1 to X, if Y has been visited by X-->y on an edge, then D[x] XOR D[y] XOR W[x,y] is a ring

And then the problem turns out to be the biggest problem with any number of XOR in a bunch of numbers, which we can do by asking for a linear base and then by a bit of greed.

1 typeNode=Record2 Po,next:longint;3 Num:int64;4      End;5 6 varF:Array[0.. -] ofInt64;7D:Array[0..50010] ofInt64;8Q,p:Array[0..50010] ofInt64;9A:Array[0..500010] ofInt64;TenE:Array[0..200010] ofnode; One X,y,i,n,m,len,t:longint; A Ans,z:int64; -  - procedureAdd (x,y:longint;z:int64); the   begin - Inc (LEN); -e[len].po:=y; -e[len].num:=Z; +e[len].next:=P[x]; -p[x]:=Len; +   End; A  at procedureBFS; -   varH,r,i,x,y:longint; -   begin -Fillchar (d,sizeof (d),255); -d[1]:=0; -h:=1; r:=1; q[1]:=1; in      whileH<=r Do -     begin tox:=Q[h]; +i:=P[x]; -        whileI<>0  Do the       begin *y:=E[i].po; $         ifd[y]=-1  ThenPanax Notoginseng         begin -d[y]:=D[x] XOR E[i].num; the Inc (R); +q[r]:=y; A         End the         Else ifD[y] XOR D[x] XOR e[i].num<>0  Then//looking for a ring +         begin - Inc (T); $a[t]:=D[y] XOR d[x] XOR E[i].num; $         End; -i:=E[i].next; -       End; the Inc (h); -     End;Wuyi   End; the  - procedureGauss; Wu   varI,j:longint; -   begin About      fori:=1  toT Do $        forj:= - Downto 0  Do -         ifA[i] and(Int64 (1) SHL J) >0  Then//To find a linear base -         begin -           iff[j]=0  Then A           begin +f[j]:=A[i]; the Break ; -           End $           Elsea[i]:=A[i] xor f[j]; the         End; the  theans:=D[n]; the      fori:= - Downto 0  Do -       ifAns and(Int64 (1) SHL i) =0  Thenans:=ans xor f[i]; in   End; the  the begin About readln (n,m); the    fori:=1  toM Do the   begin the readln (x, y, z); + Add (x, y, z); - Add (y,x,z); the   End;Bayi BFS; the Gauss; the writeln (ans); - End.
View Code

bzoj2844

The number of species that a number of XOR can produce is the rank of the 2^ linear basis.

And each number appears the same number of times (for proof)

And then we can do it, and notice that the linear base here is to use Gaussian elimination to find a way to use the problem.

Because to calculate that a number appears in the first, the size of the bits must be satisfied by the number of reciprocal XOR of the linear basis

For example, such as linear base a,b,c and a>b>c, if you take a A, B xor, select a state of 110, so that the number of XOR selection must be larger than 011, we call it to satisfy the binary size relationship (my own mouth of the noun)

1 Constmo=10086;2 3 varA,d:Array[0..100010] ofLongint;4F,b:Array[0.. +] ofLongint;5 K,p,i,j,n,ans,m,t:longint;6 7 procedureSwapvara,b:longint);8   varC:longint;9   beginTenc:=A; Onea:=b; Ab:=C; -   End; -  the begin - READLN (n); -    fori:=1  toN Do - read (a[i]); +d[0]:=1; -    fori:=1  toN Do +d[i]:=d[i-1]*2 MoDmo; A  at Readln (m); -k:=N; -    fori:=1  toN Do -   begin -      forj:=i+1  toN Do -       ifA[I]&LT;A[J] Thenswap (a[i],a[j]); in     ifa[i]=0  Then -     begin tok:=i-1; + Break ; -     End; the      forj:= - Downto 0  Do *       ifA[i] and(1SHL j) >0  Then $       beginPanax Notoginsengb[i]:=J; -          forp:=1  toN Do the           if(P<>i) and(A[p] and(1SHL j) >0) Then +a[p]:=A[p] xor a[i]; A Break ; the       End; +   End; -ans:=1; $    fori:=1  toK Do $     ifM and(1SHL B[i]) >0  Then -     begin -m:=m xor a[i]; theAns:= (Ans+d[k-i+n-k])MoDmo; -     End;Wuyi  the writeln (ans); - End.
View Code

A summary of solving the XOR problem with Gauss elimination element

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.