Notes for competitive programming
•
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 - - are not enough to get AC. If we follow the same strategy from before, we’d have 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:
First off, let’s try to cut down the for loop nesting from 3 to 2. From equation 1, we have so we just need to loop through and and determine from that.
We can still cut down the nesting from 2 to just 1. Substitute back to equation 2, and we have: 1
which is a quadratic equation in , 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 . There are still far too many possible values of , so let’s try to slim that down.
From equation 2, is a factor of . Recall that calculates the number of factors of a number . It would be reasonable to think that this would be a fairly reasonable number even for large values of , and certainly much more reasonable than the billions of values for we’d have to search.2 So it would make sense for our input space to be , which we can derive by an adaptation of the prime factorization algorithm.
Deriving and takes constant time, and verifying our conditions would also take constant time, so this solution runs in overall.
A few notes:
- Don’t forget to use
long long. - might be out of order, so print them in the required lexicographical order before printing them to the screen.
Footnotes
-
We can safely divide by because is never 0. If it was, , but the bounds guarantee is positive. is also integral because, as we’ll see later, is a factor of . ↩
-
Per Terence Tao: “For any large number , only a “logarithmically small” set of numbers less than will actually divide exactly… The average value of [] is much smaller, being about on the average.” ↩
- Editorial created
- Last updated
- Solution summary
Complete search through factors of .
- Running time
Problem link
On Online JudgeTags
complete-searchnumber-theory