Intranets
Memory limit1024 MB
Given M machines with randomly assigned link priorities, find the probability that greedy highest-priority routing yields exactly K intranets, modulo 1000000007.
- Level
Medium7 of 10
- Topics
- Combinatorics, Math, Graph
- Solved
- No attempts yet
Problem
Apricot Rules LLC is developing a new simplified networking protocol and wants to show off its routing algorithm. In their design, a network consists of machines numbered from to , and each pair of machines is connected by a direct link. Each link gets a unique integer priority from to , and each machine routes traffic according to those priorities.
Unfortunately, the routing algorithm is too aggressive. It routes all traffic from a machine through the highest priority link connected to it. This may isolate some groups of machines from the others.
Formally, a machine uses a link if and only if is the highest priority link connected to . A link is active if at least one of the two machines it connects uses it. The original network splits into disjoint intranets. Two machines belong to the same intranet if and only if there is a path between them using only active links.

For example, in the left image above, only the links with priorities and are active, which creates two disjoint intranets. In the right example, three links are active, so all machines form one intranet.
As part of the quality assurance team at Apricot Rules LLC, you are investigating how serious the problem is. Find the probability that there are exactly intranets when the priorities are assigned uniformly at random among all ways of doing so.
Input
The first line of the input gives the number of test cases, . test cases follow. Each test case is described in a single line containing two integers and : the number of machines and the target number of intranets, respectively.
Output
For each test case, output one line containing Case #x: y, where is the test case number (starting from 1) and is the sought probability modulo the prime (). Represent the probability as an irreducible fraction , where and are non-negative integers that minimize . Then must equal , where is the modular multiplicative inverse of with respect to the modulus . It can be shown that under the constraints of this problem, such a number always exists and is unique.
Constraints
- .
- .
Hint
In Sample Case #1, consider the following situation. Let us call the machines , and denote the link connecting machine and machine by . Assume the priorities of links are , respectively. Then machines 1 and 2 use link , machine 3 uses link , and machines 4 and 5 use link . Thus three links are active, and there are two intranets and . Since , this situation counts toward the answer.

There are ways to assign the priorities so that there are exactly intranets, out of ways, so the probability is . In Sample Case #2, the probability is . In Sample Case #3, the probability is .