跳到正文

题解

P11008 『STA - R7』异或生成序列 题解

Mis_约 7 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年5月15日
篇幅
约 7 分钟
获赞
12
题目
P11008

版本

  1. v1当前

题目大意:给出一个序列 bb,求一个排列 pp,满足 ∀1≤i≤n−1,pi⊕pi+1=bi{\forall} 1 \le i \le n - 1, p_i \oplus p_{i + 1} = b_i,其中 ⊕\oplus 代表异或操作。

首先我们发现,如果我们知道 p1p_1,那么我们就可以直接代入序列 bb,就可以算出 pp。

对于奇数,我们可以先算出 x=1⊕2⊕⋯⊕nx = 1\oplus2\oplus\cdots \oplus n,然后我们直接取 p1=x⊕b2⊕b4⊕⋯⊕bn−1p_1 = x \oplus b_2\oplus b_4 \oplus\cdots \oplus b_{n-1} 即可,原因是把后面的展开就是 p2⊕p3⊕⋯⊕pnp_2\oplus p_3 \oplus\cdots \oplus p_n,然后异或上 xx 就可以算出来了。

对于偶数,比较暴力的想法是我们枚举 p1∈{1,2,⋯ ,n}p_1 \in \{1, 2, \cdots, n\},然后代入求解,但时间复杂度显然是 O(n2)O(n^2) 的,不可行。

我们可以转而去考虑一个序列是排列的充要条件。

一个序列 pp 是排列,当且仅当

  • min⁡i=1npi=1\min\limits _{i = 1}^{n} p_i = 1。

  • max⁡i=1npi=n\max\limits _{i = 1}^{n} p_i = n。

  • 序列中元素各不相同。

这个充要条件显然是成立的,然后我们就可以开始我们的正解。

我们先求出 bb 序列的前缀异或和 aa,那么展开我们就知道 ai=p1⊕pi+1a_i = p_1\oplus p_{i+1}。

那么我们考虑枚举完 p1p_1 后,只要序列 {p1,p1⊕a1,p1⊕a2,⋯ ,p1⊕an−1}\{p_1, p_1\oplus a_1, p_1\oplus a_2,\cdots, p_1\oplus a_{n-1}\} 满足这三个条件,那么我们就找到了一组答案。

我们可以说明,第三个条件是可以省略的。因为如果 p1=p1⊕axp_1 = p_1\oplus a_x 或者 p1⊕ai=p1⊕ajp_1\oplus a_i = p_1 \oplus a_j,我们可以分别得到 ax=0a_x = 0 或者 ai=aja_i = a_j。但我们注意到这两个条件都是与枚举的 p1p_1 无关的。所以这样的话整个问题就无解了,但是题目保证有解,所以我们可以无视第三个条件。

同时,对于第一个条件,因为参与异或的都是自然数,我们只要保证最小值不是 00 就可以了,也就是枚举的 p1∉{a}p_1 \not\in\{a\}。

那么我们现在只需要处理对于一个数 p1p_1,求 max⁡i=1n−1(p1⊕ai)\max\limits_{i = 1}^{n-1} (p_1\oplus a_i)。我们可以把 aa 里的元素拍到一棵 01 Trie 上,每次在树上贪心地向 p1p_1 在第 kk 位的取反那边走,就可以求解最大值了。复杂度 O(nlog⁡n)O(n\log n)。

注意多测,要清空 Trie。

代码:

C++
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e6 + 5;
int T, n, a[maxn], cnt[maxn], ans[maxn], vis[maxn];
int read() {
	int sum = 0;
	char c = getchar();
	while(!isdigit(c))
		c = getchar();
	while(isdigit(c))
		sum = sum * 10 + c - '0', c = getchar();
	return sum;
}
int cal(int x) {
	int len = 0;
	while(x)
		len++, x /= 2;
	return len;
}
struct Trie {
	int tr[maxn * 30][2], tot, len;
	void insert(int x) {
		int p = 0;
		for (int i = len; i >= 1; i--) {
			int t = ((x >> (i - 1)) & 1);
	//		cout << x << " " << " " << i << " " << t << endl;
			if(!tr[p][t])
				tr[p][t] = ++tot;
			p = tr[p][t];
		}
	}
	int query(int x) {
		int p = 0, ans = 0;
		for (int i = len; i >= 1; i--) {
			int t = ((x >> (i - 1)) & 1);
	//		if(x == 4)
	//			cout << tr[p][t] << " " << tr[p][1 - t] << " " << ans << endl;
			if(tr[p][1 - t])
				ans |= (1 << i - 1), p = tr[p][1 - t];
			else
				p = tr[p][t];
		//	cout << p << endl;
		}
		return ans;
	}
	void clear() {
		for (int i = 0; i <= tot; i++)
			tr[i][0] = tr[i][1] = 0;
		tot = len = 0;
	}
} tree;
void solve() {
	n = read();
	int t = 0;
	for (int i = 1; i < n; i++)
		a[i] = read(), t ^= i, ans[i] = 0, vis[i] = 0;
//	cout << n << endl;
	ans[n] = 0;
	int len = cal(2 * n), tmx = 0;
	for (int i = 1; i <= len; i++)
		cnt[i] = 0;
	for (int i = 1; i <= n; i++) {
		int tmp = i;
		for (int j = 1; j <= len; j++)
			cnt[j] += (tmp & 1), tmp /= 2;
	}
	t ^= n;
	int x = 0;
	if(n % 2) {
		for (int i = 2; i < n; i += 2)
			x ^= a[i];
		int lst = 0;
		printf("%d ", (lst = (t ^ x)));
		for (int i = 2; i <= n; i++)
			printf("%d ", (lst = (lst ^ a[i - 1])));
		putchar('\n');
	}
	else {
		tree.clear();
		tree.len = len;
		for (int i = 2; i < n; i++)
			a[i] ^= a[i - 1];
	//	cout << tree.tot << endl;
		for (int i = 1; i < n; i++)
			tree.insert(a[i]), vis[a[i]] = 1;
		for (int i = 1; i < n; i++) {
//			cout << tree.query(i) << endl;
			if(tree.query(i) == n && !vis[i]) {
				ans[1] = i;
	//			break;
			}
		}
		if(tree.query(n) == n - 1 && !vis[n])
			ans[1] = n;
		cout << ans[1] << " ";
		for (int i = 1; i < n; i++)  {
			cout << (ans[1] ^ a[i]) << " ";
		}
		for (int i = 1; i < n; i++)
			vis[a[i]] = 0;
		cout << endl;
	}
}
int main() {
	int T;
	cin >> T;
	while(T--)
		solve();
	return 0;
}
//0821_qwq
原文