A Foldy but a Goody
Time limit1sMemory limit128 MB
Given a string of U and L folds, find the coordinates of the m-th point (end or right angle) on the unfolded strip.
- Level
Medium6 of 10
- Topics
- Recursion, Divide and conquer, Simulation, Math
- Solved
- No attempts yet
Problem
Suppose you have a strip of paper and may fold it in one of two ways:
- an upper fold, where the right end of the paper is brought over the top of the left end; and
- a lower fold, where the right end of the paper is brought below the left end.
The diagram below illustrates both kinds of folds.

After folding the strip several times, you unfold it again, opening every crease to a right angle of . The example below shows an upper fold, followed by a lower fold, and then the unfolding.

Place the left end of the folded strip at the origin and the first right angle at . It is natural to ask: where is the second right angle? The third? Where does the other end of the strip come to rest? Given a sequence of folds and an index, report the location of the chosen point.
Input
The first line contains an integer , the number of test cases.
Each of the next lines describes one test case: a string of the letters U and L, giving a series of upper and lower folds, followed by an integer . The length of the string is between and inclusive.
If the string describes folds, the unfolded strip is made of unit segments, so the two ends and all right angles occupy the positions . The value chooses one of them:
- is the left end, located at ;
- is the right end of the strip;
- any with is a right angle, counted from the left ( is the first right angle, and so on).
Output
For each test case, print one line giving the location of the requested right angle or end point, written as (x,y): an opening parenthesis, the integer , a comma with no surrounding spaces, the integer , and a closing parenthesis.
Assume that when there are folds the strip has length , so the distance between adjacent creases is exactly unit.