Smallest Graph with K Spanning Trees

Find the smallest node count of a graph built by the move/attach process whose spanning tree count equals K, for K up to 10000.

Hard8GraphDynamic programmingNumber theoryMathNo attempts yetTime limit5sMemory limit512 MB

Problem

A spanning tree of an undirected graph is a tree that uses only edges of the graph and contains every node, so it has one edge fewer than the number of nodes. Two spanning trees are different when their edge sets are different.

You want a graph whose number of spanning trees is exactly KK, and you want to use as few nodes as possible. Only a graph built by the process below counts.

Start with a graph that has a single node. That node is both the center cc and the tip tt. Then apply the following two operations any number of times, in any order.

  • Move: make the current tip the new center. That is, set ctc \leftarrow t, and add no node and no edge.
  • Attach: choose positive integers aa and bb. If cc and tt are the same node, then a+b3a + b \ge 3 is required. Create new nodes x1,x2,,xa+b1x_1, x_2, \ldots, x_{a+b-1} and add the edges (t,x1),(x1,x2),,(xa+b2,xa+b1),(xa+b1,c)(t, x_1), (x_1, x_2), \ldots, (x_{a+b-2}, x_{a+b-1}), (x_{a+b-1}, c). Then set txat \leftarrow x_a.

Attach glues on a new path that leaves the tip and returns to the center, and the aa-th new node of that path becomes the next tip. A graph built by this process never has an edge from a node to itself, and never has two edges between the same pair of nodes.

Find the smallest number of nodes of a graph that this process can build and that has exactly KK spanning trees.

Input

The first line has the number of test cases TT. Each of the next TT lines has one integer KK.

  • 1T3001 \le T \le 300
  • 3K100003 \le K \le 10000

Output

For each test case print one line of the form Case #x: y, where xx is the test case number starting from 1 and yy is the smallest number of nodes.

Every KK in the given range has an answer, and the answer is never larger than 22.

Explanation

In the first Attach, cc and tt are the same node, so the operation creates a cycle of length a+ba + b. A cycle has as many spanning trees as it has edges, so that graph has a+ba + b spanning trees. For K=3K = 3 a single triangle is enough and the answer is 3.

For K=8K = 8, first build a triangle with a=1a = 1 and b=2b = 2, then apply Attach once more with a=1a = 1 and b=1b = 1. The result has 4 nodes and 5 edges, and it has 8 spanning trees.

After a Move, everything attached later shares exactly one node with the part built before. The number of spanning trees of the whole graph is then the product of the numbers of the two parts. Two triangles that share one node have 5 nodes and 9 spanning trees.