Plotter
Time limit5sMemory limit128 MB
Given the recursively defined order-n bytecurve and m integer points, report how many times and at which seconds the pen visits each point.
- Level
Hard8 of 10
- Topics
- Recursion, Divide and conquer, Implementation, Math
- Solved
- No attempts yet
Problem
To test his newly bought plotter, Byteasar decides to draw a few bytecurves.
A bytecurve of order consists of segments, each of length . The very first segment joins the points and . The bytecurve of order is described by a word of length over the two-letter alphabet . The -th letter tells the pen, right after the -th segment has been drawn, to turn by and continue at a right angle either to the left (letter ) or to the right (letter ) before drawing the next segment.
is the single letter (one left turn), and (two left turns followed by one right turn). In general, is built from like this: write the letters of separated by single spaces and add one extra space before the first letter and one after the last, then fill the newly created gaps from left to right with the alternating letters starting with . For example,
and in the same way (drawn in the figure below).

Drawing one segment takes exactly one second, and the pen starts at at time . While the plotter works, Byteasar wonders: for a given point , at which moments is the pen located there? For example, on the order- bytecurve above the pen is at after seconds and again after seconds. Answer Byteasar's question.
Input
The first line contains two integers and (): the curve is and there are query points. Each of the next lines contains two integers and (), the coordinates of the -th query point. A query point need not lie on the curve, and no point appears twice in the input.
Output
Print lines, one per query in order. For the -th query print a nonnegative integer , the number of times the pen is at while the order- curve is drawn (the starting position at time counts as a visit), followed by those visit times in increasing order, in seconds since drawing began. Separate all numbers on a line by single spaces, with no leading or trailing space.
Hint
If you enjoyed this problem, try the harder variant that asks the same question under tighter limits.