PROBLEM LINK:
Practice
Contest: Division 1
Contest: Division 2
Contest: Division 3
Contest: Division 4
Author: raysh07
Tester: sushil2006
Editorialist: iceknight1093
DIFFICULTY:
Cakewalk
PREREQUISITES:
None
PROBLEM:
Alice and Bob play a game.
They start with X = 0, and then the players add either 1 or 2 to it, alternating turns.
The game ends when X \geq N.
If X = N Bob wins, otherwise Alice wins.
Alice starts first. Who wins?
EXPLANATION:
If N = 1, clearly Alice wins by just adding 2 to X. This is also showcased in the samples.
In general, notice that since each player adds either 1 or 2, the game will end with either X = N or X = N+1.
So, the only way for Alice to win is if X = N+1 in the end; which in turn can only happen when X = N-1 and the current player adds 2.
Of course, if X = N-1 on his turn, Bob will never add 2 - he will just add 1 and reach X = N, hence winning.
This means Alice’s only hope of winning, is to herself receive N-1, and then add 2 to it.
However, as it turns out, when N \gt 1 Bob can always ensure that Alice doesn’t receive N-1.
In other words, if N \gt 1, Bob always wins!
How?
Since N \gt 1 and we start with X = 0, certainly X \neq N-1 on Alice’s first turn.
Now,
- If Bob receives X = N-1 or X = N-2 on his turn, he can simply add 1 or 2 respectively and reach X = N, immediately winning.
- Otherwise, Bob will receive X such that X \leq N-3.
He can simply always add 1, so that Alice will receive X \leq N-2 (in particular, it doesn’t equal N-1).
This way, Alice can never win.
In summary, Alice wins N = 1 and loses otherwise.
TIME COMPLEXITY:
\mathcal{O}(1) per testcase.
CODE:
Editorialist's code (PyPy3)
for _ in range(int(input())):
n = int(input())
print('Alice' if n == 1 else 'Bob')