One-Dimensional Cellular Automaton
Time limit2sMemory limit128 MB
Simulate a linear recurrence over N cells modulo M for up to 1e9 steps using fast matrix exponentiation.
- Level
Medium6 of 10
- Topics
- Matrix, Math, Simulation
- Solved
- No attempts yet
Problem
There is a one-dimensional cellular automaton made of cells, numbered from to .
Each cell has a state, a non-negative integer less than . The states evolve as time advances by one unit. Let be the state of cell at time . The state at time is given by
where , , are non-negative integers. For or , we take .
Given the initial state of the automaton, write a program that computes the state of the cells after time units.
Input
Each test case has the following format.
N M A B C T
S(0,0) S(1,0) ... S(N-1,0)
The constraints are , , , and .
The last line of the input contains six zeros.
Output
For each test case, output the state of the cells at time , in the following format.
S(0,T) S(1,T) ... S(N-1,T)
Each cell state is an integer, and the values are separated by spaces.