Hilbert Curve
Time limit1sMemory limit128 MB
Count how many points the n-th Hilbert curve shares with a given horizontal segment whose endpoints are grid multiples of 1/2^n.
- Level
Hard8 of 10
- Topics
- Recursion, Divide and conquer, Geometry, Implementation
- Solved
- No attempts yet
Problem
The Hilbert curve is a continuous, space-filling fractal curve first described by the German mathematician David Hilbert.
It is defined through a sequence of curves , each made of horizontal and vertical segments. Every curve lies inside the unit square .
consists of the three segments that connect the four points , , , in order.
is built recursively from through the following four steps.
- Halve every coordinate of .
- Add a copy of that curve rotated counterclockwise about the point .
- Add a copy of the curve obtained so far, reflected across the line .
- Let . Join the pieces with three segments: connect with , connect with , and connect with .
Given a horizontal segment, write a program that counts how many points it shares with the curve. For example, meets the horizontal segment – at points, and meets the horizontal segment – at points.
The vertex coordinates of are always odd multiples of , while the endpoint coordinates of the horizontal segment are always multiples of . Consequently the horizontal segment only ever meets the vertical parts of .
Input
The input consists of several datasets (test cases), at most of them.
Each dataset is given as four space-separated integers , , , , describing the curve and the horizontal segment –. Here and , and , , are all integers in the range .
A single line containing follows the last dataset and marks the end of the input.
Output
For each dataset, print on its own line the number of points where the curve meets the given horizontal segment.