One-Dimensional Cellular Automaton

Time limit2sMemory limit128 MB

Problem

There is a one-dimensional cellular automaton made of $N$ cells, numbered from $0$ to $N-1$.

Each cell has a state, a non-negative integer less than $M$. The states evolve as time advances by one unit. Let $S(i, t)$ be the state of cell $i$ at time $t$. The state at time $t+1$ is given by

$$S(i, t+1) = (A \times S(i-1, t) + B \times S(i, t) + C \times S(i+1, t)) \bmod M$$

where $A$, $B$, $C$ are non-negative integers. For $i < 0$ or $i \ge N$, we take $S(i, t) = 0$.

Given the initial state of the automaton, write a program that computes the state of the cells after $T$ 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 $0 < N \le 50$, $0 < M \le 1000$, $0 \le A, B, C < M$, and $0 \le T \le 10^9$.

The last line of the input contains six zeros.

Output

For each test case, output the state of the cells at time $T$, 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.