Codeforces
CF Step
Youtube Linkedin Discord Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Back to homepage

Hints: Prime Gaming (Easy Version)

Start with one fixed configuration of pile sizes.
Ignore counting over all arrays for now and only ask: after optimal play, which original index survives?

Answer to Hint 1: the game choices only remove indices, so the winner index depends on move options and turn order, not on arithmetic with values.

For a fixed array, the final value is just the value at the surviving index, so we can separate:

  1. game structure (which index survives),
  2. value aggregation (how much that contributes).

Turn the fixed configuration into a yes/no question for some threshold $a$:
is the final value $x \ge a$?

Now each pile becomes binary:

  • $0$ if value $< a$
  • $1$ if value $\ge a$

The objective becomes: Alice wants final bit $1$, Bob wants final bit $0$.

Answer to Hint 3: this is a finite impartial game with minimax on a binary payoff.
State = current sequence of binary labels (and current valid good positions after reindexing).

For a state, win = 1 means the current player can force final bit $1$ (under optimal play), win = 0 otherwise.

How do you transition?
From a state of length $l$, removing good position $i$ gives a state of length $l - 1$ with that bit deleted.

If it is Alice’s turn, she wants at least one move to win = 1.
If it is Bob’s turn, he wants all moves to fail for Alice (equivalently he looks for win = 0 in his perspective).

For implementation, encode each length-$l$ binary sequence as a bitmask and do DP by increasing $l$.
The base for $l = 1$ is immediate: the only remaining bit is the game result.

Because $n \le 20$ and total $\sum 2^n$ is bounded, bitmask DP over all sequences is feasible.

Now go back from one threshold $a$ to all arrays.
Let $K_c$ be the number of binary masks with exactly $c$ zeros (equivalently $c$ piles with value $< a$) for which Alice can force $x \ge a$.

For fixed $a$, each such mask corresponds to:

$$ (a - 1)^c \cdot (m - a + 1)^{n-c} $$

arrays.

Answer to Hint 7: total number of arrays with final $x \ge a$ is

$$ F(a) = \sum_{c=0}^{n} K_c (a - 1)^c (m - a + 1)^{n-c}. $$

Then use the identity

$$ \sum_{\text{all arrays}} x = \sum_{a=1}^{m} F(a), $$

because each configuration contributes $1$ to every threshold $a \le x$.

For E1, $m \le 2$, so only $a = 1, 2$ are needed, making the outer summation tiny.
You can still keep the same generic formula as E2; it already passes E1 comfortably.