Legends Never Die#13136

Legends Never Die (Editorial)

First we must form the area from to . This can be achieved by the difference of A(0,r) and A(0,l). We can see it as a right angled triangle with right sides and a \cdot r, and the area is \frac{1}{2}\left ( ar^2 \right ), and for is \frac{1}{2} \left ( al^2 \right ). Thus the area is

\frac{a}{2}\left (r^2-l^2 \right )

Notice the right brackets part can be factorized (Difference of Squares) !

r^2-l^2=(r-l)(r+l)

Now we've this equation over

b=\frac{a}{2}\left (r-l \right )\left (r+l \right )

Multiply both sides by \frac{2}{a}:

\left (\frac{2b}{a} \right )=(r-l)(r+l)

With a simple Number Theory principle that for any ab \le n, then a,b \le \lfloor \sqrt{n} \rfloor, thus here the same applies

(r-l),(r+l) \le \left \lfloor \sqrt{\frac{2b}{a}} \right \rfloor

. So we iterate as from 1 to \left \lfloor \sqrt{\frac{2b}{a}} \right \rfloor. Notice that (l,r) must be integers. Formally, the solution to the system

\begin{cases} r-l = i \\ r+l = \left ( \frac{2b}{a \cdot i} \right ) \\ \end{cases}

By summing the equations, we obtain r=\left ( \frac{i}{2}+\frac{b}{a\cdot i} \right ) and l=\left ( r-i \right ). We've a necessary condition to obtain an integer pair that's

\left ( i \text{ mod } 2 \right )= \left ( \frac{2b}{a \cdot i} \text{ mod } 2 \right )

Finally, append all valid pairs to a vector , sort them. If there is no valid pair, print .

Complexity is \mathcal{O}\left(\sqrt{\frac{b}{a}} \log \sqrt{\frac{b}{a}} \right) .