eolymp
bolt
Try our new interface for solving problems
Problems

Rule 110

Rule 110

Ann is decorating her office with the coolest arrangement of lights ever. She is using very long LED strips, where each individual cell is switched on or off every second, according to the following simple and pretty algorithm. At each step, the status of each cell ($0$ for off and $1$ for on) is determined from the status of its two neighbor cells on the strip (left and right) and its own status, according to the following table: \includegraphics{https://static.eolymp.com/content/hl/hlcm8plakl5tbb2o9v43et16o0.gif} Ann is choosing an initial configuration for the cells and she marvels at the resulting animation, which happens to be highly similar to Conway’s Game of Life, with interesting behavior on the boundary between stability and chaos. \InputFile The first line contains the initial configuration, as a string of $16$ characters $0$ and $1$. All the cells to the left and to the right of this string are considered to be $0$. The second line contains the number $n~(0 \le n < 2^{60})$ of steps to perform. The LED strip is considered to be large enough to ensure that no $1$-cells will ever reach the ends of the strip. \OutputFile The output should contain a single line with a single integer that is the total number of $1$-cells in the final configuration. \Examples The output is $11$ since we have the following five steps: \begin{lstlisting}[language=C++] ...0000000010011011111000... ...0000000110111110001000... ...0000001111100010011000... ...0000011000100110111000... ...0000111001101111101000... ...0001101011111000111000... \end{lstlisting} where everything not displayed contains only $0$-cells.
Time limit 1 second
Memory limit 128 MiB
Input example #1
0001001101111100
5
Output example #1
11
Source 2021 ACM Southwestern Europe Regional Contest (SWERC), Paris, March 7, Problem B