做题记录 25.2.7

\(\textcolor{purple}\odot\) [AGC035C] Skolem XOR Tree

\(1\) 为根,对于每个偶数 \(x\)\(1<x< n\)),将 \(x\)\(x+1\) 一起考虑

可构造 \(x-(x+1)-1-(x+n)-(x+n+1)\),显然 \(x\oplus (x+1)\oplus 1\oplus x=x\)\((x+1)\oplus 1\oplus x\oplus (x+1)=x+1\),即 \(x\)\(x+1\) 都满足要求

然后考虑节点 \(1\),令 \(u\) 为任意一个叶子(取 \([1,n)\) 的偶数即可),则可令 \(n+1\) 接在这个叶子下方,\(1\oplus u\oplus (u+1)\oplus 1=1\),符合要求

\(n\) 为偶数时,上述过程没有加入 \(n\)

\(t\)\(n\) 的任意一个二进制位(实现时取 \(\operatorname{lowbit}\) 即可),则可将 \(n\)\(n+n\) 分别接在 \(t+1\)\(n-t+n\) 下,\(n\oplus (t+1)\oplus 1\oplus (n-t)\oplus n=n\),符合要求

时间复杂度 \(O(n)\)

代码

参考

posted @ 2025-02-07 20:49  Hstry  阅读(9)  评论(0)    收藏  举报