Hilbert Curve Intersections | 프로그래밍의 벗 PivotOJ
PivotOJ

Hilbert Curve Intersections

시간 제한: 1000ms메모리 제한: 128MB출처: ICPC Mid-Central Regional 2002BOJ 4645
이 문제는 본문 이미지 일부가 표시되지 않습니다. 텍스트만으로 풀이가 어려울 수 있습니다.

문제

  Starting curve Transformation 1 Transformation 2 Transformation 3 Transformation 4
H1 [이미지 1] [이미지 2] [이미지 3] [이미지 4] [이미지 5]
H2 [이미지 6] [이미지 7] [이미지 8] [이미지 9] [이미지 10]
H3 [이미지 11] [이미지 12] [이미지 13] [이미지 14] [이미지 15]
H4 [이미지 16] [이미지 17] [이미지 18] [이미지 19] [이미지 20]

David Hilbert proved the existence of a very counter-intuitive curve that fills space. The construction of the Hilbert curve is based on a sequence of curves, H1, H2, H3, H4, ... composed of horizontal and vertical segments. Each curve lies in the unit square [0, 1] × [0, 1]. H1 contains just three segments, connecting the points (¼, ¾) to (¼, ¼) to (¾, ¼) to (¾, ¾). Hn is defined recursively in terms of Hn-1, for n = 2, 3, ... by four transformations:

  • Halve all coordinates in Hn-1.
  • Add a copy rotated 90 degrees counterclockwise about the point (0, ½).
  • Add the reflection across the line x = ½.
  • Let m = ½n+1. Add segments connecting endpoints (½ - m, ½ - m) to (½ + m, ½ - m), (m, ½ - m) to (m, ½ + m), and (1 - m, ½ - m) to (1 - m, ½ + m).

Your job is to count the number of intersections of horizontal line segments with these curves. For example, consider Figures 1 and 2, which illustrate the first two example input data sets below.

Figure 1 Figure 2
[이미지 21] [이미지 22]
Segment from (2/8, 7/8) to (7/8, 7/8) crossing H3 three times. Segment from (0/16, 1/16) to (16/16, 1/16) crossing H4 sixteen times.

The coordinates of vertices of Hn are odd multiples of ½n+1. The coordinates of horizontal segment endpoints will always be multiples of ½n. Hence the specified horizontal segment can only cross vertical segments in Hn.

입력

Input consists of one to 100 data sets, one per line, followed by a final line containing only 0. Each data set consists of four integers separated by blanks in the form

n x1 x2 y

which represents Hn and the segment from (x1/2n, y/2n) to (x2/2n, y/2n), where 0 < n < 31, x1 < x2, and each of x1, x2, and y lie in the range 0 to 2n, inclusive.

출력

The output is one integer per line for each data set: the number of intersections of Hn with the segment. 

Caution: A brute force solution that computes each intersection individually will not finish within the one minute time limit. As you can see below, there may be more than one billion intersections for any data set

예제

예제 1

입력
3 2 7 7
4 0 16 1
30 1 1073741823 1
0
출력
3
16
1073741822
코드를 제출하려면 로그인하세요.