题意: 分别给定 n n n对数字对 ( A i x , A i y ) (A_i^x,A_i^y) (Aix,Aiy),和 m m m 对 ( B i x , B i y ) (B_i^x,B_i^y) (Bix,Biy)。有两个人,第一个人 A A A从每对 ( A i x , A i y ) (A_i^x,A_i^y) (Aix,Aiy)中选一个数字取异或和,即 A x o r = A 0 x ∣ y ⊗ A 1 x ∣ y ⊗ ⋯ ⊗ A n x ∣ y A_{xor}=A_0^{x|y} \otimes A_1^{x|y} \otimes \dots \otimes A_n^{x|y} Axor=A0x∣y⊗A1x∣y⊗⋯⊗Anx∣y,另一个人 B B B从每对 ( B i x , B i y ) (B_i^x,B_i^y) (Bix,Biy)中选取一个数字取异或和,即 B x o r = B 0 x ∣ y ⊗ B 1 x ∣ y ⊗ ⋯ ⊗ B m x ∣ y B_{xor}=B_0^{x|y} \otimes B_1^{x|y} \otimes \dots \otimes B_m^{x|y} Bxor=B0x∣y⊗B1x∣y⊗⋯⊗Bmx∣y,最后得到一个数字 a n s = A x o r ⊗ B x o r ans=A_{xor}\otimes B_{xor} ans=Axor⊗Bxor。 A A A希望 a n s ans ans尽可能大, B B B希望 a n s ans ans尽可能小。双方选择均为最优,ans值应该是多少。
分析: 因为所有的数字对对 A , B A,B A,B都是透明的,所以 a n s ans ans值是唯一的。
首先这个问题并不是一个朴素的线性基问题。我们需要通过某种转换方法之后,使得它能使用线性基解决。首先构造 A x o r ′ = A 0 x ⊗ A 1 x ⊗ ⋯ ⊗ A n x A_{xor}^{'}=A_0^x\otimes A_1^x\otimes \dots \otimes A_n^x Axor′=A0x⊗A1x⊗⋯⊗Anx。我们先将假设每次都取了 A i x A_i^x Aix,然后通过判断是否要异或上 A i ′ = A i x ⊗ A i y A_i^{'}=A_i^x\otimes A_i^y Ai′=Aix⊗Aiy来选择是选择 A i x A_i^x Aix还是 A i y A_i^y Aiy。如, A x o r ′ ⊗ A i ′ = A 0 x ⊗ ⋯ ⊗ A i y ⊗ ⋯ ⊗ A n x A_{xor}^{'}\otimes A_i^{'}=A_0^{x}\otimes \dots \otimes A_i^{y} \otimes \dots \otimes A_n^{x} Axor′⊗Ai′=A0x⊗⋯⊗Aiy⊗⋯⊗Anx。那么我们可以对集合 { A 0 ′ , A 1 ′ , … , A n ′ } \{ A_0^{'},A_1^{'},\dots,A_n^{'}\} {A0′,A1′,…,An′}构造线性基 P A = { P A 0 , P A 1 , … , P A 63 } \boldsymbol{PA}=\{PA_0,PA_1,\dots,PA_{63}\} PA={PA0,PA1,…,PA63}.其中, P A i PA_i PAi为最高位在 i i i上的基。同理可以对 { B 0 ′ , … , B m ′ } \{B_0^{'},\dots,B_m^{'}\} {B0′,…,Bm′}构造出线性基 P B = { P B 0 , P B 1 , … , P B 63 } \boldsymbol{PB}=\{PB_0,PB_1,\dots,PB_{63}\} PB={PB0,PB1,…,PB63}.
我们首先得到一个 a n s ′ = A x o r ′ ⊗ B x o r ′ ans'=A_{xor}^{'}\otimes B_{xor}^{'} ans′=Axor′⊗Bxor′。 接下来要做的是根据 P A , P B \boldsymbol{PA,PB} PA,PB来使得 a n s ′ ans' ans′满足题目要求。因为线性基的性质,我们从高位开始判断。我们用 a n s ′ [ i ] ans'[i] ans′[i]表示 a n s ′ ans' ans′在第 i i i位上是 0 , 1 0,1 0,1。那么对于第 i i i位来说,存在以下8中情况。用 ( a n s ′ [ i ] , P A i , P B i ) (ans'[i],PA_i,PB_i) (ans′[i],PAi,PBi)来表示。其中,如果 P A i PA_i PAi不为0,表示 P A i PA_i PAi能够控制当前位置上是 0 0 0还是 1 1 1. P B i PB_i PBi同理。
除了 4 , 8 4,8 4,8两种情况,其他情况均很显然。重点是如何处理 4 , 8 4,8 4,8这两种情况。对于第 4 4 4种情况,无论我们是否要将 P A i PA_i PAi异或到 a n s ′ ans' ans′上, P B i PB_i PBi均要采用同样的操作。所以我们可以发现,对于当前这一位我们要么异或上 P A i , P B i PA_i,PB_i PAi,PBi,要么两个都不异或。我们发现这个时候 P A i PA_i PAi实际上并没有被使用。我们把 P A i PA_i PAi从线性基删去。然后构造一个新的变量 P A B i = P A i ⊗ P B i PAB_{i}=PA_i\otimes PB_i PABi=PAi⊗PBi,将 P A B i PAB_{i} PABi插入线性基 P A \boldsymbol{PA} PA。这样,如果选取了 P A B i PAB_{i} PABi,那么说明 A A A将 P A i PA_i PAi异或上 a n s ′ ans' ans′,那么 P B i PB_i PBi也要异或上,反之则均不取。
第8种情况是4的特例,我们只需要先将 a n s ′ = a n s ′ ⊗ P A i ans'=ans'\otimes PA_i ans′=ans′⊗PAi,那么情况 ( 1 , 1 , 1 ) (1,1,1) (1,1,1)就变成了 ( 0 , 1 , 1 ) (0,1,1) (0,1,1).
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include