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. js错误处理和调试

    1.重点关注函数和可能导致函数执行失败的因素. 2.抛出错误和使用try-catch的时机:一般来说在应用程序架构较低的层次中经常会抛出错误.这个层次并不会影响当前执行的代码. 错误也得不到真正的处理 ...

  2. hive 提取用户第一次浏览/购买 某商品的 时间

    Hive虽然强大,但是我们遇到的业务需求必定是千奇百怪的. 在做日志解析的时候,我们会遇到访客,和访次的概念. 对于在2个小时之内的访问,我们认为是访问一次,对于两个小时之外的访问,我们认定是再次访问 ...

  3. eclipse中使用weblogic作为服务器控制台中文乱码

    使用WebLogic时控制台输出中文乱码解决方法:   1.找到weblogic安装目录,当前项目配置的domain   2.找到bin下的setDomainEnv.cmd文件   3.打开文件,从文 ...

  4. Discuz!X3.2修改用户名注册长度限制的方法

    Discuz!X3.2用户名注册长度限制为15个字符,有些站长朋友觉得太短,需要改的长一点,但是很多人都不知道怎么修改,下面就告诉大家如何修改这个限制.按照以下步骤来就可以了! 1.打开 ) {    ...

  5. 记centos 安装 mysql5.7.23.tar.gz教程

    1.下载tar包,这里使用wget从官网下载 wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.22-linux-glibc2. ...

  6. 微信小程序 js逻辑

    }) 页面 Page() 函数用来注册一个页面.接受一个 object 参数,其指定页面的初始数据.生命周期函数.事件处理函数等. data 页面的初始数据,data 将会以 JSON 的形式由逻辑层 ...

  7. 【转】Python 第三方 http 库-Requests 学习

    原文地址:http://www.itwhy.org/%E8%BD%AF%E4%BB%B6%E5%B7%A5%E7%A8%8B/python/python-%E7%AC%AC%E4%B8%89%E6%9 ...

  8. C++ 类的多态一(virtual关键字--构造函数深刻理解)

    //virtual关键字--构造函数深刻理解 #include<iostream> using namespace std; /* C语言编译器,c++编译器全部是静态链编,就是一段一段代 ...

  9. Web安全测试(一)-手工安全测试方法&修改建议

    常见问题 1.XSS(CrossSite Script)跨站脚本攻击 XSS(CrossSite Script)跨站脚本攻击.它指的是恶意攻击者往Web 页面里插入恶意 html代码,当用户浏览该页之 ...

  10. [Django] 问题记录追踪表

    关注的网站: https://simpleisbetterthancomplex.com/ Linux部署 1. Ubuntu下vsftpd安装部署 2. Utuntu下Django+Apache+W ...