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 about what a swap at one index does to the two final XOR scores. It does not only affect one player’s score.
Let and . Swapping index changes both XORs by the same value: .
Because both final scores are XORed by the same total mask, their XOR difference never changes. So if initially, nobody can ever break the tie. Very dramatic game, zero actual drama.
If , only the highest bit where and differ matters for deciding who has the larger final number. Lower bits are noise.
For that highest differing bit, look at indices where contains that bit. The owner of the last such index gets the final say, because they can either flip the deciding bit or leave it alone.
Let
and
At index , swapping and changes Ajisai's XOR by
and it changes Mai's XOR by the exact same value. So if the total XOR of all swapped index-differences is , the final scores are
That means
So the set of bits where the two final scores differ is fixed forever. The players can wiggle the numbers around, but they cannot change where the comparison is decided.
If
Then , and after any sequence of swaps we still have .
Answer: Tie.
Now suppose
Let be the highest bit where and differ. This is the only bit that matters for deciding which final score is larger. Higher bits are equal, and lower bits are irrelevant.
So:
A swap at index toggles bit of exactly when bit of
is .
All other indices are useless for deciding the winner. Decorative nonsense, basically.
Now focus only on indices where has bit set. Each such turn gives its owner one choice: flip the deciding bit, or don't.
The key observation: among these relevant indices, the player who owns the last one completely controls the final deciding bit.
Why? Before that last relevant move, the deciding bit has some value. On that last move, the player may either pass and keep it, or swap and flip it. Therefore they can force whichever final value they want.
Ajisai wants bit of to be . Mai wants it to be $0`.
So:
There must be at least one relevant index because is set in
So the algorithm is:
Tie.Ajisai, even index means Mai.Remember the statement uses 1-indexing. In C++, index 0 is turn 1, so i % 2 == 0 means Ajisai.
Time complexity: per test case.
Memory complexity: with stored arrays.
#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;
vector<int> a(n), b(n);
int xrA = 0, xrB = 0;
for (int i = 0; i < n; i++) {
cin >> a[i];
xrA ^= a[i];
}
for (int i = 0; i < n; i++) {
cin >> b[i];
xrB ^= b[i];
}
int diff = xrA ^ xrB;
if (diff == 0) {
cout << "Tie\n";
continue;
}
int bit = 31 - __builtin_clz(diff);
int last = -1;
for (int i = 0; i < n; i++) {
if (((a[i] ^ b[i]) >> bit) & 1) {
last = i;
}
}
if (last % 2 == 0) cout << "Ajisai\n";
else cout << "Mai\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;
vector<int> a(n), b(n);
int xrA = 0, xrB = 0;
for (int i = 0; i < n; i++) {
cin >> a[i];
xrA ^= a[i];
}
for (int i = 0; i < n; i++) {
cin >> b[i];
xrB ^= b[i];
}
int diff = xrA ^ xrB;
if (diff == 0) {
cout << "Tie\n";
continue;
}
int bit = 31 - __builtin_clz(diff);
int last = -1;
for (int i = 0; i < n; i++) {
if (((a[i] ^ b[i]) >> bit) & 1) {
last = i;
}
}
if (last % 2 == 0) cout << "Ajisai\n";
else cout << "Mai\n";
}
}