Hilbert Curve

Time limit1sMemory limit128 MB

Summary
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 H1,H2,H3,…H_1, H_2, H_3, \dots, each made of horizontal and vertical segments. Every curve lies inside the unit square [0,1]×[0,1][0, 1] \times [0, 1].

H1H_1 consists of the three segments that connect the four points (14,34)(\frac{1}{4}, \frac{3}{4}), (14,14)(\frac{1}{4}, \frac{1}{4}), (34,14)(\frac{3}{4}, \frac{1}{4}), (34,34)(\frac{3}{4}, \frac{3}{4}) in order.

HnH_n is built recursively from Hn−1H_{n-1} through the following four steps.

  1. Halve every coordinate of Hn−1H_{n-1}.
  2. Add a copy of that curve rotated 90∘90^\circ counterclockwise about the point (0,12)(0, \frac{1}{2}).
  3. Add a copy of the curve obtained so far, reflected across the line x=12x = \frac{1}{2}.
  4. Let m=12n+1m = \frac{1}{2^{n+1}}. Join the pieces with three segments: connect (12−m,12−m)(\frac{1}{2} - m, \frac{1}{2} - m) with (12+m,12−m)(\frac{1}{2} + m, \frac{1}{2} - m), connect (m,12−m)(m, \frac{1}{2} - m) with (m,12+m)(m, \frac{1}{2} + m), and connect (1−m,12−m)(1 - m, \frac{1}{2} - m) with (1−m,12+m)(1 - m, \frac{1}{2} + m).

Given a horizontal segment, write a program that counts how many points it shares with the curve. For example, H3H_3 meets the horizontal segment (28,78)(\frac{2}{8}, \frac{7}{8})–(78,78)(\frac{7}{8}, \frac{7}{8}) at 33 points, and H4H_4 meets the horizontal segment (016,116)(\frac{0}{16}, \frac{1}{16})–(1616,116)(\frac{16}{16}, \frac{1}{16}) at 1616 points.

The vertex coordinates of HnH_n are always odd multiples of 12n+1\frac{1}{2^{n+1}}, while the endpoint coordinates of the horizontal segment are always multiples of 12n\frac{1}{2^{n}}. Consequently the horizontal segment only ever meets the vertical parts of HnH_n.

Input

The input consists of several datasets (test cases), at most 100100 of them.

Each dataset is given as four space-separated integers nn, x1x_1, x2x_2, yy, describing the curve HnH_n and the horizontal segment (x12n,y2n)(\frac{x_1}{2^n}, \frac{y}{2^n})–(x22n,y2n)(\frac{x_2}{2^n}, \frac{y}{2^n}). Here 0<n<310 < n < 31 and x1<x2x_1 < x_2, and x1x_1, x2x_2, yy are all integers in the range [0,2n][0, 2^n].

A single line containing 00 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 HnH_n meets the given horizontal segment.

Examples6

  1. Example 1

    Input
    3 2 7 7
    4 0 16 1
    30 1 1073741823 1
    0
    
    Expected output
    3
    16
    1073741822
    
  2. Example 2

    Input
    1 0 2 1
    0
    
    Expected output
    2
    
  3. Example 3

    Input
    2 0 4 1
    2 0 4 2
    2 0 4 3
    0
    
    Expected output
    4
    2
    2
    
  4. Example 4

    Input
    3 0 8 0
    3 0 8 8
    0
    
    Expected output
    0
    0
    
  5. Example 5

    Input
    4 0 16 8
    4 3 5 8
    0
    
    Expected output
    2
    0
    
  6. Example 6

    Input
    3 3 4 5
    3 0 1 5
    3 1 3 3
    0
    
    Expected output
    1
    1
    2