Editorial for π𝓮s


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: Sucram314

Happy π day!!!!!

Initial Observations

Hint 1 Try to determine an expression for the expected change to the current player's points on a given turn number n.
Hint 1 Answer Notice that the total number of pies on turn n (starting at n=0) is X + Y + Zn. Additionally, since the slice of \pi e which is eaten on each turn is replaced, and the Z additional slices of \pi e have an equal probability of being either good or bad, the expected difference between the number of good slices of \pi e and bad slices of \pi e remains constant, equal to X - Y. Now, the expected point change for the current player on turn n is the probability they pick a good slice of \pi e, minus the probability they pick a bad slice of \pi e. \displaystyle \frac{\text{expected # of good slices of }\pi e\text{ on turn }n}{X + Y + Zn} - \frac{\text{expected # of bad slices of }\pi e\text{ on turn }n}{X + Y + Zn} But since the expected difference between the number of good slices of \pi e and bad slices of \pi e is always X - Y, this can be written compactly as: \displaystyle \frac{X - Y}{X + Y + Zn}
Hint 2 Now that we have an expression for the expected change to the current player's points on a given turn number n, determine an expression for the expected value of Alice's points minus Bob's points after N turns, where N is a given whole number.
Hint 2 Answer Notice that Alice always plays on even numbered turns and Bob always plays on odd numbered turns (turn numbers start from n=0). Thus, the expected change to Alice's points minus Bob's points on turn n is \displaystyle \begin{cases}\phantom{-}\dfrac{X - Y}{X + Y + Zn} & n \equiv 0 \pmod{2} \\ \\ -\dfrac{X - Y}{X + Y + Zn} & n \equiv 1 \pmod{2} \end{cases} Using powers of -1, this can be written more compactly as \displaystyle (-1)^n\frac{X - Y}{X + Y + Zn} To find the expected value of Alice's points minus Bob's points after N turns, we can sum the expected changes to this value for every turn n = 0, 1, \dots N-1. \displaystyle \sum_{n=0}^{N-1}(-1)^n\frac{X - Y}{X + Y + Zn} Now, the question asks us to determine the limit of Alice's points minus Bob's points as the number of turns N approaches infinity, so after factoring out X-Y and plugging in N = \infty, the value which we wish to calculate is \displaystyle (X - Y)\sum_{n=0}^\infty\frac{(-1)^n}{X + Y + Zn}

Subtask 1

Hint In this subtask, 1 \le Z \le 2. Thus, try writing out the terms to the desired sum for Z = 1 and Z = 2. Does this look similar to any known infinite series?
Answer Let's first try Z = 1. \displaystyle (X - Y)\sum_{n=0}^\infty\frac{(-1)^n}{X + Y + n} After reindexing the sum, we get \displaystyle (X - Y)(-1)^{X+Y}\sum_{n=X+Y}^\infty\frac{(-1)^n}{n} We can break this sum into a finite piece and a well known infinite sum as follows: \displaystyle (X - Y)(-1)^{X+Y}\left[\left(\sum_{n=1}^\infty\frac{(-1)^n}{n}\right)-\left(\sum_{n=1}^{X + Y - 1}\frac{(-1)^n}{n}\right)\right] It is known that \displaystyle \sum_{n=1}^\infty\frac{(-1)^n}{n} = -1 + \frac{1}{2} - \frac{1}{3} + \frac{1}{4} - \frac{1}{5} + \dots = -\ln 2 This is called the alternating harmonic series. (well, to be precise, the negative of it). Plugging this back into our expression we get \displaystyle (X - Y)(-1)^{X+Y}\left[-\ln(2)-\sum_{n=1}^{X + Y - 1}\frac{(-1)^n}{n}\right] Since 0 \le X,Y \le 314 in this subtask, X + Y - 1 is small enough that we can simply calculate this sum with a loop.

A similar method can be applied to Z = 2, with casework on whether X+Y is even or odd. It is left as an exercise to the reader to find exact formulae for Z = 2.

Hint: When X+Y is odd, it involves \pi 🤯🤯🤯‼️

Time Complexity: \mathcal{O}(X + Y) per testcase

More Math for Subtask 2 and 3

Hint We were thinking too specifically for subtask 1. Now that Z can take on many possible values, we need a more general approach to calculate alternating sums of the reciprocals of arithmetic sequences. Can you find a way to relate the desired sum to a well-known sequence of numbers? (terrible hint i know)
Answer Begin with our desired sum. \displaystyle (X - Y)\sum_{n=0}^\infty\frac{(-1)^n}{X + Y + Zn} Factoring out \frac{1}{Z}, we get \displaystyle \frac{X - Y}{Z}\sum_{n=0}^\infty\frac{(-1)^n}{\frac{X + Y}{Z} + n} We can write every two consecutive terms explicitly like so \displaystyle \frac{X - Y}{Z}\sum_{n=0}^\infty\left(\frac{1}{\frac{X + Y}{Z} + 2n}-\frac{1}{\frac{X + Y}{Z} + 2n+1}\right) Factoring out a \frac{1}{2}, we get \displaystyle \frac{X - Y}{2Z}\sum_{n=0}^\infty\left(\frac{1}{\frac{X + Y}{2Z} + n}-\frac{1}{\frac{X + Y}{2Z} + n + \frac{1}{2}}\right) Reindexing the sum to begin at n=1 and collecting the constants into the fractions in the denomators, we get \displaystyle \frac{X - Y}{2Z}\sum_{n=1}^\infty\left(\frac{1}{\frac{X + Y - 2Z}{2Z} + n}-\frac{1}{\frac{X + Y - Z}{2Z} + n}\right) Now we introduce some magical terms that cancel each other out but make the sum more similar to a well-known sequence \displaystyle \frac{X - Y}{2Z}\sum_{n=1}^\infty\left(\frac{1}{\frac{X + Y - 2Z}{2Z} + n}-\frac{1}{\frac{X + Y - Z}{2Z} + n}+\frac{1}{n}-\frac{1}{n}\right) Grouping the terms as follows, we get \displaystyle \frac{X - Y}{2Z}\sum_{n=1}^\infty\left(\left(\frac{1}{n}-\frac{1}{\frac{X + Y - Z}{2Z} + n}\right)-\left(\frac{1}{n}-\frac{1}{\frac{X + Y - 2Z}{2Z} + n}\right)\right) We can split this into two sums which still converge (and I am not going to prove why), to get \displaystyle \frac{X - Y}{2Z}\left[\sum_{n=1}^\infty\left(\frac{1}{n}-\frac{1}{\frac{X + Y - Z}{2Z} + n}\right)-\sum_{n=1}^\infty\left(\frac{1}{n}-\frac{1}{\frac{X + Y - 2Z}{2Z} + n}\right)\right] Now, each sum takes on the form of a harmonic number (analytically continued to the real numbers)! \displaystyle H(x) = \sum_{n=1}^\infty\left(\frac{1}{n} - \frac{1}{x+n}\right) A good video on this topic is Extending the Harmonic Numbers to the Reals. Substituting this into our expression, we get \displaystyle \frac{X - Y}{2Z}\left[H\left(\frac{X + Y - Z}{2Z}\right)-H\left(\frac{X + Y - 2Z}{2Z}\right)\right] We can also write this using the digamma function \displaystyle \psi(x) = H(n-1) - \gamma where \gamma is the Euler-Mascheroni constant. Using the digamma function instead of the harmonic numbers, our final answer is \displaystyle \frac{X - Y}{2Z}\left[\psi\left(\frac{X + Y + Z}{2Z}\right)-\psi\left(\frac{X + Y}{2Z}\right)\right] Now, the only issue is calculating the digamma function...

Subtask 2

Spoiler This subtask was intended to reward suboptimal implementations of the full solution, or solutions which attempted to calculate the exact value of the digamma function using Gauss's Digamma Theorem.

Let \dfrac{p}{q} be a positive rational number with p < q. Then, \displaystyle \psi\left(\frac{p}{q}\right) = -\gamma - \ln(2q) - \frac{\pi}{2}\cot\left(\frac{p}{q}\pi\right) + 2\sum_{n=1}^{\left\lceil\frac{q}{2}\right\rceil-1}\cos\left(\frac{2\pi pn}{q}\right)\ln\left(\sin\left(\frac{\pi n}{q}\right)\right) For positive rational numbers \dfrac{p}{q} where p \ge q, we can use the fact that \displaystyle \psi(x+1) = \psi(x) + \frac{1}{x} to bring the rational number between 0 and 1, where we can then use Gauss's Digamma Theorem, or the fact that \psi(1) = -\gamma.

Since 0 \le X,Y \le 314 and 1 \le Z \le 271 in this subtask, using a while loop and the digamma recurrence to bring the fractions between 0 and 1 and then a for loop for the sum in Gauss's Digamma Theorem is fast enough.

Time Complexity: \mathcal{O}\left(\dfrac{X + Y}{Z} + Z\right) per testcase

Subtask 3

Spoiler The intended solution for this problem uses an approximation of the digamma function, which can be derived using the Euler-Maclaurin formula, or with a quick Wikipedia search :-) \displaystyle \psi(x) \sim \ln x - \frac{1}{2x} - \frac{1}{12x^2} + \frac{1}{120x^4} - \frac{1}{252x^6} + \frac{1}{240x^8} - \frac{1}{132x^{10}} + \frac{691}{32760x^{12}} - \frac{1}{12x^{14}} + \dots This approximation gets better for larger x, so we can use the fact that \displaystyle \psi(x+1) = \psi(x) + \frac{1}{x} to increase the value of x to a sufficiently large number, and then apply the approximation.

This approximation is extremely good, so you should only need to apply this recurrence around 5 times and keep around 4 terms of the series to get a value within 10^{-6} of the exact answer.

Time Complexity: \mathcal{O}(1) per testcase

Comments

There are no comments at the moment.