E. Tree Colorings

Problem Statement

Consider a rooted undirected tree. Each vertex can be colored blue, green, or yellow. A coloring is called beautiful if it meets these conditions:

  • The root of the tree is green
  • If you consider all blue and green vertices, they are reachable from each other without passing through any yellow vertices
  • If you consider all yellow and green vertices, they are reachable from each other without passing through any blue vertices

You are given an integer mm. Your task is to calculate the minimum number of vertices in a tree with exactly mm beautiful colorings.

Input

The first line contains a single integer tt (1t105)(1 \leq t \leq 10^5) — the number of test cases.

The only line of each test case contains a single integer mm (1m5105)(1 \leq m \leq 5 \cdot 10^5).

Output

For each test case, print a single integer — the minimum number of vertices in a tree with exactly mm beautiful colorings. If such a tree does not exist, print 1-1.

Solution (Solution from Official Editorial)

Key Observation

Important: If a vertex is not green, its entire subtree must be colored the same color. For example, if the vertex is blue and it has a green or yellow descendant, the root is unreachable from that descendant without passing through that blue vertex. At the same time, the green vertices do not impose any additional constraints.

Counting Beautiful Colorings

Using this observation, let’s explore how to solve the inverse problem to the original one (namely, given a tree, count the number of beautiful colorings) using dynamic programming.

Let cntvcnt_v be the number of ways to color the subtree rooted at vertex vv, provided that vv itself is green. Since vv is green, each of its child vertices can be colored in any color:

  • If a child uu is green, there are cntucnt_u ways to color its subtree
  • If uu is blue or yellow, there is only one way to color its subtree

Therefore, the total number of colorings for uu is cntu+2cnt_u + 2.

Since each child subtree can be colored independently:

cntv=(cntu1+2)(cntu2+2)(cntuk+2)cnt_v = (cnt_{u_1} + 2)(cnt_{u_2} + 2) \cdots (cnt_{u_k} + 2)

where u1,u2,,uku_1, u_2, \ldots, u_k are the children of vv.

Dynamic Programming Approach

Notice that the formula above shows the number of colorings for a tree depends solely on the number of colorings of its child subtrees. Therefore, we can solve the original problem using the following dynamic programming approach:

Let dpmdp_m be the minimum number of vertices in a tree with exactly mm beautiful colorings.

To calculate this, we can use the fact that the number of colorings of a tree is the product of colorings of its child subtrees. Let’s iterate over the number of colorings of the last subtree (denote it xx), then:

dpm=min(dpm/x+dpx2)dp_m = \min(dp_{m/x} + dp_{x - 2})

Tip

Think of it as having a tree root with green color that has m/xm/x colorings. Now we want to add another subtree to the root node of the tree with m/xm/x colorings. The new subtree has x2x-2 colorings with its root colored green. But this subtree can also be colored blue or yellow. Thus we have a new tree with m/x×(x2+2)=m/x×x=mm/x \times (x - 2 + 2) = m/x \times x = m colorings.

for all values of xx. Note that xx must be a divisor of mm.

Optimization

To speed up the solution, we can iterate through either xx up to m\sqrt{m} or m/xm/x up to m\sqrt{m}.

Time Complexity Analysis

This approach yields a solution with time complexity O(MM)O(M\sqrt{M}), where MM is the maximum number of colorings we need to consider. This is enough to get AC, but we can speed this up to O(MlogM)O(M \log M) as follows:

For every integer xx from 11 to MM, let’s prepare the list of its divisors. We can iterate on the divisor dd and append it to the lists of divisors for integers 2d,3d,4d,2d, 3d, 4d, \ldots

The integer dd will be processed in O(M/d)O(M/d), so in total, we achieve a complexity of:

d=1MMd=O(MlogM)\sum_{d=1}^{M} \frac{M}{d} = O(M \log M)

because it is the harmonic sum (multiplied by MM).

submission link