Circle of Life
Time limit2sMemory limit512 MB
A circular binary strip updates so each cell is alive next generation only if exactly one neighbor is alive; find the state after T generations, with T up to 10^15.
- Level
Medium7 of 10
- Topics
- Bit manipulation, Math, Simulation, Implementation
- Solved
- No attempts yet
Problem
Conway's Game of Life applies a handful of rules to cells on a grid and still produces very complicated configurations. This problem uses a version of that game simplified down to a one dimensional circular strip.
There is a circular strip of cells. The cells are numbered from 1 to in order: cell 1 and cell 2 are adjacent, cell 2 and cell 3 are adjacent, and so on up to cell , which is adjacent to cell . Since the strip is circular, cell 1 is also adjacent to cell .
Each cell is either alive or dead. An alive cell is written as the character '1' and a dead cell as the character '0'. When a generation passes, every cell changes state at the same time. If exactly one of a cell's two neighbours is alive in the current generation, then the cell is alive in the next generation. Otherwise the cell is dead in the next generation.
Given the initial state of the strip, find the state after generations.
Input
The first line contains two integers and separated by a space (, ).
The second line contains a string of exactly characters describing the initial state. Each character is either '0' or '1', and the -th character is the initial state of cell . The character '1' means an alive cell and '0' means a dead cell.
Output
Print the state of the cells after generations on one line, in the same format and order as the input. That is, print a string of characters whose -th character is '1' if cell is alive and '0' if it is dead.