CF2180C.XOR-factorization

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Ostad thinks that the usual way of factoring numbers is too mathematical, so he invented a new notion called XOR-factorization, which is more computer-science-like. For a given integer nn, a sequence of integers a1,a2,…,aka_1, a_2, \ldots, a_k with 0≤ai≤n0 \le a_i \le n for all ii is called a XOR-factorization of nn if and only if $$ a_1 \oplus a_2 \oplus \cdots \oplus a_k = n, $$ where ⊕\oplus denotes the bitwise XOR operation.

You are given integers nn and kk. Find a XOR-factorization a1,a2,…,aka_1, a_2, \ldots, a_k of nn that maximizes the sum a1+a2+⋯+aka_1 + a_2 + \cdots + a_k.

It can be proven that under the problem conditions, a XOR-factorization always exists.

奥斯塔德认为,传统的因数分解方式过于数学化,因此他发明了一种名为“异或分解(XOR-factorization)”的新概念,该概念更具计算机科学特色。对于给定的整数 nn,若一个整数序列 a1,a2,…,aka_1, a_2, \ldots, a_k 满足对所有 ii 都有 0≤ai≤n0 \le a_i \le n,且

a_1oplusa_2opluscdotsoplusa_k=n,a\_1 \\oplus a\_2 \\oplus \\cdots \\oplus a\_k = n,

则称该序列为 nn 的一个异或分解,其中 ⊕\oplus 表示按位异或运算。

现给你两个整数 nn 和 kk。请找出 nn 的一个异或分解 a1,a2,…,aka_1, a_2, \ldots, a_k,使得其元素之和 a1+a2+⋯+aka_1 + a_2 + \cdots + a_k 最大。

在本题约束条件下,可以证明异或分解一定存在。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1041 \le t \le 10^4). The description of the test cases follows.

Each of the next tt lines contains two integers nn and kk (1≤n≤1091 \le n \le 10^9, 1≤k≤1051 \le k \le 10^5).

It is guaranteed that the sum of kk over all test cases does not exceed 10510^5.

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤1041 \le t \le 10^4)。随后是测试用例的描述。

接下来的 tt 行中,每行包含两个整数 nn 和 kk(1≤n≤1091 \le n \le 10^9,1≤k≤1051 \le k \le 10^5)。

保证所有测试用例中 kk 的总和不超过 10510^5。

输出格式

For each test case, output kk integers a1,a2,…,aka_1, a_2, \ldots, a_k such that 0≤ai≤n0 \le a_i \le n.

We can show that an answer always exists. If there are multiple valid answers, you may print any of them in any order.

对于每个测试用例,输出 kk 个整数 a1,a2,…,aka_1, a_2, \ldots, a_k,满足 0≤ai≤n0 \le a_i \le n。

可以证明答案一定存在。如果存在多个合法的答案,你可以以任意顺序输出其中任意一个。

输入输出样例

  • 输入#1

    4
    5 4
    4 3
    8 2
    1 1

    输出#1

    1 4 5 5
    4 4 4
    0 8
    1

说明/提示

In the first test case, we can factor 55 as 1⊕4⊕5⊕51 \oplus 4 \oplus 5 \oplus 5 with a sum of 1515, and it can be shown that no other XOR-factorization has a higher sum.

In the second test case, we can factor 44 as 4⊕4⊕44 \oplus 4 \oplus 4 with a sum of 1212, which is trivially the maximum possible.

在第一个测试用例中,我们可以将 55 分解为 1⊕4⊕5⊕51 \oplus 4 \oplus 5 \oplus 5,其和为 1515,且可以证明不存在其他异或分解方式能得到更高的和。

在第二个测试用例中,我们可以将 44 分解为 4⊕4⊕44 \oplus 4 \oplus 4,其和为 1212,这显然是可能的最大值。

输入解题思路,AI测评打分。不知道怎么写?

首页