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
№ | Пользователь | Рейтинг |
---|---|---|
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 |
Страны | Города | Организации | Всё → |
№ | Пользователь | Вклад |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 155 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
10 | nor | 152 |
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
Название |
---|
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.