This page is still under construction.

Parts of this page are still being built. What you see may change.

Fractal Cake

Interview

Time limit1sMemory limit128 MB

Summary
Print the chocolate pattern of a rectangular window in a 2^(N+1) square grid built by recursively darkening the middle 2x2 of every 4x4 block N times.
Level

Medium6 of 10

Topics
Divide and conquer, Recursion, Implementation, Math
Solved
No attempts yet

Problem

Fyodor celebrates his birthday today. Before the guests arrive he decorates a cake with chocolate cream in a special way.

At the start the cake is a square split into 4 equal white square cells — a 2×22 \times 2 grid.

Fyodor calls the following sequence of steps a fractalization:

  1. Group all current cells into non-overlapping 2×22 \times 2 groups, so that no cell is left ungrouped.
  2. Split every cell into 4 equal cells, so each 2×22 \times 2 group becomes a 4×44 \times 4 group. Every new cell keeps the color of the cell it was split from.
  3. Fill the 4 central cells (the middle 2×22 \times 2) of each 4×44 \times 4 group with chocolate.

Fyodor does not stop after one fractalization: he repeats it N times, even when he needs a microscope. The picture below shows the initial cake, the result after the first fractalization, and the cake after the fifth fractalization:

After N fractalizations the cake is a 2N+1×2N+12^{N+1} \times 2^{N+1} grid of cells. Fyodor wants a program that quickly shows the pattern of a chosen rectangular part of the cake.

Input

A single line contains five non-negative integers N, R1, R2, C1, C2:

  • N — the number of fractalization iterations (N<20N < 20).
  • R1, R2 — the first and last row of the part.
  • C1, C2 — the first and last column of the part.

Rows and columns are numbered from 0. The following restrictions hold: R1≤R2R1 \le R2, C1≤C2C1 \le C2; 0≤R2−R1<1000 \le R2 - R1 < 100 and 0≤C2−C1<1000 \le C2 - C1 < 100; 0≤R1,R2,C1,C2<2N+10 \le R1, R2, C1, C2 < 2^{N+1}.

Output

Print R2−R1+1R2 - R1 + 1 lines, each containing C2−C1+1C2 - C1 + 1 characters. Each character corresponds to one cell: it is 1 if the cell is filled with chocolate and 0 otherwise.

Examples3

  1. Example 1

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

    Input
    2 0 3 0 3
    
    Expected output
    0000
    0110
    0111
    0011
    
  3. Example 3

    Input
    13 50 55 95 100
    
    Expected output
    101111
    100111
    100111
    101111
    101101
    100001