The Missing Pet
Time limit4sMemory limit512 MB
For each hole on an n by n board, find the expected steps of a random walk from a start cell, given it falls into that hole, modulo 10^9+7.
- Level
Hard8 of 10
- Topics
- Probability, Matrix, Math
- Solved
- No attempts yet
Problem
You are given an chessboard, with rows and columns numbered from 1 to . Little Q punched several holes in specified locations: the -th hole is at .
Little Q also has a pet dog. The dog gets lost on the board at cell . Every second, it moves to a random adjacent cell, and each adjacent cell is chosen with equal probability. Two cells are adjacent if they share an edge. If the dog arrives at a cell with a punched hole, it falls into the hole.
For each hole, what is the expected number of seconds the dog walks on the board, given that it finally falls into this hole? Please help him.
Input
The first line contains an integer (), the number of test cases. For each test case:
The first line contains two integers and (, ), the size of the chessboard and the number of holes.
Then lines follow. The -th of them contains two integers and (), the location of the -th hole.
The last line of each test case contains two integers and (), the starting location of the pet.
All holes are distinct, and the pet does not start on a hole. It is guaranteed that holds in at most one test case.
Output
For each test case, output a single line with integers: one per hole, in input order, the expected number of seconds the pet walks before falling into that hole, given that it falls into it. If a hole cannot be reached, print -1 instead.
If a hole is reachable and the reduced fraction of the expected value is , print the minimum non-negative integer such that . You may assume such always exists.