Editorial for Bubble Cup V9 H Dexterina's Lab
Submitting an official solution before solving the problem yourself is a bannable offence.
Dynamic programming — 
A well-known result from game theory states that the winning player of a game of Nim is determined only by the bitwise XOR of the piles' sizes. The first player has a winning strategy if (and only if) this value is not zero.
Let  be the probability that a pile contains 
 objects. Define 
 as the probability that the
bitwise XOR of 
 random sizes is equal to 
. The values of 
 can be expressed with the
recurrence relation
where  is the maximal possible value of the XOR (one less than the first power of 
 greater than
the maximal pile size 
) and 
 the bitwise XOR operator.
The probability that the second player wins is . Because the first player wins if the second
does not, we can calculate the values of 
 in 
 and output 
.
Matrix exponentiation — 
Let  be the vector 
. The transformation that produces 
 from 
 is
linear, and can therefore be expressed as 
, where 
 is a matrix given by 
.
Since the matrix multiplication is associative,  we can calculate 
 using 
matrix multiplications, for a total complexity to calculate 
 (which contains the solution 
) of
.
This problem can also be solved in:
- Vector exponentiation — 
 - Faster multiplication — 
 - Even faster multiplication — 
 
But  is enough to get AC.
Comments