题解 AT5140 【[AGC035C] Skolem XOR Tree】

· · 题解

[AGC035C] Skolem XOR Tree

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

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

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

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

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

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

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

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

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

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

#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;
}