Fractal Cake
InterviewTime limit1sMemory limit128 MB
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 grid.
Fyodor calls the following sequence of steps a fractalization:
- Group all current cells into non-overlapping groups, so that no cell is left ungrouped.
- Split every cell into 4 equal cells, so each group becomes a group. Every new cell keeps the color of the cell it was split from.
- Fill the 4 central cells (the middle ) of each 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 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 ().
- 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: , ; and ; .
Output
Print lines, each containing characters. Each character corresponds to one cell: it is 1 if the cell is filled with chocolate and 0 otherwise.