POJ 3150 Cellular Automaton
来源:互联网 发布:mac系统画流程图 编辑:程序博客网 时间:2024/05/22 07:56
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 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 #15 3 1 11 2 2 1 2sample input #25 3 1 101 2 2 1 2
Sample Output
sample output #12 2 2 2 1sample output #22 0 0 2 2
将初始值看做一个1*n的矩阵s,分析题意,容易得到ans=s* A^k;
(A是根据题意构造的矩阵)
时间复杂度O(N^3),显然爆炸。
仔细观察发现A是一个循环矩(方)阵。
循环方阵A满足:A[x][y]=A[1][(y-x+n)%n+1];
我们只计算和存储A的第一行,时空复杂度下降到O(n^2)可以通过。
代码如下:
#include<cstdio>#include<iostream>#include<cstdlib>#include<cmath>#include<cstring>#include<queue>#include<vector>#include<algorithm>#define LL long long#define CLEAR(xxx) memset(xxx,0,sizeof(xxx))using namespace std;const LL maxn=500+5,inf=1e9;LL n,m,d,k,s[maxn];LL A[maxn],C[maxn],ans[maxn],z[maxn];void multi(LL *A,LL *B){CLEAR(C);for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)(C[i]+=A[j]*B[(i-j+n)%n+1])%=m;memcpy(A,C,sizeof(C));} void Pow(int x){int i,k,j;CLEAR(ans); ans[1]=1;for(;x;x>>=1,multi(A,A))if(x&1)multi(ans,A);}void solve(){int i,j;CLEAR(A);CLEAR(s);for(i=1;i<=n;i++)scanf("%d",&s[i]);for(i=1;i<=d+1;i++)A[i]=1;for(i=n-d+1;i<=n;i++)A[i]=1;Pow(k);multi(s,ans);for(i=1;i<=n;i++)printf("%d ",C[i]);putchar('\n'); }int main(){while(cin>>n>>m>>d>>k)solve();return 0;}
<pre name="code" class="cpp">#include<cstdio>#include<iostream>#include<algorithm>#include<cstring>#include<cstdlib>#define mod m#define clear(xxx) memset(xxx,0,sizeof(xxx))using namespace std;const long long maxn=505;typedef long long arr[maxn];arr a,b,ans,z;long long n,m,d,k;void multiply(arr x,arr y){ memset(z,0,sizeof(z)); for(long long i=1;i<=n;i++){ for(long long h=1;h<=n;h++){ z[i]=(z[i]+x[h]*y[(i-h+n)%n+1])%mod; } } memcpy(x,z,sizeof(z)); } void mont(long long t){ memset(ans,0,sizeof(ans)); ans[1]=1; while(t>0){ if(t&1)multiply(ans,b); t>>=1; multiply(b,b); } } int main(){while(scanf("%I64d%I64d%I64d%I64d",&n,&m,&d,&k)!=EOF){clear(a);clear(b);clear(ans);clear(z);long long i,j;for(i=1;i<=n;i++){scanf("%I64d",&a[i]);}for(i=1;i<=d+1;i++)b[i]=1;for(i=n;i>=n-d+1;i--)b[i]=1;mont(k);multiply(a,ans);for(i=1;i<=n;i++){printf("%I64d ",z[i]);}printf("\n");}}
- poj 3150 Cellular Automaton
- POJ 3150 Cellular Automaton
- poj 3150 Cellular Automaton
- POJ 3150 Cellular Automaton
- poj 3150 Cellular Automaton
- POJ 3150 Cellular Automaton
- POJ 3150 Cellular Automaton
- poj 3150 Cellular Automaton
- 【poj 3150】Cellular Automaton 矩阵
- POJ 3150 Cellular Automaton 笔记
- POJ 3150 Cellular Automaton 矩阵dp
- POJ 3150 Cellular Automaton(矩阵快速幂)
- poj 3150Cellular Automaton(矩阵快速幂)
- POJ Cellular Automaton
- Poj 3150/UVA 1386/UVALive 3704 Cellular Automaton 循环矩阵
- poj 3150 Cellular Automaton(矩阵快速幂)
- POJ 3150 Cellular Automaton --矩阵快速幂及优化
- POJ 3150 Cellular Automaton(矩阵快速幂)
- L2-003. 月饼-PAT团体程序设计天梯赛GPLT
- NYOJ118 修路方案 次小生成树
- LeetCode-JAVA-Valid Parentheses
- LeetCode-83:Remove Duplicates from Sorted List
- Spring事务管理(5)-开启事务
- POJ 3150 Cellular Automaton
- 跟我一起玩嵌入式开发-硬件抽象与汇编基础-1
- js中的history和location对象及节点
- markdown使用
- Java并发修改错误ConcurrentModificationException分析
- mysql 表名、字段名大小写
- JVM对象的内存布局和访问
- Haar小波变换
- 多线程---抢购