Fractal

Time limit1sMemory limit128 MB

Summary
Given a recursively self-similar fractal built from a base polyline, find the point at a given fraction of its total arc length after d recursive refinements.
Level

Medium6 of 10

Topics
Recursion, Geometry, Math
Solved
No attempts yet

Problem

Fractals are fascinating mathematical objects. They often share several interesting properties:

  • fine structure at arbitrarily small scales;
  • self-similarity: when magnified, a part looks like a copy of the whole;
  • a simple, recursive definition.

Approximate fractals appear throughout nature, for example in clouds, snowflakes, mountain ranges, and river networks.

In this problem we consider fractals generated by the following procedure. We start with a polyline, that is, a chain of connected line segments defined by points p1,p2,…,pnp_1, p_2, \dots, p_n. This polyline is the fractal of depth one.

To build the fractal of depth two, we replace every line segment by a scaled and rotated copy of the whole original polyline: the copy is the unique similarity transform (rotation and uniform scaling only, no reflection) that maps the polyline's first point p1p_1 onto the segment's start and its last point pnp_n onto the segment's end. Repeating this replacement, at each step substituting every current segment by a transformed copy of the original polyline, yields fractals of arbitrary depth dd with increasingly fine structure.

The complexity of such a fractal grows rapidly with its depth. Given a fraction ff of the total length, we want to know the coordinate of the point reached after travelling that fraction along the fractal curve, starting from p1p_1.

Input

The first line contains a single integer cc (1≤c≤2001 \le c \le 200), the number of test cases. Each test case is given as follows:

  • one line with an integer nn (3≤n≤1003 \le n \le 100), the number of points of the polyline;
  • nn lines, the ii-th of which contains two integers xix_i and yiy_i (−1000≤xi,yi≤1000-1000 \le x_i, y_i \le 1000), the consecutive points of the polyline;
  • one line with an integer dd (1≤d≤101 \le d \le 10), the depth of the fractal;
  • one line with a floating point number ff (0≤f≤10 \le f \le 1), the fraction of the total length that is traversed.

The length of each line segment of the polyline is strictly smaller than the distance between the first point (x1,y1)(x_1, y_1) and the last point (xn,yn)(x_n, y_n). The total length of the polyline is strictly smaller than twice that distance.

Output

For each test case, print one line with the coordinate of the point reached after traversing the fraction ff of the fractal's length.

Print it in the form (x,y)(x,y) where xx and yy are each written with exactly six digits after the decimal point and with no spaces, for example (0.426777,2.000000).

Examples3

  1. Example 1

    Input
    1
    4
    -2 -2
    0 0
    0 2
    2 2
    3
    0.75
    
    Expected output
    (0.426777,2.000000)
    
  2. Example 2

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

    Input
    1
    5
    1 1
    3 1
    4 2
    5 1
    7 1
    1
    0.9
    
    Expected output
    (6.317157,1.000000)