## GUPTA MECHANICAL

IN THIS WEBSITE I CAN TELL ALL ABOUT TECH. TIPS AND TRICKS APP REVIEWS AND UNBOXINGS ALSO TECH. NEWS .............

## Intranets Solution Round 1C 2022 - Code Jam 2022

### Problem

Apricot Rules LLC is developing a new simplified networking protocol and wants to show off their routing algorithm. In their design, a network consists of $M$ machines numbered from $1$ to $N$, and each pair of machines is connected by a direct link. Each of the links is given a unique integer priority value between $1$ and $\left(\mathbf{M}×\left(\mathbf{M}-1\right)/2\right)$ and each machine routes traffic according to those priorities.

Unfortunately, the routing algorithm is too aggressive and will route all traffic from a machine through the highest priority link connected to it. This may make some groups of machines isolated from others.

Formally, we say that a machine $m$ uses a link $\ell$ if (and only if) $\ell$ is the highest priority link connected to $m$. We also say that a link is active if it is used by at least one of the two machines it connects. Given the link priorities, the original network becomes partitioned into disjoint intranets. Two machines belong to the same intranet if and only if there is some path between them using only active links.  For example, as seen in the left image above, only the links with priorities $6$ and $5$ are active. This creates two disjoint intranets. However, in the example on the right, three links are active, which results in one intranet consisting of all $4$ machines.

Solution Click Below:-

As part of the quality assurance team at Apricot Rules LLC, you are investigating the extent of the problem. You are interested in knowing the probability of there being exactly $K$ intranets if the priorities are assigned uniformly at random from among the $\left(\mathbf{M}×\left(\mathbf{M}-1\right)/2\right)!$ ways of doing so.

### Input

The first line of the input gives the number of test cases, $T$$T$ test cases follow. Each test case is described in a single line containing two integers $M$ and $K$: the number of machines and the target number of intranets, respectively.

Letter Blocks Solution Round 1C 2022 - Code Jam 2022

Squary Solution Round 1C 2022 - Code Jam 2022

### Output

For each test case, output one line containing Case #x$x$: y$y$, where $x$ is the test case number (starting from 1) and $y$ is the sought probability computed modulo the prime ${10}^{9}+7$ ($1000000007$), which is defined precisely as follows. Represent the probability as as an irreducible fraction $p/q$ (with $p$ and $q$ being non-negative integers that minimize $p+q$). Then, $y$ must equal $p\cdot {q}^{-1}mod{10}^{9}+7$⁠, where ${q}^{-1}$ is the modular multiplicative inverse of $q$ with respect to the modulus ${10}^{9}+7$. It can be shown that under the constraints of this problem, such a number $y$ always exists and is unique.

### Limits

Memory limit: 1 GB.
$1\le \mathbf{T}\le 50$.
$1\le \mathbf{K}\le \mathbf{M}/2$.

#### Test Set 1 (Visible Verdict)

Time limit: 20 seconds.
$2\le \mathbf{M}\le 50$.

#### Test Set 2 (Hidden Verdict)

Time limit: 60 seconds.
$2\le \mathbf{M}\le 5×{10}^{5}$.

### Sample

Sample Input
3
5 2
5 1
6 3

Sample Output
Case #1: 428571432
Case #2: 571428576
Case #3: 47619048


In Sample Case #1, consider the following situation. Let's call $\mathbf{M}=5$ machines $1,2,3,4,5$ and denote the link connecting machine $a$ and machine $b$ by $\left(a,b\right)$. Assume that the priorities of links $\left(1,2\right),\left(1,3\right),\left(1,4\right),\left(1,5\right),\left(2,3\right),\left(2,4\right),\left(2,5\right),\left(3,4\right),\left(3,5\right),\left(4,5\right)$ are $9,8,7,6,5,4,3,2,1,10$, respectively. Then machines $1$ and $2$ use link $\left(1,2\right)$, machine $3$ uses link $\left(1,3\right)$, and machines $4$ and $5$ use link $\left(4,5\right)$. Thus three links $\left(1,2\right),\left(1,3\right),\left(4,5\right)$ are active, and there are two intranets $\left\{1,2,3\right\}$ and $\left\{4,5\right\}$. Since $\mathbf{K}=2$, this situation counts the answer. We can find that there are $1555200$ ways to assign the priorities to have exactly $2$ intranets among $10!=3628800$ ways, so the probability is $3/7$.

In Sample Case #2, the probability is $4/7$.

In Sample Case #3, the probability is $1/21$.