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 of as a fixed-length binary string. Even if loses leading zeroes as an integer, you can still compare both numbers using the original length of .
If has bits from low to high, then the -th bit of , padded to length , is .
So the output bit is . That immediately gives . In other words, must become a binary palindrome after adding some leading zeroes.
There is one extra trap: if is odd, the middle bit is . So a valid palindrome of odd length must have middle bit .
For , the number of leading zeroes you add is forced to equal the number of trailing zeroes of . Remove all trailing zeroes from ; the remaining binary string must be a palindrome, and if its length is odd, its middle bit must be .
Let have binary length . Write its bits from least significant to most significant as
with .
Reversing the binary representation means that, if we still view using exactly padded bits, its bit at position is .
So if
then the -th bit of , padded to length , is
Now the whole problem basically folds in half.
The symmetry
Because XOR is symmetric,
So the padded binary representation of must be a palindrome.
Example: . Add one leading zero and get , which is a palindrome, so is possible.
Example: . The tempting padded palindrome is , but that has a problem we still need to catch.
The middle bit trap
If is odd, there is a middle bit . Its output bit is
So any valid odd-length palindrome must have middle bit . This kills , because the only reasonable palindrome shape is , whose middle bit is . Binary is not here to be vibes-based; the center bit has to behave.
Which leading zeroes can we add?
For , the answer is immediately YES: take , then , so .
Now assume .
Let the usual binary representation of be . We are allowed to add leading zeroes because may have a longer length than .
Suppose we add leading zeroes. For the padded string to be a palindrome, those leading zeroes must match the last bits. Therefore must have at least trailing zeroes.
But if we do not remove all trailing zeroes, the remaining inner string would start with and end with , so it cannot be a palindrome. Therefore is forced:
So the test becomes very simple:
YES.If all checks pass, answer YES; otherwise answer NO.
Why this is sufficient
We proved every possible answer must satisfy those conditions. Now we also need to know that the conditions are enough.
Assume the padded bits form a palindrome, and the middle bit is when is odd. We can construct bits of pair by pair.
For each pair , we only need
That is always possible: choose one bit freely, then the other is forced. For the outer pair, choose so that really has length , and set . The middle bit, if it exists, requires , which we already checked.
So the condition is both necessary and sufficient. No hidden nonsense.
Complexity
, so checking bits takes per test case. With tests, this is tiny.
#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--) {
int n;
cin >> n;
if (n == 0) {
cout << "YES\n";
continue;
}
while ((n & 1) == 0) n >>= 1;
vector<int> bits;
while (n > 0) {
bits.push_back(n & 1);
n >>= 1;
}
bool ok = true;
int m = (int)bits.size();
for (int i = 0; i < m; i++) {
if (bits[i] != bits[m - 1 - i]) ok = false;
}
if (m % 2 == 1 && bits[m / 2] == 1) ok = false;
cout << (ok ? "YES\n" : "NO\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--) {
int n;
cin >> n;
if (n == 0) {
cout << "YES\n";
continue;
}
while ((n & 1) == 0) n >>= 1;
vector<int> bits;
while (n > 0) {
bits.push_back(n & 1);
n >>= 1;
}
bool ok = true;
int m = (int)bits.size();
for (int i = 0; i < m; i++) {
if (bits[i] != bits[m - 1 - i]) ok = false;
}
if (m % 2 == 1 && bits[m / 2] == 1) ok = false;
cout << (ok ? "YES\n" : "NO\n");
}
}