As the title is pretty clear, I want to solve this problem DISTX but I am unable to design any algorithm within time limit. The best I can think of is an n2 approach.
Can anyone please help me solve this problem?
# | User | Rating |
---|---|---|
1 | tourist | 3993 |
2 | jiangly | 3743 |
3 | orzdevinwang | 3707 |
4 | Radewoosh | 3627 |
5 | jqdai0815 | 3620 |
6 | Benq | 3564 |
7 | Kevin114514 | 3443 |
8 | ksun48 | 3434 |
9 | Rewinding | 3397 |
10 | Um_nik | 3396 |
# | User | Contrib. |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 156 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
10 | nor | 152 |
As the title is pretty clear, I want to solve this problem DISTX but I am unable to design any algorithm within time limit. The best I can think of is an n2 approach.
Can anyone please help me solve this problem?
Name |
---|
A parallel BFS from each point should run in O( MAX * (sqrt(MAX) / log(sqrt(MAX)) ) if you only transition through the primes lower than sqrt(MAX) and handle the (possibly) single prime bigger than that separately.
That said, my solution with runs locally on maximal random inputs in 0.7s keeps getting TLE, so maybe this isn't the right approach.
EDIT: Accepted, works but you reaaaally need to squeeze it.
Solution with O(N + MAX * log(MAX)) complexity exists. On Java it gets TLE, actually, but on CPP 14 it has "Time 1.20" on SPOJ (your AC solution has 2.74).
Before all we need calculate distance from any number to 1. It can be done with Sieve of Eratosthenes and storing smallest/largest prime for each number for fast factorization.
There are two main cases in this problem:
First case is handled very simply: we store for each value first and second index of element with that value. If element is first with that value — answer is second index, else — first index.
Second case we can process next way: each path from x to y (x != y) can be represented as several divide operations and several multiplications. Let's call lca(x, y) number, before that we divide and after — multiply. It's obvious, that lca(x, y) is divisor of x and y. And d(x, lca, y) equals to distance(x) — distance(lca) + distance(y) — distance(lca) = distance(x) + distance(y) — 2 * distance(lca).
We can see that if we fix lca, we can find optimal y for each x — it will be such y, that distance(y) is minimal, y is multiple of lca and y is presented in the input (tiebreaker is index of value in the input array). Actually, we need second minimum, too (when first minimum is start of path). So we can iterate over all numbers as lca in some answer paths and try to update minimal distances.
It's known that complexity of 'for each number from 1 to N iterate over all multiples of that number)' is O(NlogN), so total complexity is O(N + MAX * log(log(MAX)) + MAX * log(MAX)) = O(N + MAX * log(MAX)).
That's a neat idea, I tried to make the LCA approach work with a BFS-like implementation but failed, didn't even imagine this sieve-like technique, thanks!
Okay, that is a really helpful comment, and I tried implementing it, and I am facing TLE. That is because I am storing all the prime factors of each number in a vector in the sieve function. Obviously, we need all the prime factors of a number to get all the ancestors of that number, so that we can iterate over them.
How have you tackled this situation?
I am storing only one (any) prime divisor of each number (sieve is needed exactly for it). After that I can calculate distances for all numbers next way:
If you are storing only one factor, that is the smallest one, then how do you calculate all the ancestors of any number, for e.g. if we have a number 12, then we need to go to all the ancestors of 12, right? That is, 12, 6, 4, 3, 2, 1. But if we store only one prime divisor, then the ancestors that we can get to are, 12, 6, 3, 1.
How, by storing only one prime divisor, you are getting all the ancestors?
First just get the distance from each number to 1, like Slamur's code above is doing.
Then, if your lca is L, the distance from a multiple M to L is just distances[M] — distances[L], and this is all you need for the algorithm: fix L and iterate through all M = k*L. No need to generate every divisor, or even every prime divisor!
My point of view is not from element to it's divisors, but from divisor to all it's multiples.
I iterates over divisors from 1 to MAX, for each divisor I update answers for elements of array that are multiples of this divisor, it can be done with simple 'for'-array:
Smallest prime for each number I store only for calculating distances from number to 1 (code in my previous commentary).
Thanks a lot guys, Slamur and itu, I finally solved the problem in O(N+max*log(max)).
It was definitely a challenging one for me!