跳到正文

题解

题解 AT5140 【[AGC035C] Skolem XOR Tree】

chen_zhe约 5 分钟
原文
文章信息与版本

文章信息

发布
2020年5月6日
收录
2025年12月12日
更新
2026年8月21日
篇幅
约 5 分钟

版本

  1. v2当前
  2. v1

[AGC035C] Skolem XOR Tree

月考之余做做构造题玩玩。

这个题比较好地考察了一些异或的性质。在一般的情况下我们用的较多的是异或的交换律,以及自己异或自己为 00。但是这个题不一样。

首先,我们要知道一点,对于一个偶数 aa,必定存在 a⨁(a+1)=1a \bigoplus (a+1) =1。因为偶数的二进制是形如 1xxxxx0 的,加上 11 就形如 1xxxxx1 了,根据异或的计算方式,前面都是相同位,故为 00,只有最后一位为 11。这个性质应该可以推广到 2n∣a2^n \mid a 时,∀b∈[1,2n)∩Z,a⨁(a+b)=b\forall b \in [1,2^n) \cap Z,a \bigoplus (a+b)=b。证明同上。

接着还有一点,不妨设 a>ba>b,那么 a⨁ba \bigoplus b 的结果不可能超过 aa 最高位的 11 所对应的位权乘以 22。证明显然,因为最高位的 11 对应的位权乘以 22,代表着 aa 最高位的左边的一位,这一位必定是 00。而 a>ba>b,所以 bb 的那一位也是 00,0⨁0=00 \bigoplus 0=0,故不存在。


那么我们知道了这么多奇怪的性质,能做什么呢?

首先考虑一下这个题的无解情况。当 nn 属于 22 的整数次幂时,那么这个时候节点 n,2nn,2n 都赋权为 nn,这样两者异或结果是 00。而中间经过的其他节点的权值是必定小于 nn 的。因为 nn 正好是 22 的整数次幂,因而小于 nn 的数字,在 nn 的最高位 11 所对应的二进制位上必定是 00。因此,凑不出异或和为 nn 这个条件。故此时无解。

那么别的情况下呢?其实根据第一条性质,会发现别的情况一定有解。

既然对于偶数 aa 来说,a⨁(a+1)=1a \bigoplus (a+1)=1,而且又根据 1⨁1=01 \bigoplus 1=0,那么我们可以考虑根据这两点,考虑整两个 11 对消,这样就可以保留 aa 和 (a+1)(a+1) 了。因此经过尝试,可以发现,联结下述路径:1→a,1→(a+1),a→(a+n+1),(a+1)→(a+n)1 \to a,1 \to (a+1),a \to (a+n+1),(a+1) \to (a+n),这样可以满足 aa 和 a+1a+1 这两个节点。

然后会发现这么做,要对 11 进行特殊判断,我们会发现,1⨁2⨁3=01 \bigoplus 2 \bigoplus 3=0。(当然如果一直异或到任意一个 2n−12^{n-1} 都是成立的,不再展开)。因此,我们可以链接 1→2,2→3,3→(n+1),(n+1)→(n+2),(n+2)→(n+3)1 \to 2,2 \to 3,3 \to (n+1),(n+1) \to (n+2),(n+2) \to (n+3),这样就可以保证了。

因为 a+n+1a+n+1,当 2∣n2\mid n 且 a=na=n 时会超过 nn,因此也要进行特殊处理。我们可以找到两个正整数 (x,y)(x,y),使得 x⨁y⨁1=nx \bigoplus y \bigoplus 1=n。此时只需连接 n→x,2n→yn \to x,2n \to y 即可。注意,对 (x,y)(x,y) 应该对 33 进行特判,因为 33 是被特殊构造出来的。

C++
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cctype>
#include <queue>
#include <vector>

using namespace std;

inline int read()
{
	int x=0,f=1;char ch=getchar();
	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
	while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
	return x*f;
}

int main()
{
	int n=read();
	if (!(n&(n-1)))
		puts("No");
	else
	{
		puts("Yes");
		puts("1 2");
		puts("2 3");
		cout << 3 << " " << n+1 << endl;
		cout << n+1 << " " << n+2 << endl;
		cout << n+2 << " " << n+3 << endl;
		for (int i=4;i+1<=n;i+=2)
		{
			int j=i+1;
			cout << 1 << " " << i << endl;
			cout << 1 << " " << j << endl;
			cout << i << " " << j+n << endl;
			cout << j << " " << i+n << endl;
		}
		if (n%2==0)
		{
			for (int i=4;i<=n;i++)
			{
				int j=n^i^1;
				if (j!=3 && j<n)
				{
					cout << i << " " << n << endl;
					cout << j << " " << 2*n << endl;
					return 0;
				}
			}
		}
	}
	return 0;
}