题目大意
有一个长度为 n n n 的序列 b b b( n n n 为偶数)。现在已知序列 a a a,满足 a i = b 1 ⊕ ⋯ ⊕ b i − 1 ⊕ ⋯ b n a_i = b_1 \oplus \cdots \oplus b_{i-1} \oplus \cdots b_n ai=b1⊕⋯⊕bi−1⊕⋯bn,要求还原出 b b b 序列
前置芝士
解题思路
设
t
o
t
=
a
1
⊕
a
2
⊕
⋯
⊕
a
n
−
1
⊕
a
n
tot = a_1 \oplus a_2 \oplus \cdots \oplus a_{n-1} \oplus a_n
tot=a1⊕a2⊕⋯⊕an−1⊕an
可以发现,所有
b
i
b_i
bi 都在里面异或了
n
−
1
n-1
n−1 次
又因为
n
n
n 为偶数(
n
−
1
n-1
n−1 为奇数),所以最后的到的结果
t
o
t
=
b
1
⊕
b
2
⊕
⋯
⊕
b
n
−
1
⊕
b
n
tot = b_1 \oplus b_2 \oplus \cdots \oplus b_{n-1} \oplus b_n
tot=b1⊕b2⊕⋯⊕bn−1⊕bn
最后
t
o
t
⊕
a
i
tot \oplus a_i
tot⊕ai 就是
b
i
b_i
bi 的值
#include<cstdio>
#include<iostream>
using namespace std;
const int Maxn=300000+20;
int n,tot;
int a[Maxn],b[Maxn];
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;++i)
{
scanf("%d",&a[i]);
tot^=a[i];
}
for(int i=1;i<=n;++i)
{
b[i]=tot^a[i];
printf("%d ",b[i]);
}
putchar('\n');
return 0;
}

本文解析了一种基于异或运算的序列还原算法,适用于已知由原序列通过异或操作生成的新序列,求还原原序列的问题。文章详细介绍了算法的原理,包括前置芝士的异或基础知识,解题思路及其实现代码。
628

被折叠的 条评论
为什么被折叠?



