CCCGuide

Problem S5: Circle of Life

2016 · S5 · Original problem · DMOJ

Problem Description

You may have heard of Conway’s Game of Life, which is a simple set of rules for cells on a grid that can produce incredibly complex configurations. In this problem we will deal with a simplified version of the game.

There is a one-dimensional circular strip of NN cells. The cells are numbered from 11 to NN in the order you would expect: that is, cell 1 and cell 2 are adjacent, cell 2 and cell 3 are adjacent, and so on up to cell N−1N-1, which is adjacent to cell NN. Since the strip is circular, cell 11 is also adjacent to cell NN.

Each cell is either alive (represented by a ‘1’) or dead (represented by a ‘0’). The cells change over a number of generations. If exactly one of a cell’s neighbours is alive in the current generation, then the cell will be alive in the next generation. Otherwise, the cell will be dead in the next generation.

Given the initial state of the strip, find the state after TT generations.

Input Specification

The first line will contain two space-separated integers NN and TT (3≤N≤100 000;1≤T≤1015)(3 \leq N \leq 100\ 000; 1 \leq T \leq 10^{15}). The second line will contain a string consisting of exactly NN characters, representing the initial configuration of the NN cells. Each character in the string will be either ‘0’ or ‘1’. The initial state of cell ii is given by the ii-th character of the string. The character ‘1’ represents an alive cell and the character ‘0’ represents a dead cell.

For 1 of the 15 available marks, N≤15N \leq 15 and T≤15T \leq 15.

For an additional 6 of the 15 available marks, N≤15N \leq 15.

For an additional 4 of the 15 available marks, N≤4000N \leq 4000 and T≤10 000 000T \leq 10\ 000\ 000.

Note that for full marks, solutions will need to handle 64-bit integers. For example

Output Specification

Output the string of NN characters representing the final state of the cells, in the same format and order as the input.

Sample Input 1

7 1
0000001

Output for Sample Input 1

1000010

Explanation for Output for Sample Input 1

Cell 1 and cell N−1N-1 are adjacent to cell NN, and thus are alive after one generation.

Sample Input 2

5 3
01011

Output for Sample Input 2

10100

Explanation for Output for Sample Input 2

After one generation, the configuration becomes 00011.

After two generations, the configuration becomes 10111.