E. Prime Gaming
This is the easy version of the problem. The difference between the versions is that in this version, . You can hack only if you solved all versions of this problem.
A valid configuration is defined as an arrangement of piles of stones such that:
The number of stones in each pile is an integer between 1 and (both inclusive). Given a valid configuration of piles of stones, some indices from 1 to are marked as good. Alice and Bob start playing a game taking turns alternately with Alice going first. In each turn, they have to perform the following operation:
Choose any integer such that (where is the number of piles left) and is good, and remove the -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 denote the number of stones in the final remaining pile. Alice wants to maximize , whereas Bob wants to minimize it. Both Alice and Bob play optimally.
Find the sum of over all the possible valid configurations modulo .
Solution
Approach
For , we can solve the problem using dynamic programming, leveraging the small number of possible pile values.
Key Observations
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.
State Representation:
- Let
dp[i][mask][player]represent the optimal value of the final pile for the current player, where:- is the number of piles left.
maskencodes the configuration: each bit represents whether a pile has 1 (0) or 2 (1) stones.playeris 0 for Alice’s turn, 1 for Bob’s turn.
- Let
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.
Computation:
- Enumerate all valid configurations and simulate the game using DP.
- Sum the optimal final pile values for all configurations.
For
For the hard version (), the problem can be reduced to the 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.