Search for a command to run...
Progressive hints first, then the full explanation and implementation when you're ready to cash out.
Review status
AI-generated and still unreviewed. Double-check the details before internalizing them.
Hints
Open only as much as you need to keep the solve alive.
Think per bit. The XOR condition only says whether the number of chosen values with bit set is odd or even, while the sum wants as many set bits as possible.
If is odd, the whole problem collapses: printing exactly times gives XOR and reaches the absolute upper bound .
For even , scan bits from high to low and split numbers into two groups: still tight with the prefix of , or already loose because they became smaller than earlier.
At a bit where has , tight numbers are forced to write . Only loose numbers may write , and you want the largest even count of them.
At a bit where has , exactly one number should write . If any tight number exists, use it as that zero and make it loose; otherwise use any loose number. Then everyone else writes .
The tempting shortcut is: for even , print copies of and solve only two numbers. That is false. For example, with , , the sequence has XOR and sum , beating the best two-number-style construction. So yeah, that idea dies pretty fast.
The sum is just the contribution of every bit. If bit is set in of the numbers, it contributes
to the total sum.
The XOR condition says only this:
But every number must also satisfy , and that is where the real problem lives.
If is odd, print every time.
The XOR of an odd number of copies of is , and the sum is , which is the absolute maximum because every . Done. No drama.
Now assume is even.
Scan bits from high to low. For each constructed number, keep whether it is:
tight: its already-built prefix is exactly equal to the prefix of ;loose: its prefix is already smaller than .A tight number has restrictions. If the current bit of is 1n$.
A loose number is already smaller, so all lower bits are free.
Suppose the current bit of is .
All tight numbers must write . If there are loose numbers, we can write in some of those loose numbers. Since the XOR bit must be , the count of ones must be even.
So the best count is:
This choice has no future downside, because it does not change which numbers are loose.
Suppose the current bit of is .
Because is even, to make the XOR bit equal to , the number of ones must be odd. The largest odd count at most is , so exactly one number should write and everyone else should write .
Could writing three zeros ever help by making more numbers loose? Nope. At bit , every extra pair of zeros loses from the current bit. Those two extra loose numbers can gain at most from all lower bits combined, which is strictly smaller. So extra zeros are a bad trade. Don't buy that garbage.
Now, if we need exactly one zero, where should it go?
If there is a tight number, put the zero there. It becomes loose. The current bit sum is unchanged, and having more loose numbers can only help future bits. If all numbers are already loose, put the zero anywhere.
Maintain two lists of indices: tight and loose.
For each bit from high to low:
tight and move it to loose;take be the largest even number not exceeding loose.size();take loose numbers.Every bit has exactly the maximum count of ones that can be optimal under the tight/loose restriction, and the choices greedily maximize future freedom. Therefore the resulting sequence has maximum possible sum.
For odd , every output number is , so each number is valid, the XOR is , and the sum is , the largest possible sum. Thus it is optimal.
For even , consider the high-to-low construction.
A number in tight always matches the processed prefix of . A number in loose already has a smaller processed prefix than . Therefore, tight numbers may only put on a current bit if also has there, while loose numbers may put either bit. This invariant proves every constructed number stays at most .
On a bit where has , tight numbers are forced to write . Among loose numbers, the XOR requires an even number of ones, so the largest possible count is the largest even number at most the loose count. The algorithm uses exactly that many, so this bit's contribution is maximal given the current state.
On a bit where has , the XOR requires an odd number of ones. Since is even, the largest possible odd count is , meaning exactly one zero. Any additional two zeros lose immediately and can recover less than that from all lower bits, so no optimal solution uses more than one zero here. The algorithm uses exactly one zero.
When that one zero can be placed on a tight number, doing so has the same current contribution as placing it on a loose number, but it increases the loose count. More loose numbers never reduce the set of possible future assignments, so this greedy choice is optimal.
Thus every bit is handled optimally, and the produced sequence maximizes the sum while satisfying the XOR and bound constraints.
There are only about relevant bits, and each bit may touch all numbers. The complexity is per test case, with total bounded by , so this easily fits.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int t;
cin >> t;
while (t--) {
ll n;
int k;
cin >> n >> k;
if (k % 2 == 1) {
for (int i = 0; i < k; i++) {
if (i) cout << ' ';
cout << n;
}
cout << '\n';
continue;
}
vector<ll> ans(k, 0);
vector<int> tight(k), loose;
iota(tight.begin(), tight.end(), 0);
for (int b = 30; b >= 0; b--) {
ll bit = 1LL << b;
if (n & bit) {
int zero;
if (!tight.empty()) {
zero = tight.back();
tight.pop_back();
loose.push_back(zero);
} else {
zero = loose[0];
}
for (int i = 0; i < k; i++) {
if (i != zero) ans[i] |= bit;
}
} else {
int take = (int)loose.size();
if (take % 2 == 1) take--;
for (int i = 0; i < take; i++) {
ans[loose[i]] |= bit;
}
}
}
for (int i = 0; i < k; i++) {
if (i) cout << ' ';
cout << ans[i];
}
cout << '\n';
}
}#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int t;
cin >> t;
while (t--) {
ll n;
int k;
cin >> n >> k;
if (k % 2 == 1) {
for (int i = 0; i < k; i++) {
if (i) cout << ' ';
cout << n;
}
cout << '\n';
continue;
}
vector<ll> ans(k, 0);
vector<int> tight(k), loose;
iota(tight.begin(), tight.end(), 0);
for (int b = 30; b >= 0; b--) {
ll bit = 1LL << b;
if (n & bit) {
int zero;
if (!tight.empty()) {
zero = tight.back();
tight.pop_back();
loose.push_back(zero);
} else {
zero = loose[0];
}
for (int i = 0; i < k; i++) {
if (i != zero) ans[i] |= bit;
}
} else {
int take = (int)loose.size();
if (take % 2 == 1) take--;
for (int i = 0; i < take; i++) {
ans[loose[i]] |= bit;
}
}
}
for (int i = 0; i < k; i++) {
if (i) cout << ' ';
cout << ans[i];
}
cout << '\n';
}
}