Fractal
Time limit1sMemory limit128 MB
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.
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 . 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 onto the segment's start and its last point 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 with increasingly fine structure.
The complexity of such a fractal grows rapidly with its depth. Given a fraction of the total length, we want to know the coordinate of the point reached after travelling that fraction along the fractal curve, starting from .
Input
The first line contains a single integer (), the number of test cases. Each test case is given as follows:
- one line with an integer (), the number of points of the polyline;
- lines, the -th of which contains two integers and (), the consecutive points of the polyline;
- one line with an integer (), the depth of the fractal;
- one line with a floating point number (), 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 and the last point . 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 of the fractal's length.
Print it in the form where and are each written with exactly six digits after the decimal point and with no spaces, for example (0.426777,2.000000).