Menger Sponge

시간 제한2초메모리 제한1024 MB

요약
레벨 L과 단위 정육면체 안의 유리수 좌표 점이 주어질 때, 그 점이 레벨 L 멩거 스펀지에 속하는지 판정한다.
난이도

보통10점 중 5점

유형
수학, 재귀, 정수론
정답자
아직 제출이 없습니다

문제

The Menger sponge is a simple 3D fractal. Its level-LL approximation can be constructed with the following algorithm:

  • Start with a single solid 1×1×11 \times 1 \times 1 cube with opposite corners at (0,0,0)(0,0,0) and (1,1,1)(1,1,1).

  • For each iteration i=1,…,Li=1, \dots ,L:

    • For each cube:

      • Cut the cube into a regular 3×3×33 \times 3 \times 3 grid of 2727 subcubes.
      • Delete the seven subcubes that don’t touch an edge of the parent cube (see illustration).

The points in the level-LL Menger sponge are those that remain after running the above algorithm. Points exactly on the boundary of cubes that remain in the sponge are part of the sponge.

The picture below shows the result for L=0L=0 through L=3L=3:

Given a level LL and a point in space given by three rational coordinates, determine if the point is in the level-LL Menger sponge.

입력

The single line of input contains seven integers LL, x_numx\_{\text{num}}, x_denomx\_{\text{denom}}, y_numy\_{\text{num}}, y_denomy\_{\text{denom}}, z_numz\_{\text{num}}, z_denomz\_{\text{denom}}:

  • 0≤L≤1050≤L≤10^5
  • 0\<x_num\<x_denom≤1060\<x\_{\text{num}}\<x\_{\text{denom}}≤10^6
  • 0\<y_num\<y_denom≤1060\<y\_{\text{num}}\<y\_{\text{denom}}≤10^6
  • 0\<z_num\<z_denom≤1060\<z\_{\text{num}}\<z\_{\text{denom}}≤10^6

where LL is the level of the Menger Sponge and the point in question is (x_numx_denom,y_numy_denom,z_numz_denom)\displaystyle\left(\frac{x\_{\text{num}}}{x\_{\text{denom}}}, \frac{y\_{\text{num}}}{y\_{\text{denom}}}, \frac{z\_{\text{num}}}{z\_{\text{denom}}}\right).

출력

Output a single integer, which is 11 if the point is in the level-LL Menger Sponge, or 00 if not.

예제3

  1. 예제 1

    입력
    1000 1 3 1 3 1 3
    
    예상 출력
    1
    
  2. 예제 2

    입력
    2 49 81 5 6 20 81
    
    예상 출력
    1
    
  3. 예제 3

    입력
    3 49 81 5 6 20 81
    
    예상 출력
    0