Algoogle

Algorithm for Programming Contest

AOJ 1327 One-Dimensional Cellular Automaton

Category: AOJ Tag: matrix

One-Dimensional Cellular Automaton

問題概要


解法


遷移行列を作って右から同じ物をひたすら掛けていく.
遷移行列はN=3の場合

のように決まる.

コード


(1327.cpp) download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <algorithm>
#include <iostream>
#include <vector>
#include <cmath>
#define rep(i,a) for(int i = 0;i < (a); i++)
#define INF 1e9
using namespace std;
typedef int number;
typedef number* array;
typedef array* matrix;

int N, M, A, B, C, T;
matrix new_matrix(int n, int m){
    matrix A = new array[n];
    for(int i = 0; i < n; i++){
        A[i] = new number[m];
        for(int j = 0; j < m; j++) A[i][j] = 0;
    }
    return A;
}

void delete_matrix(matrix A, int n, int m){
    for(int i = 0; i < n; i++) delete[] A[i];
    delete[] A;
}

matrix identity(int n){
    matrix A = new_matrix(n,n);
    for(int i = 0; i < n; i++) A[i][i] = 1;
    return A;
}

matrix mul_matrix(matrix A, matrix B, int p, int n, int q){
    matrix C = new_matrix(p,q);
    for(int i = 0; i < p; i++){
        for(int j = 0; j < q; j++){
            C[i][j] = 0;
            for(int k = 0; k < n; k++){
                C[i][j] += A[i][k]*B[k][j];
                C[i][j] %= M;
            }
        }
    }
    return C;
}

matrix pow_matrix(matrix A, int n, int e){
    return e==0? identity(n):
        e%2==0? pow_matrix(mul_matrix(A,A,n,n,n),n,e/2):
        mul_matrix(A,pow_matrix(A,n,e-1),n,n,n);
}

int main(){

    while(cin>>N>>M>>A>>B>>C>>T,N||M||A||B||C||T){
        matrix s = new_matrix(1,N);
        rep(i,N) cin >> s[0][i];
        matrix t = new_matrix(N,N);
        rep(i,N){
            if(i > 0) t[i][i-1] = C;
            t[i][i] = B;
            if(i<N-1) t[i][i+1] = A;
        }
        s = mul_matrix(s,pow_matrix(t,N,T),1,N,N);
        rep(i,N) cout << s[0][i] << (i==N-1? '\n': ' ');
    }
}

Comments