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.
Since every and is only or , each final score is also only or . So the only real winning cases are for Ajisai and for Mai.
If , swapping does literally nothing. That index contributes the same bit to both final XORs, so it cannot be strategically controlled.
If , then after the move one player gets from this index and the other gets . The controller of this index can choose which way around that happens.
Let be the number of indices where . Across those indices, Mai's XOR differs from Ajisai's XOR by exactly . So if is even, the final scores are always equal. No drama, just Tie.
If is odd, the scores are always opposite. The last mismatching index decides the whole game, because its player moves after all earlier useful choices and can set the final parity however they want.
Because , this game looks way more complicated than it is. There are turns, swaps, optimal play, anime emotional damage, etc. But the whole thing collapses into parity.
Let Ajisai's final XOR be , and Mai's final XOR be .
Equal positions are boring
If , then swapping does nothing. The pair is either or , so both players receive the same contribution from that index.
These indices may flip both final XORs, but they never create an advantage for either player.
So the only interesting positions are the mismatches:
At such an index, the pair is either or . Whoever controls that turn can choose whether Ajisai gets or from this position. Mai gets the opposite bit.
Track only the mismatches
Suppose there are mismatching indices.
For each mismatching index, let be the final bit contributed to Ajisai's XOR. Since the pair contains one and one , the bit contributed to Mai is:
Let be the XOR contribution from all equal positions. Equal positions contribute the same thing to both players.
Then:
and
XORing exactly times adds , so:
That one equation is basically the whole problem.
Case 1: is even
Then:
The scores are always equal, regardless of what anyone does. Neither player can force a win because a win is impossible.
So the answer is:
Case 2: is odd
Then:
So the final scores are always opposite. There cannot be a tie.
Ajisai wins exactly when . Mai wins exactly when .
Now we only need to know who can force the final value of .
Every mismatching index lets its controller choose whether Ajisai receives or from that index. Since the final score depends only on XOR parity, the last mismatching index is decisive.
Why? By the time that turn happens, all earlier useful choices are fixed. The player moving at the last mismatch sees the current parity and can choose the final bit at this index to make whatever they want.
That's it. No giant minimax tree needed. The last useful move owns the parity button.
Algorithm
For each test case:
Tie.Ajisai.Mai.Complexity
We scan the arrays once.
per test case, with total over all test cases.
Memory usage is here because we store the arrays, though it could also be done with input storage as written comfortably under the limits.
#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);
for (int &x : a) cin >> x;
for (int &x : b) cin >> x;
int mismatches = 0;
int last = -1;
for (int i = 0; i < n; i++) {
if (a[i] != b[i]) {
mismatches++;
last = i;
}
}
if (mismatches % 2 == 0) {
cout << "Tie\n";
} else if (last % 2 == 0) {
cout << "Ajisai\n";
} else {
cout << "Mai\n";
}
}
return 0;
}#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);
for (int &x : a) cin >> x;
for (int &x : b) cin >> x;
int mismatches = 0;
int last = -1;
for (int i = 0; i < n; i++) {
if (a[i] != b[i]) {
mismatches++;
last = i;
}
}
if (mismatches % 2 == 0) {
cout << "Tie\n";
} else if (last % 2 == 0) {
cout << "Ajisai\n";
} else {
cout << "Mai\n";
}
}
return 0;
}