atcoder#APC001I. Simple APSP Problem
Simple APSP Problem
Score : points
Problem Statement
You are given an grid. The square at the top-left corner will be represented by , and the square at the bottom-right corner will be represented by .
Of those squares, squares are painted black, and the other squares are painted white.
Let the shortest distance between white squares and be the minimum number of moves required to reach from visiting only white squares, where one can travel to an adjacent square sharing a side (up, down, left or right) in one move.
Since there are white squares in total, there are ways to choose two of the white squares.
For each of these ways, find the shortest distance between the chosen squares, then find the sum of all those distances, modulo .
Constraints
- If , then either or .
- There is at least one white square.
- For every pair of white squares and , it is possible to reach from visiting only white squares.
Input
Input is given from Standard Input in the following format:
Output
Print the sum of the shortest distances, modulo .
2 3
1
1 1
20
We have the following grid (.
: a white square, #
: a black square).
...
.#.
Let us assign a letter to each white square as follows:
ABC
D#E
Then, we have:
- dist(A, B) =
- dist(A, C) =
- dist(A, D) =
- dist(A, E) =
- dist(B, C) =
- dist(B, D) =
- dist(B, E) =
- dist(C, D) =
- dist(C, E) =
- dist(D, E) =
where dist(A, B) is the shortest distance between A and B. The sum of these distances is .
2 3
1
1 2
16
Let us assign a letter to each white square as follows:
ABC
DE#
Then, we have:
- dist(A, B) =
- dist(A, C) =
- dist(A, D) =
- dist(A, E) =
- dist(B, C) =
- dist(B, D) =
- dist(B, E) =
- dist(C, D) =
- dist(C, E) =
- dist(D, E) =
The sum of these distances is .
3 3
1
1 1
64
4 4
4
0 1
1 1
2 1
2 2
268
1000000 1000000
1
0 0
333211937