Notes for competitive programming

Editorial

Online Judge

11571

Simple Equations - Extreme!!

This is a harder variation of OJ 11565.

Even with our modified solution space from that problem, the new limits for this problem - A,B,C6×1018A, B, C \le 6 \times 10^{18} - are not enough to get AC. If we follow the same strategy from before, we’d have x,y,z[2.5×109,2.5×109]x, y, z \in [-2.5 \times 10^9, 2.5 \times 10^9] which is still too huge. Given the input limits, we’d want our eventual algorithm to run in sub-linear time instead of cubic time.

To recall, here are our equations:

{x+y+z=A(1)xyz=B(2)x2+y2+z2=C(3)\begin{equation}\begin{cases}x+y+z=A&(1)\\xyz=B&(2)\\x^2+y^2+z^2=C&(3)\end{cases}\end{equation}

First off, let’s try to cut down the for loop nesting from 3 to 2. From equation 1, we have z=Axyz = A - x - y so we just need to loop through xx and yy and determine zz from that.

We can still cut down the nesting from 2 to just 1. Substitute z=Axyz = A - x - y back to equation 2, and we have: 1

xy(Axy)=BAxyx2yxy2=BAxyx2yxy2B=0xy2x2y+AxyB=0(x)y2(x2+Ax)yB=0(x)y2(x(x+A))yB=0y2+(x+A)y+Bx=0\begin{align*} xy(A-x-y) &= B\\ Axy - x^2y - xy^2 &= B\\ Axy-x^2y-xy^2-B&=0\\ -xy^2-x^2y+Axy-B&=0\\ (-x)y^2-(x^2+Ax)y-B&=0\\ (-x)y^2-(x(x+A))y-B&=0\\ y^2+(x+A)y + \frac{B}{x}&=0 \end{align*}

which is a quadratic equation in yy, and easily solvable using the quadratic formula. (We can get both solutions and keep which ever one is positive, or simply get the positive root.)

Now we’ll just have to loop through all possible values of xx. There are still far too many possible values of xx, so let’s try to slim that down.

From equation 2, xx is a factor of BB. Recall that σ0(n)\sigma_0(n) calculates the number of factors of a number nn. It would be reasonable to think that this would be a fairly reasonable number even for large values of nn, and certainly much more reasonable than the billions of values for xx we’d have to search.2 So it would make sense for our input space to be {x(xB)}\{x \mid (x \mathrel{|} B)\}, which we can derive by an O(logn)O(\log{n}) adaptation of the prime factorization algorithm.

Deriving yy and zz takes constant time, and verifying our conditions would also take constant time, so this solution runs in O(σ0(B))O(logB)O(\sigma_0(B)) \approx O(\log{B}) overall.

A few notes:

  • Don’t forget to use long long.
  • x,y,zx, y, z might be out of order, so print them in the required lexicographical order before printing them to the screen.

Footnotes

  1. We can safely divide by xx because xx is never 0. If it was, B=0B = 0, but the bounds guarantee BB is positive. Bx\frac{B}{x} is also integral because, as we’ll see later, xx is a factor of BB.

  2. Per Terence Tao: “For any large number nn, only a “logarithmically small” set of numbers less than nn will actually divide nn exactly… The average value of [σ0(n)\sigma_0(n)] is much smaller, being about logn\log{n} on the average.”

Editorial created
2024-08-06T00:00:00.000Z
Last updated
2025-01-29T00:00:00.000Z
Solution summary

Complete search through factors of BB.

Running time

O(σ0(B))O(logB)O(\sigma_0(B)) \approx O(\log{B})

Problem link

On Online Judge

Tags

complete-searchnumber-theory