Conversation
The idea is to transform drichlet convolution and use prefix sum trick to get performance gain
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The idea is to first create a$NPln(P)$ solution and then optimise it
Root the tree arbitrarily$u$ and each value $x$ we calculate the number of connect subgraphs such that u is the highest node of the subgraph and the product is equal to $x$
For each connected subgraph we calculate it using the highest node in the subgraph, (must be unique)
For each node
let$f_{u}(x)$ denote the above quantity
$$ans = \sum_{u} {\sum_{x=1}^{P} f_{u}(x)}$$
Clearly
let
$$g_{u}(x) = \sum_{i=1}^{x}f_{u}(x)$$
$$ans = \sum_{u} g_{u}(P)$$
Therefore,
lets look at following case,$u$ and another tree rooted at $v$ $u$ is parent of $v$ ,$f_u$ $u$ that also contain $v$ , denoted by$h$
$$h(x) = \sum_{i | x}f_{u}(i)*f_{v}(x/i)$$
We have a tree rooted as
We want to combine these tree such that
We want to calculate the new
Lets first find the new subgraphs formed by highest node
The above mention is a Dirichlet Convolution$f_u$ be denoted by ${f'}$
Clearly the updated
Using this we can find and update the f values in dfs fashion
Instead of performing the above Drichlet Convolution naively we use the trick - Drichlet Convolution with prefix sums
Time Complexity:
$$O(N*P^{\frac{2}{3}})$$ $N-1$ times, each time for one edge, each combine take $O(P^{\frac{2}{3}})$ operations
We perform combine
Space Complexity:
$$O(N*P^{\frac{2}{3}})$$ $O(P^{2,3})$ and the recursion depth can go upto $N$
We have to save