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(|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.
The problem is to calculate the state of the n,m-automaton after k d-steps.
Input
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 < n⁄2 , 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
题意: 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] [1] [0 1 2 3 4]
[1 1 1 0 0] [1]第二个矩阵[4 0 1 2 3]
[0 1 1 1 0] * [0] 下标表示 [3 4 0 1 2]
[0 0 1 1 1] [0] = = = = [2 3 4 0 1]
[1 0 0 1 1] [1] [1 2 3 4 0]
for(int i = 0; i < n; ++i)
{
for(int j = 0; j < n; ++j)
c[i] += A[j]*B[ i<=j ? (j-i) : (n-i+j)];
}
在网上看到别人的题解是: c[i] += A[j]*B[ i>=j ? (i-j) : (n+i-j)];
代码:
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
#define MAX 501
int n, m, d, K;
long long a[MAX], temp[MAX];
int num;
inline void multiply(long long *A, long long *B, int mod)
{
long long c[MAX];
for(int i = 0; i < n; ++i)
{
c[i] = 0;
for(int j = 0; j < n; ++j)
c[i] += A[j]*B[ i<=j ? (j-i) : (n-i+j)];
}
for(int i = 0; i < n; ++i)
A[i] = c[i]%mod;
}
void pow(long long *A, int n, int mod)
{
while(n)
{
if(n%2 != 0)
multiply(a,temp,mod);
multiply(temp,temp,mod);
n /= 2;
}
}
int main()
{
//freopen("input.txt","r",stdin);
while(scanf("%d %d %d %d",&n, &m, &d, &K) != EOF)
{
memset(temp,0,sizeof(temp));
for(int i = 0; i < n; ++i)
scanf("%lld",&a[i]);
temp[0] = 1;
for(int i = 1; i <= d; ++i)
temp[i] = temp[n-i] = 1;
pow(temp,K,m);
for(int i = 0; i < n; ++i)
{
if(i == 0) printf("%lld",a[i]);
else printf(" %lld",a[i]);
}
printf("\n");
}
return 0;
}