E. Prime Gaming

This is the easy version of the problem. The difference between the versions is that in this version, m2m \leq 2. You can hack only if you solved all versions of this problem.

A valid configuration is defined as an arrangement of nn piles of stones such that:

The number of stones in each pile is an integer between 1 and mm (both inclusive). Given a valid configuration of nn piles of stones, some indices from 1 to nn are marked as good. Alice and Bob start playing a game taking n1n−1 turns alternately with Alice going first. In each turn, they have to perform the following operation:

Choose any integer ii such that 1ip1\leq i \leq p (where pp is the number of piles left) and ii is good, and remove the ii-th pile completely. Note that after performing the operation once, the number of piles decreases by 1 and the remaining piles are re-indexed. The game will end when there is only one pile left. It is guaranteed that the index 1 is always good.

Let xx denote the number of stones in the final remaining pile. Alice wants to maximize xx, whereas Bob wants to minimize it. Both Alice and Bob play optimally.

Find the sum of xx over all the possible valid configurations modulo 109+710^9+7.

Solution

Approach

For m2m \leq 2, we can solve the problem using dynamic programming, leveraging the small number of possible pile values.

Key Observations

  1. Game Structure:

    • Each pile can have either 1 or 2 stones.
    • The game is played by removing a “good” pile in each turn, with Alice and Bob alternating moves.
    • The goal is to maximize (for Alice) or minimize (for Bob) the number of stones in the last remaining pile.
  2. State Representation:

    • Let dp[i][mask][player] represent the optimal value of the final pile for the current player, where:
      • ii is the number of piles left.
      • mask encodes the configuration: each bit represents whether a pile has 1 (0) or 2 (1) stones.
      • player is 0 for Alice’s turn, 1 for Bob’s turn.
  3. Transitions:

    • For each state, try removing each possible “good” pile and update the state accordingly.
    • Alice will choose the move that maximizes the final pile value, while Bob will minimize it.
  4. Computation:

    • Enumerate all valid configurations and simulate the game using DP.
    • Sum the optimal final pile values for all configurations.

For m>2m > 2

For the hard version (m>2m > 2), the problem can be reduced to the m=2m = 2 case by considering the positions with values above or below a certain threshold. The details of this reduction are more involved and can be found by comparing the two versions of the code.

References