Problem:
Problem C: One-Dimensional Cellular Automaton

There is a one-dimensional cellular automaton consisting of N cells. Cells are numbered from 0 to N − 1.

Each cell has a state represented as a non-negative integer less than M. The states of cells evolve through discrete time steps. We denote the state of the i-th cell at time t as S(i, t). The state at time t + 1 is defined by the equation

S(i, t + 1) = (A × S(i − 1, t) + B × S(i, t) + C × S(i + 1, t)) mod M,         (1)

where A, B and C are non-negative integer constants. For i < 0 or N ≤ i, we define S(i, t) = 0.

Given an automaton definition and initial states of cells, your mission is to write a program that computes the states of the cells at a specified time T.

Input

The input is a sequence of datasets. Each dataset is formatted as follows.

N M A B C T

S(0, 0) S(1, 0) ... S(N − 1, 0)

The first line of a dataset consists of six integers, namely N, M, A, B, C and T. N is the number of cells. M is the modulus in the equation (1). A, B and C are coefficients in the equation (1). Finally, T is the time for which you should compute the states.

You may assume that 0 < N ≤ 50, 0 < M ≤ 1000, 0 ≤ A, B, C < M and 0 ≤ T ≤ 109.

The second line consists of N integers, each of which is non-negative and less than M. They represent the states of the cells at time zero.

A line containing six zeros indicates the end of the input.

Output

For each dataset, output a line that contains the states of the cells at time T. The format of the output is as follows.

S(0, T) S(1, T) ... S(N − 1, T)

Each state must be represented as an integer and the integers must be separated by a space.

Sample Input

5 4 1 3 2 0
0 1 2 0 1
5 7 1 3 2 1
0 1 2 0 1
5 13 1 3 2 11
0 1 2 0 1
5 5 2 0 1 100
0 1 2 0 1
6 6 0 2 3 1000
0 1 2 0 1 4
20 1000 0 2 3 1000000000
0 1 2 0 1 0 1 2 0 1 0 1 2 0 1 0 1 2 0 1
30 2 1 0 1 1000000000
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0
30 2 1 1 1 1000000000
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
30 5 2 3 1 1000000000
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0

Output for the Sample Input

0 1 2 0 1
2 0 0 4 3
2 12 10 9 11
3 0 4 2 1
0 4 2 0 4 4
0 376 752 0 376 0 376 752 0 376 0 376 752 0 376 0 376 752 0 376
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0
1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0
1 1 3 2 2 2 3 3 1 4 3 1 2 3 0 4 3 3 0 4 2 2 2 2 1 1 2 1 3 0

Solution in JAVA (judge verdict: Accepted)


import java.io.IOException;
import java.io.PrintWriter;

public class Main {
	int N, M, A, B, C, T;

	public void solve() {
		while(true){
			N = nextInt();
			M = nextInt();
			A = nextInt();
			B = nextInt();
			C = nextInt();
			T = nextInt();
			if(N == 0) break;
			IntMatrix mat = new IntMatrix(N, N);
			for(int i = 0; i < N; i++){
				if(i - 1 >= 0) mat.set(i, i - 1, A);
				mat.set(i, i, B);
				if(i + 1 < N) mat.set(i, i + 1, C);
			}
			IntVector vec = new IntVector(N);
			for(int i = 0; i < N; i++){
				vec.set(i, nextInt());
			}

			IntVector ans = mat.pow(T).mul(vec);
			for(int i = 0; i < N; i++){
				if(i > 0) out.print(" ");
				out.print(ans.get(i));
			}
			out.println();
		}

	}

	public IntMatrix identity(int n) {
		IntMatrix mat = new IntMatrix(n, n);
		for(int i = 0; i < n; i++){
			for(int j = 0; j < n; j++){
				if(i == j) mat.set(i, j, 1);
				else mat.set(i, j, 0);
			}
		}
		return mat;
	}

	class IntMatrix {
		public final int row;
		public final int colum;
		int[] data;

		public IntMatrix(int row, int colum) {
			this.row = row;
			this.colum = colum;
			this.data = new int[row * colum];
		}

