ACM: 矩阵变形题 数论题 poj 3150
来源:互联网 发布:提花组织软件 编辑:程序博客网 时间:2024/06/06 00:22
A cellular automatonis a collection of cells on a grid of specified shape that evolvesthrough a number of discrete time steps according to a set of rulesthat describe the new state of a cell based on the states ofneighboring cells. The order of the cellular automaton isthe 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 valuesit may contain. Usually, values of a cell of order m areconsidered to be integer numbers from 0 to m − 1.
One of the most fundamental properties of a cellular automatonis the type of grid on which it is computed. In this problem weexamine the special kind of cellular automaton — circular cellularautomaton of order n with cells of order m. We willdenote such kind of cellular automaton as n,m-automaton.
A distance between cells i and j inn,m-automaton is defined as min(|i −j|, n − |i − j|). A d-environment ofa cell is the set of cells at a distance not greater thand.
On each d-step values of all cells are simultaneouslyreplaced by new values. The new value of cell i afterd-step is computed as a sum of values of cells belonging tothe 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 then,m-automaton after k d-steps.
Input
The first line of the inputfile contains four integer numbers n, m, d,and k (1 ≤ n ≤ 500, 1 ≤ m ≤ 1 000 000, 0 ≤d < n⁄2 , 1 ≤k ≤ 10 000 000). The second line contains n integer numbersfrom 0 to m − 1 — initial values of the automaton’scells.
Output
Output the values of then,m-automaton’s cells after kd-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
题意: Cellular Automaton收集细胞, 现在有n个grid, 每个grid上的值是0~m-1,
d-steps: 每一次收集当前位置相邻d距离的细胞, 进行d-steps步骤k次.
求最后每个grid的结果模m
解题思路:
1. 这题有点怪, 为什么样例输入输出要加"sample input/output #1"这些,
而输入输出的时候却没有.
2. 其实题意很明白, 每次操作就是把当前位置的值加上左右下标距离不超过d的值.
问题分析:
第一个样例 [1 2 2 1 2]进行一次d-steps操作:
[(a4+a0+a1) (a0+a1+a2) (a1+a2+a3) (a2+a3+a4) (a3+a4+a0)]
显然, 我们可以构建一个矩阵来表示一次操作.
[1 1 0 0 1]
[1 1 1 0 0]
[0 1 1 1 0]
[0 0 1 1 1]
[1 0 0 1 1]
思路出来了: 设初始矩阵A, 操作矩阵B. 求: A*B^k;
B^k可以用二分方法解决.
3. 问题再出现了: 1 ≤ n ≤ 500 给的范围有点大. 再观察发现
B^1 = B^2 = B^3 =
[1 1 0 0 1] [3 2 1 1 2] [7 6 4 4 6]
[1 1 1 0 0] [2 3 2 1 1] [6 7 6 4 4]
[0 1 1 1 0] [1 2 3 2 1] [4 6 7 6 4]
[0 0 1 1 1] [1 1 2 3 2] [4 4 6 7 6]
[1 0 0 1 1] [2 1 1 2 3] [6 4 4 6 7]
每一行的操作是往下滚动一位.即: B[i][j] = B[i-1][j-1];
这样就可以只需开一维数组即可.一个简单的例子即可: B*B
[1 1 0 0 1]