Need Editorail for the problem INSQ17X from INSOMNIA. There are lot of problems innvolvinf Tree and GCD. Any insights would help https://www.codechef.com/INQU2017/problems/INSQ17X
# | User | Rating |
---|---|---|
1 | tourist | 3985 |
2 | jiangly | 3814 |
3 | jqdai0815 | 3682 |
4 | Benq | 3529 |
5 | orzdevinwang | 3526 |
6 | ksun48 | 3517 |
7 | Radewoosh | 3410 |
8 | hos.lyric | 3399 |
9 | ecnerwala | 3392 |
9 | Um_nik | 3392 |
# | User | Contrib. |
---|---|---|
1 | cry | 169 |
2 | maomao90 | 162 |
2 | Um_nik | 162 |
4 | atcoder_official | 161 |
5 | djm03178 | 158 |
6 | -is-this-fft- | 157 |
7 | adamant | 155 |
8 | awoo | 154 |
8 | Dominater069 | 154 |
10 | luogu_official | 150 |
Need Editorail for the problem INSQ17X from INSOMNIA. There are lot of problems innvolvinf Tree and GCD. Any insights would help https://www.codechef.com/INQU2017/problems/INSQ17X
Name |
---|
I did not solve it but I had got an idea but with a problem. :(
You can do a dfs traversal in the tree while maintaining a deque. You add the value of the node in the deque as you traverse it. Maintain a map containing the frequency of all the prime factors of the values in the deque. Suppose the value of node u has a prime factor that has already been used by another value in the deque. Start popping the values from the front until there is no more contradiction. Since all the elements will be in the deque twice(You need not add a value back once its popped). Suppose the length of the deque is N after popping the elements, then there N nodes where the path could have started to end at u, add N to your answer and continue the dfs. When the dfs on u is finished, if the last element is the value of u(Not a duplicate value, see if the value is the value that was pushed in by u) then pop u from the back.
The problem is that, after u finishes a dfs in one of its child's subtree, to go to the next subtree the whole deque needs to be made again. I hope someone can help me figure out what to do in this case. :( :( :(
For anyone who helps me out: Thank you in advance. :)
First let's define an array maxUp[i].
Suppose you are at node x whose level is L and value is v[x]. Now you want to know the node y(an ancestor of x) present at least depth such that all nodes from y to parent[x] are coprime to v[x].
So maxUp[x] = level[y]. But one important thing to notice is that, maxUp of a node has to be >= maxUp of its parent(always). Because we want a chain where all values are coprime.
So answer contributed by node x will be (L-maxUp[x]+1).
Sum these values for all nodes.
Now to implement it run a dfs and maintain an array of vectors val. Here val[i] = vector of levels of nodes whose values were divisible by i. And now compute maxUp[x] with the help of val and factors of v[x].
You can see my implementation here
Happy coding :)
Thanks for such a neat explanation :)
Great explanation.