This page is still under construction.

Parts of this page are still being built. What you see may change.

A Foldy but a Goody

Time limit1sMemory limit128 MB

Summary
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.

Upper and lower folds

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

Folding then unfolding

Place the left end of the folded strip at the origin (0,0)(0,0) and the first right angle at (1,0)(1,0). 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 TT, the number of test cases.

Each of the next TT 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 mm. The length of the string is between 11 and 3030 inclusive.

If the string describes nn folds, the unfolded strip is made of 2n2^n unit segments, so the two ends and all right angles occupy the positions 0,1,…,2n0, 1, \dots, 2^n. The value mm chooses one of them:

  • m=0m = 0 is the left end, located at (0,0)(0,0);
  • m=2nm = 2^n is the right end of the strip;
  • any mm with 1≤m≤2n−11 \le m \le 2^n - 1 is a right angle, counted from the left (m=1m = 1 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 xx, a comma with no surrounding spaces, the integer yy, and a closing parenthesis.

Assume that when there are nn folds the strip has length 2n2^n, so the distance between adjacent creases is exactly 11 unit.

Examples5

  1. Example 1

    Input
    3
    UL 4
    UL 3
    LLUL 13
    
    Expected output
    (2,0)
    (2,-1)
    (1,-2)
    
  2. Example 2

    Input
    3
    U 0
    U 1
    U 2
    
    Expected output
    (0,0)
    (1,0)
    (1,1)
    
  3. Example 3

    Input
    3
    L 0
    L 1
    L 2
    
    Expected output
    (0,0)
    (1,0)
    (1,-1)
    
  4. Example 4

    Input
    5
    UU 0
    UU 1
    UU 2
    UU 3
    UU 4
    
    Expected output
    (0,0)
    (1,0)
    (1,1)
    (0,1)
    (0,2)
    
  5. Example 5

    Input
    4
    LUL 0
    LUL 4
    LUL 7
    LUL 8
    
    Expected output
    (0,0)
    (2,0)
    (2,-1)
    (2,-2)