## The flipping game is a two player game played on an $N$ by $N$ square board.

Each square contains a disk with one side white and one side black.

The game starts with all disks showing their white side.

A turn consists of flipping all disks in a rectangle with the following properties:

the upper right corner of the rectangle contains a white disk

the rectangle width is a perfect square ($1$, $4$, $9$, $16$, …)

the rectangle height is a triangular numberThe triangular numbers are defined as $\frac 1 2 n(n + 1)$ for positive integer $n$. ($1$, $3$, $6$, $10$, …)

Players alternate turns. A player wins by turning the grid all black.

Let $W(N)$ be the number of winning movesThe first move of a strategy that ensures a win no matter what the opponent plays. for the first player on an $N$ by $N$ board with all disks white, assuming perfect play.

$W(1) = 1$, $W(2) = 0$, $W(5) = 8$ and $W(10^2) = 31395$.

For $N=5$, the first player’s eight winning first moves are:

Find $W(10^6)$.

### This problem is highly computational, and due to its complexity (considering the size of the game board i.e., $10^6$ by $10^6$), it cannot be solved just by using paper and pencil or even a common scientific calculator. In such case, computational software or programming is typically used.

Unfortunately, I can’t pen down here a detailed code necessary to solve this problem. However, we can sketch a general approach to solve this:

1. The problem can be approached through dynamic programming as the problem consists of overlapping subproblems.

2. We can maintain a 2D grid representation of the game board, with each cell value corresponding to the color state of the disc in that cell. You can initialize all the cells with value 1, denoting white.

3. For the moves, we need to loop through all rectangles that have their width as a perfect square and their height as a triangular number. Generate all possible moves for the player and flip the colors in the cells that get chosen.

4. Then we need to calculate who wins in each valid game state. Depending on whether it’s the first player’s turn, we need to find whether there exists any move that leads to a winning state or whether all moves lead to a losing state.

5. A state is winning if the current player has any move leading to a losing state for the next player, a state is losing if every move leads to a winning state for the next player.

6. Use a memoization array to store the winning moves for each valid game state to optimize the calculations.

7. Run the game simulation and on each move check the state of the board if it’s all black. If yes then increment the count of wining moves for the first player.

8. Once you run this simulation and logic for $10^6$ by $10^6$ game board, you should get a value for $W(10^6)$.

Still, generating and evaluating a game tree for a $10^6$ by $10^6$ game board is extremely time and memory consuming, and might not be practical for consumer level machines. This problem is more suited to algorithms competitions or high-performance computing environments, rather than regular computational needs.

##### More Answers:

Triangles Containing the Origin IIA Polynomial Modulo the Square of a Prime

Permutations of Project