Younghee's Errand
Time limit2sMemory limit1024 MB
Given an N by N grid of candies (o) and chocolates (x), find the average expected candy and chocolate counts over all target cells after each label flip.
- Level
Hard9 of 10
- Topics
- Combinatorics, Dynamic programming, Matrix
- Solved
- No attempts yet
Problem
Younghee, the class president at Snoops High School, has an errand from her teacher. She must find Chulsoo somewhere on campus and tell him to go to the faculty office.
She has no time for it, though, because she has to solve the math workbook problems due in the next period. So she lends Kere a secret map that marks where she hid her candies and chocolates, and asks whether Kere can run the errand instead.
The map has these properties:
- The map is an grid, and each cell is one area of the school.
- From each cell, you can move up, down, left, or right. The distance between adjacent cells is the same in every direction.
- A cell with candy is marked
o, and a cell with chocolate is markedx. A cell with nothing has no mark.
Kere runs the errand in these steps. "Choose uniformly at random" means picking one of the available options, with each option having the same probability.
- Find the set of shortest paths from the top-left cell to Chulsoo's cell that pass through at least one candy or chocolate cell.
- If is empty, Kere gets 0 candies and 0 chocolates. Otherwise, choose one path in uniformly at random. Walk along it and collect every candy and chocolate on the way.
Kere wants to know the expected number of candies and the expected number of chocolates collected with this map.
The problem is that Younghee does not know where Chulsoo is. So assume Chulsoo can be in any cell, and find the average of the expected candy count and the average of the expected chocolate count over all possible positions.
Some cells are known to hold an item, but the map may have labeled them as the wrong kind. Each time a report says the information for cell is wrong, flip that cell's label and report the average expected values on the updated map.
Input
The first line contains the grid size ().
The next lines each contain a string . The -th character of describes row , column of the map.
.: nothing in this cello: candy in this cellx: chocolate in this cell
The next line contains the number of reports (). The next lines each contain two integers and (). The cell at changes from chocolate to candy, or from candy to chocolate.
It is guaranteed that cell is not . in the original map.
Output
Print the answers as follows. Let . If the average expected value is the irreducible fraction , print modulo . For every Chulsoo position, the size of is either 0 or coprime to , and this is guaranteed.
On the first line, print the average expected candy count and the average expected chocolate count for the original map, separated by a space.
From line 2 through line , line prints the same two averages for the map after applying the first reports, separated by a space.
Hint
For the original map, the candy average is computed as follows.
- If Chulsoo is at row 1, column 1, no path contains a candy or chocolate, so Younghee gets 0 candies.
- If Chulsoo is at row 1, column 2, the result is also 0 candies.
- If Chulsoo is at row 2, column 1, the only shortest path with a candy or chocolate goes down one cell. It collects 1 candy.
- If Chulsoo is at row 2, column 2, there are two shortest paths: an L-shaped path and the other L-shaped path. The first collects no candy, and the second collects 1 candy. The expected candy count is 0.5.
So the average is , and the output is modulo , which is 623902721.
The chocolate average works out similarly to , so the output is modulo , which is 748683265.
After the first report, the map has no chocolate, so the chocolate average is 0.
After the second report, the map is the same as the original, so the first and third output lines are identical.