Cellular Automaton

Time Limit: 12000MS Memory Limit: 65536K

Total Submissions: 3504 Accepted: 1421

Case Time Limit: 2000MS

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

这道题目的矩阵好找,但是由于n比较大,用n*n的矩阵再加上快速幂,是O(n^3*log k) 回超时。观察矩阵,发现矩阵是一个循环矩阵,无论矩阵取多少次方,矩阵的每一行相当于第一行向后推了一步,所以说是循环矩阵,这样我们只要计算矩阵的第一行就可以知道矩阵的其他行,所以只开一维数组效率就是O(n^2log k)

#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
#include <stdio.h> using namespace std;
typedef long long int LL;
int n,m,d,k;
struct Node
{
LL a[505];
};
Node multiply(Node a,Node b)
{
Node c;
memset(c.a,0,sizeof(c.a));
for(int i=0;i<n;i++)
{
int cnt=(n-i)%n;
for(int j=0;j<n;j++)
{
(c.a[i]+=(a.a[j]*b.a[cnt++])%m)%=m;
if(cnt==n) cnt=0;
}
}
return c;
}
Node get(Node a,int x)
{
Node c;
memset(c.a,0,sizeof(c.a));
c.a[0]=1;
for(x;x;x>>=1)
{
if(x&1) c=multiply(c,a);
a=multiply(a,a);
}
return c;
}
int main()
{
scanf("%d%d%d%d",&n,&m,&d,&k);
Node a;Node b;
memset(a.a,0,sizeof(a.a));
memset(b.a,0,sizeof(b.a));
for(int i=0;i<n;i++)
scanf("%lld",&b.a[i]);
a.a[0]=1;
for(int i=1;i<=d;i++)
a.a[i]=a.a[n-i]=1;
a=get(a,k);
a=multiply(b,a);
for(int i=0;i<n;i++)
if(i==n-1) printf("%lld\n",a.a[i]);
else printf("%lld ",a.a[i]);
return 0;
}

POJ 3150 Cellular Automaton(矩阵快速幂)的更多相关文章

  1. POJ 3150 Cellular Automaton --矩阵快速幂及优化

    题意:给一个环,环上有n块,每块有个值,每一次操作是对每个点,他的值变为原来与他距离不超过d的位置的和,问k(10^7)次操作后每块的值. 解法:一看就要化为矩阵来做,矩阵很好建立,大白书P157页有 ...

  2. [POJ 3150] Cellular Automaton (矩阵高速幂 + 矩阵乘法优化)

    Cellular Automaton Time Limit: 12000MS   Memory Limit: 65536K Total Submissions: 3048   Accepted: 12 ...

  3. POJ 3150 Cellular Automaton(矩阵高速幂)

    题目大意:给定n(1<=n<=500)个数字和一个数字m,这n个数字组成一个环(a0,a1.....an-1).假设对ai进行一次d-step操作,那么ai的值变为与ai的距离小于d的全部 ...

  4. poj 3070 && nyoj 148 矩阵快速幂

    poj 3070 && nyoj 148 矩阵快速幂 题目链接 poj: http://poj.org/problem?id=3070 nyoj: http://acm.nyist.n ...

  5. poj 3070 Fibonacci(矩阵快速幂,简单)

    题目 还是一道基础的矩阵快速幂. 具体的居者的幂公式我就不明示了. #include<stdio.h> #include<string.h> #include<algor ...

  6. POJ 3070 Fibonacci(矩阵快速幂)

    题目链接 题意 : 用矩阵相乘求斐波那契数的后四位. 思路 :基本上纯矩阵快速幂. #include <iostream> #include <cstring> #includ ...

  7. poj 2778 AC自动机+矩阵快速幂

    题目链接:https://vjudge.net/problem/POJ-2778 题意:输入n和m表示n个病毒,和一个长为m的字符串,里面只可以有'A','C','G','T' 这四个字符,现在问这个 ...

  8. Scout YYF I POJ - 3744(概率dp + 矩阵快速幂)

    题意: 一条路上有n个地雷,你从1开始走,单位时间内有p的概率走一步,1-p的概率走两步,问安全通过这条路的概率 解析: 很容易想到 dp[i] = p * dp[i-1] + (1 - p) * d ...

  9. POJ 3070 Fibonacci 【矩阵快速幂】

    <题目链接> Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 ...

  10. POJ 3734 Blocks (矩阵快速幂)

    题目链接 Description Panda has received an assignment of painting a line of blocks. Since Panda is such ...

随机推荐

  1. (转)解决WinDbg调试Dump文件不同环境mscordacwks.dll版本问题

    解决WinDbg调试Dump文件不同环境mscordacwks.dll版本问题   开发人员提交一个dump文件(Windows Server 2008 R2),当前调试环境Windows Serve ...

  2. 纯div+css制作的弹出菜单

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. Powershell 获取文件版本信息

    获取文件版本信息,通过FileVersionInfo::GetVersioninfo(file) 来获取信息 function Check-DdpstoreFileVersion{ $Ddpstore ...

  4. Windows Azure Platform 性能监视器(转载)

    Windows操作系统提供了查看性能监视器的功能,用于监视CPU使用率.内存使用率,硬盘读写速度,网络速度等.您可以在开始-->运行-->输入Perfmon,就可以打开性能监视器. 我们知 ...

  5. spring和hibernate整合,事务管理

    一.spring和hibernate整合开发步骤 1 引入jar文件,用户libarary列表如下 //spring_core spring3..9core\commons-logging-1.2.j ...

  6. CentOS下yum常用命令

    1.自动搜索最快镜像插件:yum install yum-fastestmirror 2.更换163的源. 首先:备份/etc/yum.repos.d/CentOS-Base.repomv /etc/ ...

  7. FPGA学习(第8节)-Verilog设计电路的时序要点及时序仿真

    一个电路能跑到多少M的时钟呢? 这和电路的设计有密切联系(组合逻辑的延时),我们知道电路器件都是由一定延迟的,所以信号的仿真很重要.如果延迟时间大于时钟,就会导致时序违例,出现逻辑错误. 项目要求30 ...

  8. linux上定时备份mysql数据库

    定时备份数据库 /usr/sbin/backupmysql timestamp=`date +"%Y-%m-%d-%H-%M-%S"` mysqldump -uroot -p'12 ...

  9. Windows 内核(WRK)简介

    引子 WRK 是微软于 2006 年针对教育和学术界开放的 Windows 内核的部分源码,WRK(Windows Research Kernel)也就是 Windows 研究内核,在 WRK 中不仅 ...

  10. 最大子矩阵 hdu1081

    To The Max Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...