Gangs
Time limit1sMemory limit128 MB
Rank all lattice paths of E and S steps from (1,1) to the diagonal at (N,N) by a recursive 'OG' order, and output the run at rank M or ERROR.
- Level
Hard8 of 10
- Topics
- Combinatorics, Dynamic programming, Recursion, Math
- Solved
- No attempts yet
Problem
The downtown core of a city is a grid. Streets run north-south and are numbered from 1st Street in the west to 20th Street in the east; avenues run east-west and are numbered from 1st Avenue in the north to 20th Avenue in the south. The city is split between two gangs, the Blips and the Cruds, by the Green Line, which runs diagonally from the corner of 1st Street and 1st Avenue to the corner of 20th Street and 20th Avenue. The Blips control the area to the southwest of the Green Line, and the Cruds the area to the northeast.
To prove themselves, the Blips make runs through Crud territory. A run starts at the intersection of 1st Street and 1st Avenue and ends at a point on the Green Line. A run may touch the Green Line along the way but never crosses it. It moves along avenues only toward the east and along streets only toward the south, so it is a string of E's (east) and S's (south). A run of length ends at the intersection of the th Street and the th Avenue.
On a given night every run has the same length, and the Blips rank them by how OG they are. A run is more OG than a run if and only if one of the following holds:
- returns to the Green Line for the first time strictly earlier than does; or
- and return to the Green Line for the first time at the same point, but the part of up to that point, with its leading E and trailing S removed, is more OG than the corresponding part of ; or
- and return to the Green Line for the first time at the same point and are identical up to that point, but the remainder of is more OG than the remainder of .
For example:
EESSis more OG thanESES.EEESSSis more OG thanEESESS.EESSEESSis more OG thanEESSESES.
List all runs of a given length from most OG to least OG. The rank of a run is its 1-based position in this list. For length 4, EESS has rank 1 and ESES has rank 2.
Input
The input is a sequence of instances, terminated by a line containing 0 0.
Each instance is a single line with two positive integers and : is the terminus of that night's runs (the th Street and th Avenue), and is the desired rank. You may assume .
Output
For each instance, print the run of length whose rank is , or ERROR if there are fewer than runs of length .