2 条题解

  • 3
    @ 2023-5-7 12:02:19

    题目分析

    首先,我们要知道一个基本的定理:

    kk=0k \oplus k=0

    这是因为异或相同为 00,不同为 11。所以两个相同的数异或的值为 00

    知道了这个之后,我们就可以把题目中的式子全部都异或起来,变成这样:

    $$\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 $$

    因为 nn 是偶数,所以可以化简成:

    $$a_1\oplus a_2\oplus \ldots \oplus a_n=b_1\oplus b_2\oplus \ldots \oplus b_n $$

    我们令 b1b2bnb_1\oplus b_2\oplus \ldots \oplus b_nxx,则:

    a1a2an=xa_1\oplus a_2\oplus \ldots \oplus a_n=x

    把这个式子和题目中第一个式子异或,得:

    $$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 $$

    化简一下,得:

    a1=xb1a_1=x\oplus b_1

    同理可证,ai=xbia_i=x\oplus b_i

    于是,我们就可以得出答案了。

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

    信息

    ID
    647
    时间
    1000ms
    内存
    256MiB
    难度
    3
    标签
    递交数
    8
    已通过
    7
    上传者