2013
11-12

Cellular Automaton

A 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 order n 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(|ij|, n − |ij|). 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.

The problem is to calculate the state of the n,m-automaton after k d-steps.

The first line of the input file contains four integer numbers n, m, d, 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 the values of the n,m-automaton’s cells after k d-steps.

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 #1
2 2 2 2 1

sample output #2
2 0 0 2 2

import java.util.Scanner;

public class Main{

static int n,m,d,k;

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);

while(scan.hasNext()){
n = scan.nextInt();
m = scan.nextInt();
d = scan.nextInt();
k = scan.nextInt();

long[] init = new long[n];
long[] tmp = new long[n];

for(int i=0;i< n;i++)
init[i] = scan.nextInt();
tmp[0] = 1;
for(int i=1;i<=d;i++)
tmp[i] = tmp[n-i] = 1;

while(k!=0){
if(k%2==1)
mul(tmp,init);
mul(tmp,tmp);
k >>= 1;
}

for(int i=0;i< n;i++)
System.out.print(init[i]+" ");
System.out.println();

}

}

public static void mul(long[] tmp, long[] init) {

long c[] = new long[n];

for(int i=0;i< n;i++){
for(int j=0;j< n;j++)
//由矩阵第一行 可以通过 平衡获得所有列
c[i] += tmp[j]*init[(i+j)%n];
}

for(int i=0;i< n;i++)
init[i] = c[i]%m;

}

}

1. 约瑟夫也用说这么长……很成熟的一个问题了，分治的方法解起来o(n)就可以了，有兴趣可以看看具体数学的第一章，关于约瑟夫问题推导出了一系列的结论，很漂亮

2. 这个方法是错的，不信你试试：
20 5
1 A:9
1 A:9
1 A:9
1 A:6
1 A:4
正确答案应该是19，这个答案是18