UVA Live 3704 Cellular Automaton (循环矩阵+快速幂)
来源:互联网 发布:vue.js实战 pdf 编辑:程序博客网 时间:2024/05/17 22:50
Description
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 denvironment 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 input file contains several test cases, each of them consists of two lines, as described below.
The first line of the input contains four integer numbers n, m, d, and k (1 ≤ n ≤ 500, 1 ≤ m ≤ 1000000, 0 ≤ d < n 2 , 1 ≤ k ≤ 10000000).
The second line contains n integer numbers from 0 to m−1 — initial values of the automaton’s cells.
Output
For each test case, write to the output, on a line by itself, the values of the n,m-automaton’s cells after k d-steps.
Sample Input
5 3 1 11 2 2 1 25 3 1 101 2 2 1 2
Sample Output
2 2 2 2 12 0 0 2 2
题意
一个细胞自动机包含 n
个格子,每个格子的取值为 (0,m)
,给定距离 d
,每次操作后每个格子的值变为与它的距离不超过 d
的所有格子在操作前的值之和模 m
,计算 k
次操作后各格子的值。
思路
我们把经过 k
次操作后各格子的值看作列向量 m
。
于是整个变换可以通过矩阵乘法来描述
设
最终答案:
其中
注意这一个矩阵是循环矩阵,而循环矩阵之间的乘积仍然是循环矩阵,于是我们可以只存储矩阵的一行,计算的时候也只计算这一行即可,时间复杂度
AC 代码
#include<bits/stdc++.h>using namespace std;typedef long long LL;LL n,m,d,k;LL np[500][500];int idx=0;struct marx{ int tot; marx() { tot=idx++; } marx mult(const marx&a,const marx&b) { marx ans = marx(); for(int i=0; i<n; i++) { LL num=0; for(int j=0; j<n; j++) { num=(num+(np[a.tot][(j-i+n)%n]*np[b.tot][j])%m)%m; } np[ans.tot][i]=num; } return ans; } marx mul(const marx&a,const marx&b) { marx ans=marx(); for(int i=0; i<n; i++) for(int j=0; j<n; j++) np[ans.tot][i]=(np[ans.tot][i]+(np[a.tot][j]*np[b.tot][(j-i+n)%n])%m)%m; return ans; }};marx mult(marx a,LL n){ marx ans=marx(); np[ans.tot][0]=1; while(n) { if(n&1)ans=ans.mul(ans,a); a=a.mul(a,a); n>>=1; } return ans;}int main(){ ios::sync_with_stdio(false); while(cin>>n>>m>>d>>k) { memset(np,0,sizeof(np)); idx=0; marx ans = marx(); for(int i=0; i<n; i++) cin>>np[ans.tot][i]; marx ap = marx(); for(int j=-d; j<=+d; j++) np[ap.tot][(j+n)%n]=1; ans=ans.mult(mult(ap,k),ans); for(int i=0; i<n; i++) cout<<np[ans.tot][i]<<((i!=n-1)?" ":"\n"); } return 0;}
- UVA Live 3704 Cellular Automaton (循环矩阵+快速幂)
- uva 1386 - Cellular Automaton(循环矩阵+矩阵快速幂)
- UVA 1386 cellular automaton [循环矩阵+矩阵快速幂]【数学】
- 【循环矩阵+矩阵快速幂】Cellular Automaton UVA
- UVA 1386 Cellular Automaton(循环矩阵)
- LA 3704 Cellular Automaton【循环矩阵,快速幂】
- UVA - 1386 Cellular Automaton (矩阵快速幂)
- LA 3704 Cellular Automaton / 矩阵快速幂
- LA 3704 Cellular Automaton (矩阵快速幂)
- poj3150 Cellular Automaton(矩阵快速幂)
- UvaLive 3704 Cellular Automaton (矩阵快速幂)
- Poj 3150/UVA 1386/UVALive 3704 Cellular Automaton 循环矩阵
- uva 1386 - Cellular Automaton(循环矩阵乘)
- UVA 1386 - Cellular Automaton(循环矩阵)
- LA 3704 Cellular Automaton(循环矩阵)
- UVA1386-Cellular Automaton(循环矩阵)
- poj 3150 Cellular Automaton(矩阵快速幂)
- POJ 3150 Cellular Automaton(矩阵快速幂)
- 树的创建及点击事件Java
- 霍夫曼编码
- Pentaho BI源码编译
- 数据结构实验之排序七:选课名单
- SDUT 2778 小明的花费预算
- UVA Live 3704 Cellular Automaton (循环矩阵+快速幂)
- 在Spark集群中,集群的节点个数、RDD分区个数、cpu内核个数三者与并行度的关系??
- Python 爬虫学习系列教程
- [bigdata-091] 规则引擎 easyrules 开发
- Message Flood
- 1028. 人口普查(20)
- 算法思想——单调队列(Foj志愿者问题)
- 合并区间
- 解决Python:'utf-8' codec can't decode byte 0xfc in position 14: invalid start byte