Contest Duration: - (local time) (100 minutes) Back to Home

## G - Roll or Increment Editorial by en_translator

Obviously, for any situation, whether we should “increase the number shown by the die” or “recast the die” only depends on what number the die currently shows. Therefore, for each $$i = 1, 2, \ldots, N$$ (except for $$i = T$$), we will try determining “which choice we should choose when the die is showing $$i$$.”

Note the following tow facts.

• If the number shown by the die is greater than $$T$$, then clearly it is optimal to “recasting” it.
• “Recasting” right after “increasing the number” is evidently worse than just “recasting.” Therefore, in the optimal strategy, if one chooses to “increase the number” when the die is showing a number $$P$$, then one should choose to “increase the number” when the die is showing any number $$P'$$ such that $$P \leq P'$$ (except for the case when it’s showing $$T$$).

By the two points above, in order to find the optimal strategy, we should find which of the strategies that satisfies the following statement is optimal.

There exists an integer $$X$$ satisfying $$1 \leq X \leq T$$ such that if the die is showing any inter between $$T-X$$ and $$T$$ (exclusive), the choice of “increasing the number” is taken; otherwise (except for $$T$$), the choice of “recasting” is taken.

Therefore, this problem asks to choose an integer $$X$$ such that $$1 \leq X \leq T$$, so that it minimizes “the expected value of changing the die shown by the die from $$S$$ to $$T$$.”

We will now find the minimum value of “the expected value of changing the die shown by the die from $$S$$ to $$T$$” for the following two cases:

• when choosing $$X$$ under the condition where “it holds that $$T-X+1\leq S \leq T$$”, and
• when choosing $$X$$ under the condition where “it doesn’t hold that $$T-X+1\leq S \leq T$$”.

(If $$S > T$$, only the latter case is considered.)
The smaller of them is the answer for the problem.

#### (1) When choosing $$X$$ under the condition where “it holds that $$T-X+1\leq S \leq T$$”

Since we choose to “increase the number” until the number shown by the die changes from $$S$$ to $$T$$, so the cost required is $$A(T-S)$$, independent of $$X$$.

#### (2) when choosing $$X$$ under the condition where “it doesn’t hold that $$T-X+1\leq S \leq T$$”

The process of transition of the number shown on the die from $$S$$ to $$T$$ consists of the following two steps.

1. First, repeat “recasting the die” until the die shows a number between $$T-X+1$$ and $$T$$ (inclusive).
2. Then, repeat “increasing the number shown by the die” until the number becomes $$T$$.

The expected value of cost required in each step is as follows.

1. “The probability of the die’s showing a number between $$T-X+1$$ and $$T$$ (inclusive) after recasting it once” is $$X/N$$, so the expected number of times of “recasting” the die in the first step is $$N/X$$. Therefore, the expected value of cost for the first step is $$BN/X$$.
2. Any integer between $$T-X+1$$ and $$T$$ (inclusive) is possible to be shown by the die after the first step ends. Also, each of them appears with the same probability. Thus, the expected cost of the second step is $$\lbrace\sum_{i = T-X+1}^{T} A(T-i)\rbrace/X = A(X-1)/2$$.

Therefore, the expected value of the cost required to change the number shown by the die from $$S$$ to $$T$$ is $$A(X-1)/2 + BN/X$$.
We will now explain how to find the minimum value of $$T-X+1\leq S \leq T$$ when an integer $$X$$ is chosen under the condition “it doesn’t hold that $$T-X+1\leq S \leq T$$.”

Consider function $$f(x) = A(x-1)/2 + BN/x$$ defined for positive real numbers $$x$$. By the inequality of arithmetic and geometric means, function $$f(x)$$ is minimum at $$x = \sqrt{2BN/A}$$. Also, since $$f''(x) = 2BN/x^3$$, $$f(x)$$ is a convex function. Therefore, it is sufficient to compute $$f(X)$$ for several number of $$X$$ around $$\sqrt{2BN/A}$$ such that “it doesn’t hold that $$T-X+1\leq S \leq T$$” and $$1 \leq X \leq T$$. (Here, actually we can prove that we can ignore the constraints of $$T-X+1\leq S \leq T$$.)

Therefore, the problem can be solved in a total of $$O(1)$$ time.

posted:
last update: