New Year and the Tricolore Recreation
Contest: Good Bye 2018
Difficulty rating: 3200
Tags: games

Alice and Bob play a game on a grid with $$$n$$$ rows and infinitely many columns. In each row, there are three tokens, blue, white and red one. Before the game starts and after every move, the following two conditions must hold:

- Any two tokens are not in the same cell.
- In each row, the blue token is to the left of the white token, and the red token is to the right of the white token.

First, they pick a positive integer $$$f$$$, whose value is valid for the whole game. Second, the starting player is chosen and makes his or her first turn. Then players take alternating turns. The player who is unable to make a move loses.

During a move, a player first selects an integer $$$k$$$ that is either a prime number or a product of two (not necessarily distinct) primes. The smallest possible values of $$$k$$$ are thus $$$2, 3, 4, 5, 6, 7, 9, 10, 11, 13, 14, 15, 17, 19, \dots$$$. Furthermore, $$$k$$$ must not be equal to the previously picked integer $$$f$$$. Each turn, a move is performed in exactly one of the rows.

If it is Alice's turn, she chooses a single blue token and moves it $$$k$$$ cells to the right. Alternatively, she may move both the blue and the white token in the same row by the same amount $$$k$$$ to the right.

On the other hand, Bob selects a single red token and moves it $$$k$$$ cells to the left. Similarly, he may also move the white and the red token in the corresponding row by $$$k$$$ to the left.

Note that Alice may never move a red token, while Bob may never move a blue one. Remember that after a move, the two conditions on relative positions of the tokens must still hold.

Both players play optimally. Given the initial state of the board, determine who wins for two games: if Alice starts and if Bob starts.

Input format:
The first line contains a two integers $$$n$$$ and $$$f$$$ ($$$1 \leq n \leq 10^5$$$, $$$2 \leq f \leq 2 \cdot 10^5$$$) — the number of rows and the forbidden move, respectively.

Each of the next $$$n$$$ lines contains three integers $$$b_i$$$, $$$w_i$$$, $$$r_i$$$ ($$$-10^5 \leq b_i < w_i < r_i \leq 10^5$$$) — the number of column in which the blue, white and red token lies in the $$$i$$$-th row, respectively.

Output format:
Output two lines.

The first line should contain the name of the winner when Alice starts, and the second line should contain the name of the winner when Bob starts.

Constraints:
Time limit: 4.0 s
Memory limit: 256.0 MB

Note:
The first example is as follows:

When Alice starts, she can win by moving the blue and white token to right by $$$2$$$ cells, getting into position $$$2~5~9$$$. Regardless of what Bob does, Alice will have one more move and then the game is over. For instance, he can move both the red and white token by $$$2$$$ cells to the left, reaching state $$$2~3~7$$$. Alice can then move blue and white token by $$$2$$$ to move into $$$4~5~7$$$, where no more moves are possible.

If Bob starts, he gains enough advantage to win. For instance, he may move the red token by $$$3$$$ to the left, getting into position $$$0~3~6$$$. Alice can, for example, move the blue token by $$$2$$$, which is countered by Bob by moving the red token by $$$2$$$. The game ends in position $$$2~3~4$$$.

In the second example, it is forbidden to move by $$$2$$$, but this doesn't stop Alice from winning! She can move the blue and white token by $$$4$$$, getting into position $$$4~7~9$$$. Now Bob has no move, since moving by $$$2$$$ is forbidden.

Example input:
1 6
0 3 9

Example output:
Alice
Bob

Example input:
1 2
0 3 9

Example output:
Alice
Bob

Editorial:
If you represent the row state as a pair $$$(x,y)$$$ where $$$x$$$ is the distance between blue and white token and $$$y$$$ is the distance between white and red, we can see that each player has one operation that reduces $$$x$$$ by $$$k$$$, and a second operation which reduces $$$y$$$ by $$$k$$$. In other words, each $$$x$$$ and $$$y$$$ defines a symmetric game, and all games are independent. This is an impartial game after all!
Once we have the Grundy numbers for each pile, we can use Sprague-Grundy theorem to find the answer. How to find the Grundy numbers? We find the set of primes and semiprime by sieving. To find the value of pile of size $$$n$$$, we can collect the Grundy numbers of each pile size reachable from $$$n$$$ (i.e. $$$n - p$$$ where p is a prime or semiprime), and then find the mex. This takes $$$\mathcal O(m^2)$$$, where $$$m$$$ is the maximum distance between two tokens ($$$2*10^5$$$) and is too slow.
To improve upon this, we can use bitset, but that's not enough. We actually need more of these! Maintain $$$k$$$ bitsets $$$G_0$$$, ..., $$$G_m$$$ where $$$i$$$-th bitset contains true at $$$j$$$-th position when there is a transition from state $$$j$$$ to state with grundy number $$$i$$$.
How do you do that? You have one bitset $$$P$$$ storing all primes and semiprimes. For each $$$i$$$, preform linear search for the mex, let the grundy number be $$$j$$$. Then or $$$G_j$$$ with $$$P « i$$$.
This works in $$$O(n^2/w)$$$. The maximum Grundy number given the input constraints is less than 100, so we can safely pick $$$k = 100$$$.