##### Canadian Computing Competition: 2016 Stage 1, Senior #5

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 cells. The cells are numbered from to in the order you would expect: that is, cell and cell are adjacent, cell and cell are adjacent, and so on up to cell , which is adjacent to cell . Since the trip is circular, cell is also adjacent to cell .

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 the 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 generations.

#### Input Specification

The first line will contain two space-separated integers and (). The second line will contain a string consisting of exactly characters, representing the initial configuration of the cells. Each character in the string will be either `0`

or `1`

. The initial state of cell is given by the 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, and .
- For an additional 6 of the 15 available marks, .
- For an additional 4 of the 15 available marks, and .

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

- in C/C++, the type
`long long`

should be used; - in Java, the type
`long`

should be used; - in Pascal, the type
`int64`

should be used.

#### Output Specification

Output the string of 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 and cell are adjacent to cell , and thus 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, configuration becomes `00011`

.

After two generations, configuration becomes `10111`

.

## Comments

I'm TLE'ing on Case 3 of Batch 6. My Java solution should implement in O(N log T) time. Does anyone have any suggestions?

Edit: Never mind, I fixed by using character array instead of string manip

Wow this problem is very elegant.

This comment is hidden due to too much negative feedback. Show it anyway.

I'd suggest you take a look on the upper bounds of and . If we're generous, and assume that DMOJ judges can run ~1e8 operations per second for Python 3, and your algorithm is , then we can ballpark your algorithm to run for approximately seconds years. In that time, we will:

TL;DR: is a very big number. Can you think of a way to greatly reduce this number down? (A starting place might be to think of cycles.)

If you're still struggling, the editorial exists :blobthumbsup: (but not for you to copy and gain free points).

:richard:

This comment is hidden due to too much negative feedback. Show it anyway.

This comment is hidden due to too much negative feedback. Show it anyway.

Getting TLE for batch 4-6 any tips or general directions to look into. A bit stuck here.

Remember that can be very large, look at the editorial for a faster algorithm

This comment is hidden due to too much negative feedback. Show it anyway.

how to get away with murder

I'm pretty sure this is what they call asking for a ban.

Edit: How did that banhammer taste?

That is correct.

This is a reminder that copy-pasting the editorial submission is neither approved, nor is it a learning opportunity. As such, anyone who has copy-pasted the editorial will:

I got WA on the second last test case... but the same code AC'd

This is likely due to the floating-point precision errors caused by different behavior of the function

`log`

on different judges.I'm getting WA on wcipeg

Try writing your own

`log`

function.Thanks, that worked!

What does Invalid Return mean? I guess my algorithm isn't just outputting a wrong answer but something else entirely?

likely, you are going over the python recursion depth limit ( times is too much)

here is a link to stackoverflow