Race for the Galaxy
Time limit4sMemory limit1024 MB
Count non-intersecting monotone grid paths for N runners with puddles and mud rows, grouped by how many runners cross the mud row S to S+1.
- Level
Hard9 of 10
- Topics
- Dynamic programming, Combinatorics, Graph
- Solved
- No attempts yet
Problem
A school playground has a large grid with horizontal lines and vertical lines. The intersection of the -th horizontal line from the top and the -th vertical line from the left is called .
Heavy rain yesterday created several obstacles on the playground.
- Between the -th and -th horizontal lines there is a muddy area covering the segments of vertical lines. That is, for each vertical line number , the vertical segment connecting and is covered in mud.
- There are puddles. The -th puddle is at .
Hyea plans to draw running tracks on the playground, one for each of runners, for a race at the sports day. The event is meant to build harmony more than to record times, so the race has several rules.
- The goal of each track is to start at the starting point on the first horizontal line and finish at the ending point on the last horizontal line. Each runner has a fixed start and end. The -th runner starts at and must finish at . Here and .
- Runners must run along the horizontal and vertical lines of the grid. They cannot leave the grid, move in a direction that is neither horizontal nor vertical, or change direction anywhere except at an intersection.
- When moving along a vertical line, a runner must move downward.
- When moving along a horizontal line, a runner must move left on an odd-numbered horizontal line and must move right on an even-numbered horizontal line.
- Puddles are dangerous because the ground is dug out there, so no track may pass through an intersection with a puddle.
- Two runners colliding is also dangerous, so no two tracks may share an intersection.
The rules stated more precisely:
- The track of the -th runner is a sequence of intersections that starts at , ends at , and in which consecutive intersections are adjacent. () Two intersections and are adjacent when .
- Every intersection in a sequence must satisfy and .
- cannot appear in any sequence. ()
- For with and , the element after cannot be .
- For and with and : if is odd, the element after cannot be . If is even, the element after cannot be .
- Each intersection belongs to at most one sequence.
Among all ways to draw the tracks under these rules, count the number of ways in which exactly runners pass through a muddy area, for each . The numbers can be very large, so print each count modulo the prime ().
- The -th runner passes through a muddy area if there is some such that and appear consecutively in the sequence.
Input
The first line contains , , , , and , separated by spaces. (; ; ; )
The second line contains , separated by spaces. ()
The third line contains , separated by spaces. ()
The fourth line contains , separated by spaces. ()
If , each of the next lines contains and , separated by a space. (; )
All puddle positions are distinct.
Output
Print integers on the first line, separated by spaces. The -th integer is the number of ways in which exactly runners pass through a muddy area, modulo (). ()