#P17472. [ICPC 2018 Jiaozuo R] Shortest Paths on Random Forests
[ICPC 2018 Jiaozuo R] Shortest Paths on Random Forests
Problem Description
Here is a problem related to forest, which is a special type of graph. Before introducing this problem to you, we intend to show some definitions used in this problem. A labelled forest with vertices is an acyclic undirected simple graph in which vertices are labelled by . Two labelled forests are regarded as different if their numbers of vertices are different or, if they have the same number of vertices, for some integers and for vertices labelled by in these two forests, their neighbours have different labels (which means that the sets of labels corresponding to all neighbours of vertices labelled by in these two forests are different).
Tree-like structures are constructed in computer programming constantly, which is the most fascinating part Bob has ever seen. Today, Bob wants to randomly choose a labelled forest from all possible labelled forests having vertices with equal probability. Then, he will set to the number of edges on the shortest path from the vertex labelled to the vertex labelled if the shortest path exists, or set to otherwise. Bob is curious about the expected value of
$$\displaystyle \sum_{i = 1}^{n}{\sum_{j = i + 1}^{n}{\delta^2(i, j)}},$$but it's hard for him. Can you help Bob find out the expected value modulo ?
More precisely, if the reduced fraction of the expected value is , what you should provide is the minimum non-negative integer such that .
Input Format
The input contains several test cases, and the first line contains a positive integer indicating the number of test cases which is up to .
For each test case, the only one line contains two integers and where and .
We guarantee that the modular multiplicative inverse of in each test case always exists, in other words, the condition is guaranteed to be true in all test cases.
Output Format
For each test case, output a line containing the answer modulo .
4
1 1
2 3
3 7
4 16
0
5
66
576