Stone Game III

Two players, Anton and Bernhard, are playing the following game.
There is one pile of $n$ stones.
The first player may remove any positive number of stones, but not the whole pile.
Thereafter, each player may remove at most twice the number of stones his opponent took on the previous move.
The player who removes the last stone wins.

E.g. $n=5$.
If the first player takes anything more than one stone the next player will be able to take all remaining stones.
If the first player takes one stone, leaving four, his opponent will take also one stone, leaving three stones.
The first player cannot take all three because he may take at most $2 \times 1=2$ stones. So let’s say he takes also one stone, leaving $2$. The second player can take the two remaining stones and wins.
So $5$ is a losing position for the first player.
For some winning positions there is more than one possible move for the first player.
E.g. when $n=17$ the first player can remove one or four stones.

Let $M(n)$ be the maximum number of stones the first player can take from a winning position at his first turn and $M(n)=0$ for any other position.

$\sum M(n)$ for $n \le 100$ is $728$.

Find $\sum M(n)$ for $n \le 10^{18}$.
Give your answer modulo $10^8$.

This problem is one from the Project Euler and it involves mathematical gaming strategy and recursive functions.

Achieving a winning strategy from a mathematical game often involves the creation of a function or a sequence based on observations which could allow for a recursive or formulaic solution.

To solve this problem, let’s define a function F(n) where F(n) equals to the smallest s such that 2^s > n. The function F(n) will provide number of steps in a binary count up to n. We need this because as we observed that the optimal strategy for the first player is to end his turn with a pile of size 2^s – 1 whenever possible. This forces the second player to leave a smaller power of 2.

Now we can start filling our F(n) array (0 to n). Initially all F(0) will be zero.

Iterate each n from 1 to n and for each n, we calculate 2^n and subtract 1 from it till it is greater than 0. If F(i) is zero where i ranges from (2^n – 1) down to (2^(n-1)). We set F(i) to n.

We define a variable winningMoveSum to calculate sum of M(n). The M(n) is the maximum number of stones the first player can take, so it will be the minimum j such that F(n – j) < F(n). Firstly we must initialize M(0) = 0 and winningMoveSum = 0. Now for every n from 1 to n, we compute M(n) = min {j : F(n - j) < F(n), j >= 1}. Once M(n) is computed we add M(n) to winningMoveSum.

The key to ensure that this algorithm runs under reasonable time is to notice that F(n) increments roughly every 2^n times and we only need to compute M(n) once for each group of these numbers.

Finally we take winningMoveSum mod 10^8 to provide the answer modulo 10^8.

This approach ensures we go through each number just once, effectively making the algorithm work in linear time.

Please remember that our solution is specific for this kind of mathematical game. Different games can require totally different insights and problem-solving strategies.

More Answers:
Bézier Curves
Comfortable Distance
A Huge Binomial Coefficient

Error 403 The request cannot be completed because you have exceeded your quota. : quotaExceeded

Share:

Recent Posts

Don't Miss Out! Sign Up Now!

Sign up now to get started for free!