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;
    }
    
    • 2
      @ 2023-5-7 14:47:49

      更好的阅读体验

      题目翻译

      给出有 nn 个数的序列 bb,还原序列 aa,使 aia_i 为除 bib_i 以外所有数的异或和。 2n2×106,0ai1092\leq n\leq 2\times 10^6,0\leq a_i\leq 10^9n0(mod2)n\equiv0\pmod{2}

      题目思路

      前置知识:异或的归零律、恒等律与自反 aa=0,a0=a,aba=ba\oplus a=0,a\oplus 0=a,a\oplus b\oplus a=b

      观察题意可得我们求出的 aia_i 即为 b1b2bnb_1\oplus b_2\oplus \dots \oplus b_n 再去除一个 bib_i

      b1b2bn=kb_1\oplus b_2\oplus \dots \oplus b_n=k

      我们根据异或的自反可得:

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

      于是我们得到了想求的 aa 数组。

      完整代码

      #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
      上传者