		public int index(int r, int c) {
			if(0 <= r && r < row && 0 <= c && c <= colum){
				return r * colum + c;
			}else{
				throw new IndexOutOfBoundsException("Matrix size row: " + row + " colum:" + colum + "access: (" + r + "," + c + ")");
			}
		}

		public void set(int r, int c, int v) {
			this.data[index(r, c)] = v;
		}

		public int get(int r, int c) {
			return this.data[index(r, c)];
		}

		public IntMatrix mul(IntMatrix mat) {
			if(this.colum != mat.row) throw new ArithmeticException();
			IntMatrix ans = new IntMatrix(this.row, mat.colum);
			for(int i = 0; i < ans.row; i++){
				for(int j = 0; j < ans.colum; j++){
					int sum = 0;
					for(int k = 0; k < this.colum; k++){
						sum = (int)((sum + ((long)get(i, k) * mat.get(k, j) % M)) % M);
					}
					ans.set(i, j, sum);
				}
			}
			return ans;
		}

		public IntVector mul(IntVector vec) {
			if(this.colum != vec.size) throw new ArithmeticException();
			IntVector ans = new IntVector(this.row);
			for(int i = 0; i < ans.size; i++){
				int sum = 0;
				for(int j = 0; j < this.colum; j++){
					sum = (int)((sum + ((long)get(i, j) * vec.get(j) % M)) % M);
				}
				ans.set(i, sum);
			}
			return ans;
		}

		public IntMatrix pow(int n) {
			if(this.colum != this.row) throw new ArithmeticException();
			if(n == 0) return identity(this.row);
			if(n % 2 == 0) return (this.mul(this)).pow(n / 2);
			else return this.mul(this.pow(n - 1));
		}

		public String toString() {
			StringBuilder build = new StringBuilder();
			build.append("[");
			for(int i = 0; i < row; i++){
				build.append("[");
				for(int j = 0; j < colum; j++){
					if(j != 0) build.append(",");
					build.append(get(i, j));
				}
				build.append("]");
				if(i != row - 1) build.append(",");
				build.append("\n");
			}
			return build.toString();
		}
	}

	class IntVector {
		public final int size;
		private int[] data;

		public IntVector(int size) {
			this.size = size;
			data = new int[size];
		}

		public void set(int i, int v) {
			this.data[i] = v;
		}

		public int get(int i) {
			return this.data[i];
		}
	}

	private static PrintWriter out;

	public static void main(String[] args) {
		out = new PrintWriter(System.out);
		new Main().solve();
		out.flush();
	}

	public static int nextInt() {
		int num = 0;
		String str = next();
		boolean minus = false;
		int i = 0;
		if(str.charAt(0) == '-'){
			minus = true;
			i++;
		}
		int len = str.length();
		for(; i < len; i++){
			char c = str.charAt(i);
			if(!('0' <= c && c <= '9')) throw new RuntimeException();
			num = num * 10 + (c - '0');
		}
		return minus ? -num : num;
	}

	public static long nextLong() {
		long num = 0;
		String str = next();
		boolean minus = false;
		int i = 0;
		if(str.charAt(0) == '-'){
			minus = true;
			i++;
		}
		int len = str.length();
		for(; i < len; i++){
			char c = str.charAt(i);
			if(!('0' <= c && c <= '9')) throw new RuntimeException();
			num = num * 10l + (c - '0');
		}
		return minus ? -num : num;
	}

	public static String next() {
		int c;
		while(!isAlNum(c = read())){}
		StringBuilder build = new StringBuilder();
		build.append((char)c);
		while(isAlNum(c = read())){
			build.append((char)c);
		}
		return build.toString();
	}

	private static byte[] inputBuffer = new byte[1024];
	private static int bufferLength = 0;
	private static int bufferIndex = 0;

	private static int read() {
		if(bufferLength < 0) throw new RuntimeException();
		if(bufferIndex >= bufferLength){
			try{
				bufferLength = System.in.read(inputBuffer);
				bufferIndex = 0;
			}catch(IOException e){
				throw new RuntimeException(e);
			}
			if(bufferLength <= 0) return(bufferLength = -1);
		}
		return inputBuffer[bufferIndex++];
	}

	private static boolean isAlNum(int c) {
		return '!' <= c && c <= '~';
	}
}