A Foldy but a Goody
Time limit1sMemory limit128 MB
Fold a paper strip n times by upper and lower folds, unfold it into right angles, and find the coordinates of the m-th point along it.
- Level
Medium7 of 10
- Topics
- Recursion, Divide and conquer, Implementation, Math
- Solved
- No attempts yet
Problem
You have a strip of paper and repeatedly fold it in one of two ways.
- Upper fold (
U): bring the right end of the strip over onto the left end (folding it in half) so that the moved half lies on top. - Lower fold (
L): bring the right end of the strip onto the left end so that the moved half lies underneath.
After folding the strip several times, you unfold it again, opening every crease to a right angle (exactly 90 degrees). This turns the strip into a path made of unit-length segments joined at right angles.
Place the left end of the unfolded strip at the origin , and let the first right angle be at . Given the sequence of folds and a position along the paper, determine the coordinates of that position.
Input
The first line contains an integer , the number of test cases.
Each test case is a single line containing a string of the letters U and L, followed by an integer . The string lists the folds in the order they are applied (from left to right), and its length satisfies .
After folds and unfolding, the strip consists of unit segments separated by right angles. The value selects a position on the paper:
- is the left end, at .
- is the right end of the strip.
- any with is a right angle: counted from the left end, is the first right angle (at ), the second, and so on.
Output
For each test case, output one line of the form (x,y) giving the coordinates of the position selected by .