Editorial for DMOPC '17 Contest 2 P5 - Game


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Author: r3mark

Let Z=max(X,Y).

For the first subtask, keep a 2-D boolean array storing if the first player can win given a certain state. The states are number of stones remaining and number of stones player is allowed to take on this turn. This array can be computed using DP.

Time Complexity: O(Z3)

For the second subtask, say that there are N stones currently. The first player can win if they are allowed to take A stones on this turn. Then clearly, the player can win if they are allowed to take B stones on this turn and BA. So store the least amount of stones the first player must take to guarantee a win if there are currently N stones and call this dp[N]. A DP recurrence can be found involving this array: if M is the largest number (less than N) such that dp[M]>NM+K, then dp[N]=NM. (Also, dp[0]=.)

Time Complexity: O(Z2)

For the third subtask, finding each dp[N] in the second subtask can be sped up. Previously, it took O(N) to compute dp[N]. Using data structures such as an RMQ segment tree, this may be sped up to O(logN).

Time Complexity: O(ZlogZ)

For the fourth subtask, first note the following fact: For any M with dp[M]M, let N be the largest number less than M such that dp[N]=N. Then dp[M]=dp[MN]. This claim can be proved by strong induction on MN.

Consider an arbitrary N1 with dp[N1]=N1 and N1K+2 (it is not hard to see that dp[N]=N for all 1NK+2). Let N3 be the smallest number larger than or equal to N1K with dp[N3]=N3 and N2 be defined as N1+N3. Then N2 is the least number larger than N1 such that dp[N2]=N2.

Proof: It can be seen from the earlier claim and from the fact that dp[N2N1]=N2N1, that for any N with N1<N<N2, that dp[N]N and also dp[N]=dp[NN1]N3(NN1)+K=N2N+K. Now for any N with 0<NN1, dp[N]N=N(N1+N1K)+KN(N1+N3)+K=NN2+K. So the largest number M less than N2 such that dp[M]>N2M+K is 0. So dp[N2]=N2.

So using this, all N such that dp[N]=N can be computed. There are approximately log2Z such Ns between K+2 and Z. For each N with dp[N]=N, compute the number of Ms such that dp[M]=1 using the first claim. Then, using these values, the final answer can be computed using the first claim.

Time Complexity: O(logZ)


Comments

There are no comments at the moment.