File size: 4,633 Bytes
e329daf |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
<p>
A family of four moles lives in an underground burrow. Each of them has an important role to play! Daddy Mole is in charge of renovating the burrow.
Mommy Mole fixes up Daddy Mole's inevitable mistakes. And Brother Mole and Sister Mole mostly lie around playing video games.
</p>
<p>
Their burrow consists of <strong>N</strong> little underground rooms, numbered from 1 to <strong>N</strong>. Room 1 is connected to the surface,
and for each other room <em>i</em> (such that 2 ≤ <em>i</em> ≤ <strong>N</strong>), the room initially "above" room <em>i</em> is room <strong>P<sub>i</sub></strong>,
meaning that there's a tunnel leading downwards from room <strong>P<sub>i</sub></strong> to room <em>i</em>, which may be traversed in either direction.
It's guaranteed that it's possible to reach each room from room 1 by travelling through a sequence of tunnels.
</p>
<p>
Daddy Mole will renovate the burrow <strong>K</strong> times in a row. For each renovation in turn, he'll independently randomly select a room <em>i</em> aside from room 1
(such that 2 ≤ <em>i</em> ≤ <strong>N</strong>), with each such room having an equal 1 / (<strong>N</strong> - 1) probability of being chosen each time.
He'll then "improve" the burrow's architectural design by simply caving in the tunnel connecting node <em>i</em> and the room currently above it, causing that tunnel to no longer exist.
Mommy Mole will then immediately salvage the situation by creating a new tunnel leading downwards from room 1 to room <em>i</em>
(such that the room above <em>i</em> will now be room 1).
This may result in Mommy Mole recreating exactly the same tunnel that Daddy Mole had just caved in. Note that, in the resulting burrow, each room will always once again be reachable from room 1.
</p>
<p>
After these <strong>K</strong> random renovations have been completed, Brother Mole (who hangs out in room <strong>A</strong>)
will go visit Sister Mole in her room (room <strong>B</strong>) to show off his latest video game high score.
He'll travel along the unique sequence of tunnels which will get him there without passing through any rooms multiple times, at a speed of 1 tunnel per minute.
What's the expected amount of time this will take him?
</p>
<p>
Let this expected time (in minutes) be represented as a quotient of integers <strong>p/q</strong> in lowest terms.
Output the value of this quotient modulo 1,000,000,007 — in other words, output the unique integer <strong>x</strong> such that
0 ≤ <strong>x</strong> < 1,000,000,007 and <strong>p</strong> = <strong>x</strong>*<strong>q</strong> (modulo 1,000,000,007).
</p>
<h3>Input</h3>
<p>
Input begins with an integer <strong>T</strong>, the number of burrows.
For each burrow, there is first a line containing the space-separated integers <strong>N</strong>, <strong>K</strong>, <strong>A</strong>, and <strong>B</strong>.
Then, <strong>N</strong> - 1 lines follow, the <em>i</em>th of which contains the integer <strong>P<sub>i+1</sub></strong> (starting with <strong>P<sub>2</sub></strong>).
</p>
<h3>Output</h3>
<p>
For the <em>i</em>th burrow, print a line containing "Case #<em>i</em>: " followed by a single integer, the expected number of minutes required for Brother Mole
to travel from room <strong>A</strong> to room <strong>B</strong> after <strong>K</strong> renovations, expressed as a quotient of integers modulo 1,000,000,007.
</p>
<h3>Constraints</h3>
<p>
1 ≤ <strong>T</strong> ≤ 70 <br />
2 ≤ <strong>N</strong> ≤ 6,000 <br />
0 ≤ <strong>K</strong> ≤ 2,000,000 <br />
1 ≤ <strong>A</strong>, <strong>B</strong>, <strong>P<sub>i</sub></strong> ≤ <strong>N</strong> <br />
<strong>A</strong> ≠ <strong>B</strong> <br />
</p>
<h3>Explanation of Sample</h3>
<p>
In the first case, if Daddy Mole chooses room 2 for his single renovation (caving in the tunnel between rooms 2 and 3), then Mommy Mole will dig a tunnel between rooms 1 and 2,
and Brother Mole will then need 2 minutes to travel from room 2 to room 3 (passing through room 1 along the way). Otherwise, if Daddy Mole chooses node 3,
then Mommy Mole will restore the burrow to its original state, and Brother Mole will be able to reach room 3 directly in 1 minute.
These two possibilities are equally likely, resulting in an expected time of (2+1)/2 = 3/2 = 500000005 (modulo 1,000,000,007) minutes.
</p>
<p>
In the second case, no matter which room Daddy Mole chooses for each renovation, Mommy Mole will recreate the tunnel that he caves in,
resulting in Brother Mole's trip taking 2 minutes after any number of renovations.
</p>
|