Cellular Automaton (矩阵乘法)

Problem Description

cellular automaton is a collection of cells on a grid of specified shape that evolves through a number of discrete time steps according to a set of rules that describe the new state of a cell based on the states of neighboring cells. The order of the cellular automaton is the number of cells it contains. Cells of the automaton of ordern are numbered from 1 to n.

The order of the cell is the number of different values it may contain. Usually, values of a cell of order m are considered to be integer numbers from 0 to m − 1.

One of the most fundamental properties of a cellular automaton is the type of grid on which it is computed. In this problem we examine the special kind of cellular automaton — circular cellular automaton of order n with cells of order m. We will denote such kind of cellular automaton as n,m-automaton.

A distance between cells i and j in n,m-automaton is defined as min(|i − j|, n − |i − j|). A d-environment of a cell is the set of cells at a distance not greater than d.

On each d-step values of all cells are simultaneously replaced by new values. The new value of cell i after d-step is computed as a sum of values of cells belonging to the d-enviroment of the cell i modulo m.

The following picture shows 1-step of the 5,3-automaton.

 

Input

The first line of the input file contains four integer numbers nmd, and k (1 ≤ n ≤ 500, 1 ≤ m ≤ 1 000 000, 0 ≤ d < n2 , 1 ≤ k ≤ 10 000 000). The second line contains n integer numbers from 0 to m − 1 — initial values of the automaton’s cells.

 

Output

Output the values of the n,m-automaton’s cells after k d-steps.

 

Sample Input
sample input #1 5 3 1 1 1 2 2 1 2 sample input #2 5 3 1 10 1 2 2 1 2
 

Sample Output
sample output #1 2 2 2 2 1 sample output #2 2 0 0 2 2
 **************************************************************************************************************************
 1 /*
 2 矩阵乘法
 3 原理:
 4 它的原理就是
 5 a0 a1 a2 a3 a4
 6 ->
 7 (a4+a0+a1) (a0+a1+a2) (a1+a2+a3) (a2+a3+a4) (a3+a4+a0)
 8 */
 9 #include<iostream>
10 #include<cstdio>
11 using namespace std;
12 int n,m,d,k;
13 void mul(long long a[],long long b[])
14 {
15       int i,j;
16       long long c[501];
17       for(i=0;i<n;++i)for(c[i]=j=0;j<n;++j)c[i]+=a[j]*b[i>=j?(i-j):(n+i-j)];
18       for(i=0;i<n;b[i]=c[i++]%m);
19 }
20 long long init[501],tmp[501];
21 int main()
22 {
23     int i,j;
24     scanf("%d%d%d%d",&n,&m,&d,&k);
25     for(i=0;i<n;++i)scanf("%I64d",&init[i]);
26     for(tmp[0]=i=1;i<=d;++i)tmp[i]=tmp[n-i]=1;//n*n的矩阵初始化
27     while(k)//二分求解
28     {
29             if(k&1)mul(tmp,init);
30             mul(tmp,tmp);
31             k>>=1;
32     }
33     for(i=0;i<n;++i)if(i)printf(" %I64d",init[i]);else printf("%I64d",init[i]);
34     printf("
");
35     return 0;
36 }
View Code
原文地址:https://www.cnblogs.com/sdau--codeants/p/3536195.html