『题解』Codeforces 1758B XOR = Average

Description

构造一个 \(a\) 序列,使 \(a_1 \oplus a_2 \oplus a_3 \oplus \cdots \oplus a_n = \dfrac{sum(a)}{n}\)

Solution

第一眼看到这道题,我想到的是分情况讨论。

首先看 \(n\) 是奇数,我们知道 \(x \oplus x = 0\),所以可以想到 \(1 \oplus 1 \oplus 1 \oplus \cdots \oplus 1 = \dfrac{1 \times n}{n} = 1\)。也就是说,假如 \(n\) 是奇数,输出 \(n\)\(1\) 就可以了。

再来看 \(n\) 是偶数,在 \(n\) 为奇数的解上稍作修改,因为 \(1 \oplus 3 = 2\),所以根据 \(n\) 是奇数的思维,就可以想到 \(1 \oplus 2 \oplus 2 \oplus \cdots \oplus 2 \oplus 3 = \dfrac{1+2+2+\cdots+2+3}{n} = 2\)

这道题很轻松的就解出来啦!

Code

#include <bits/stdc++.h>
using namespace std;

int main()
{
    int t;
    cin >> t;
    while (t --)
    {
        int n;
        cin >> n;
        if (n % 2)
        {
            for (int i = 1; i <= n; i ++)
                cout << 1 << " ";
            cout << endl;
        }
        else
        {
            cout << 1 << " ";
            for (int i = 2; i <= n - 1; i ++)
                cout << 2 << " ";
            cout << 3;
            cout << endl;
        }
    }
    return 0;
}
posted @ 2022-11-27 15:42  Clyfort  阅读(92)  评论(0编辑  收藏  举报