2 条题解
-
3
题目分析
首先,我们要知道一个基本的定理:
这是因为异或相同为 ,不同为 。所以两个相同的数异或的值为 。
知道了这个之后,我们就可以把题目中的式子全部都异或起来,变成这样:
$$\overbrace{(a_1\oplus a_1\oplus \ldots \oplus a_1)}^{n-1}\oplus \overbrace{(a_2\oplus a_2\oplus \ldots \oplus a_2)}^{n-1}\oplus \ldots \oplus \overbrace{(a_n\oplus a_n\oplus \ldots \oplus a_n)}^{n-1}=b_1\oplus b_2\oplus \ldots\oplus b_n $$因为 是偶数,所以可以化简成:
$$a_1\oplus a_2\oplus \ldots \oplus a_n=b_1\oplus b_2\oplus \ldots \oplus b_n $$我们令 为 ,则:
把这个式子和题目中第一个式子异或,得:
$$a_1\oplus (a_2\oplus a_2)\oplus (a_3\oplus a_3)\oplus \ldots \oplus (a_n\oplus a_n)=x\oplus b_1 $$化简一下,得:
同理可证,。
于是,我们就可以得出答案了。
AC 代码
#include<bits/stdc++.h> using namespace std; int a[200100]; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n,k=0; cin>>n; for (int i=1;i<=n;i++) { cin>>a[i]; k^=a[i]; } for (int i=1;i<=n;i++) cout<<(k^a[i])<<' '; return 0; }
-
2
题目翻译
给出有 个数的序列 ,还原序列 ,使 为除 以外所有数的异或和。 。 。
题目思路
前置知识:异或的归零律、恒等律与自反 。
观察题意可得我们求出的 即为 再去除一个 。
设 。
我们根据异或的自反可得:
$$\begin{aligned}k\oplus b_i&=b_1\oplus b_2\oplus \dots \oplus b_n\oplus b_i\\&=b_1\oplus b_2\oplus\dots\oplus b_{i-1}\oplus b_{i+1}\oplus b_{i+2}\oplus \dots \oplus b_n\end{aligned} $$于是我们得到了想求的 数组。
完整代码
#include<bits/stdc++.h> using namespace std; int n,k; int a[200020]; int main() { cin>>n; for(int i=1;i<=n;i++) { cin>>a[i]; k^=a[i]; } for(int i=1;i<=n;i++) { cout<<(k^a[i])<<" \n"[i==n]; } return 0; }
- 1
信息
- ID
- 647
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 3
- 标签
- 递交数
- 8
- 已通过
- 7
- 上传者