Hexagonal Region
Time limit2sMemory limit1024 MB
Given a closed, simple hex-grid path of N moves, sum A + d*B over every cell in the enclosed region, where d is the distance from the start.
- Level
Hard8 of 10
- Topics
- Geometry, Shortest path, Math
- Solved
- No attempts yet
Problem
Hexagonal tiles are laid out in an infinite grid of cells, and Jaehyeon stands on a cell called the "starting cell". Two cells are neighbors if they share an edge. As shown in the figure below, each move lets Jaehyeon step to a neighboring cell in one of six directions numbered 1 to 6.

The path formed by Jaehyeon's moves forms a region. On the -th move, he travels cells in direction . The path has the following properties.
- The path is closed. The last cell reached is the starting cell.
- The path is simple. Every cell except the starting cell is visited at most once. The starting cell is visited exactly twice, counting the first and last positions together.
- The path is exposed. Every cell on the path has at least one neighbor that is not on the path and is not interior.
- A cell not on the path is interior if the number of cells that can be visited from it without passing through any cell on the path is finite.
The following is one path Jaehyeon can take.
- Cell 1 (pink) is the starting (and ending) cell.
- Light blue cells are on the path, and the visit order is written inside each cell.
- Dark blue cells marked with an x are interior cells.

The region consists of all cells on the path or in the interior. The distance of a cell in the region is the minimum number of moves needed to reach from the starting cell, moving only through cells in the region. The score of a cell in the region is , where and are constants chosen in advance by Jaehyeon and is the distance of that cell.
The figure below shows the distance of each cell in the region formed by the path above.

Write a program that computes the sum of the scores of all cells in the region formed by Jaehyeon's moves. The total can be very large, so output it modulo .
Constraints
- (for all )
- (for all )
- The sum of all elements of does not exceed .