#P8743. Alice and Bob's XOR Game
Alice and Bob's XOR Game
Alice and Bob's XOR Game
Alice and Bob are playing a game with XOR operations. Initially, Alice and Bob have two integers \(a\) and \(b\), and there is a common sequence of \(n\) integers \(X_1, X_2, \dots, X_n\). The players take turns for exactly \(n\) rounds (with Alice moving first). In each round, the current player chooses one unused number \(X_i\) from the sequence and applies one of the following two actions:
Option 1: Update Alice's number \(a\) by performing \(a \oplus X_i\) (here \(\oplus\) denotes the bitwise XOR operation).
Option 2: Update Bob's number \(b\) by performing \(b \oplus X_i\).
Each \(X_i\) can be used exactly once. When all numbers have been used, the game ends. The winner is the one whose final number is strictly greater than the other’s. If both numbers are equal, the game is a draw.
Both players play optimally. Determine who will win the game.
Game analysis via bits:
Since XOR is applied bit‐by‐bit, the final outcome is determined by the highest bit position (say \(k\)) that can be influenced by at least one move (i.e. where at least one \(X_i\) has its \(k\)th bit set). Let \(cnt\) be the number of \(X_i\) with the \(k\)th bit set. Then the optimal play outcome is decided as follows (for the highest such bit):
[ \text{If } cnt ;%; 2 = 0 \quad \Longrightarrow \quad \text{this bit remains balanced; check lower bits.} ]
[ \text{Otherwise, if } cnt ;%; 2 = 1 \text{:} ]
- If \(cnt \;\%\; 4 = 1\), then Alice can force a win.
- If \(cnt \;\%\; 4 = 3\), then the outcome depends on the parity of \(n - cnt\):
- If \((n - cnt) \;\%\; 2 = 0\), then Alice wins.
- If \((n - cnt) \;\%\; 2 = 1\), then Bob wins.
If no bit position is affected (i.e. none of the \(X_i\) contains a set bit) then no move changes the numbers and the final outcome is determined by the initial comparison between \(a\) and \(b\): if \(a > b\), Alice wins; if \(a < b\), Bob wins; otherwise the game is a draw.
Your task is to implement a program that, given \(a\), \(b\), \(n\) and the sequence \(X_1, X_2, \dots, X_n\), outputs the result of the game: either Alice
, Bob
, or Draw
.
inputFormat
The first line contains three integers \(a\), \(b\) and \(n\).
The second line contains \(n\) integers representing the sequence \(X_1, X_2, \dots, X_n\).
\(0 \le a, b \le 10^{18}\), \(1 \le n \le 10^5\), and \(0 \le X_i \le 10^{18}\).
outputFormat
Output a single line containing either Alice
, Bob
, or Draw
depending on the outcome of the game under optimal play.
sample
0 0 1
1
Alice
</p>