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 nmd, and k (1 ≤ n ≤ 500, 1 ≤ m ≤ 1 000 000, 0 ≤ d < n2 , 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的环,给出M,K,D,以及N个数。我们进行K次操作,每次操作把距离当前点不超过D的累加到当前点,结果模M。

思路:因为要进行K次,每次的原则是一样的,我们可以想到用矩阵来优化,如果i能到达j,把么base[i][j]=1;则结果ans=A*(base^K)。

但是需要优化,时间复杂度为O(N^3*lgK)。我们发现矩阵是下一行由上一行右移一位而来,那么我们保存一维即可代表这个矩阵。同样的,我们只需要得到第一行的矩阵结果,就能得到整个矩阵的结果。

#include<bits/stdc++.h>
#define ll long long
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int maxn=;
int N,Mod,D,K;
struct mat
{
int M[maxn];
mat(){ rep(i,,N) M[i]=; }
mat friend operator*(mat a,mat b){
mat res;
rep(k,,N)
rep(j,,N){
res.M[j]=(res.M[j]+(ll)a.M[k]*b.M[j-k+>?j-k+:j-k++N]%Mod)%Mod;
}
return res;
}
mat friend operator ^(mat a,int x)
{
mat res;rep(i,,N) res.M[]=;
while(x){
if(x&) res=res*a; a=a*a; x/=;
} return res;
} };
int main()
{
scanf("%d%d%d%d",&N,&Mod,&D,&K);
mat a,base;
rep(i,,N) scanf("%d",&a.M[i]);
rep(i,,N)
if(i-<=D||N-i+<=D||N-+i<=D) base.M[i]=;
a=a*(base^K);
rep(i,,N-) printf("%d ",a.M[i]);
printf("%d\n",a.M[N]);
return ;
}

POJ - 3150 :Cellular Automaton(特殊的矩阵,降维优化)的更多相关文章

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

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

  2. POJ 3150 Cellular Automaton(矩阵快速幂)

    Cellular Automaton Time Limit: 12000MS Memory Limit: 65536K Total Submissions: 3504 Accepted: 1421 C ...

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

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

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

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

  5. POJ 3150 Cellular Automaton(矩阵乘法+二分)

    题目链接 题意 : 给出n个数形成环形,一次转化就是将每一个数前后的d个数字的和对m取余,然后作为这个数,问进行k次转化后,数组变成什么. 思路 :下述来自here 首先来看一下Sample里的第一组 ...

  6. poj 3150 Cellular Automaton

    首先来看一下Sample里的第一组数据.1 2 2 1 2经过一次变换之后就成了5 5 5 5 4它的原理就是a0 a1 a2 a3 a4->(a4+a0+a1) (a0+a1+a2) (a1+ ...

  7. 【POJ】3150 Cellular Automaton(矩阵乘法+特殊的技巧)

    http://poj.org/problem?id=3150 这题裸的矩阵很容易看出,假设d=1,n=5那么矩阵是这样的 1 1 0 0 1 1 1 1 0 0 0 1 1 1 0 0 0 1 1 1 ...

  8. POJ3150—Cellular Automaton(循环矩阵)

    题目链接:http://poj.org/problem?id=3150 题目意思:有n个数围成一个环,现在有一种变换,将所有距离第i(1<=i<=n)个数小于等于d的数加起来,对m取余,现 ...

  9. UVA 1386 - Cellular Automaton(循环矩阵)

    UVA 1386 - Cellular Automaton option=com_onlinejudge&Itemid=8&page=show_problem&category ...

随机推荐

  1. MySQL数据库(6)_用户操作与权限管理、视图、存储过程、触发器、基本函数

    用户操作与权限管理 MySQL用户操作 创建用户 方法一: CREATE USER语句创建 CREATE USER "用户名"@"IP地址" IDENTIFIE ...

  2. 【HackerRank】Closest Numbers

    Sorting is often useful as the first step in many different tasks. The most common task is to make f ...

  3. 通过调节坐标进行jfree图的放大缩小

    http://blog.csdn.net/lt1983lt/article/details/5665085 import Java.awt.BorderLayout;import java.awt.C ...

  4. 使用ASP.Net MVC5 Web API OData和Sencha Touch 开发WebAPP

    使用ASP.Net MVC5 Web API OData和SenCha Touch 开发WebAPP Demo 效果 第一步 创建数据库 创建表 第二步 搭建MVC,并导入OData 第三步,写入We ...

  5. 爬虫实例之使用requests和Beautifusoup爬取糗百热门用户信息

    这次主要用requests库和Beautifusoup库来实现对糗百的热门帖子的用户信息的收集,由于糗百的反爬虫不是很严格,也不需要先登录才能获取数据,所以较简单. 思路,先请求首页的热门帖子获得用户 ...

  6. linux内核调试+qemu+eclipse【转】

    本文转载自:https://blog.csdn.net/WANG__RONGWEI/article/details/54922727 一.调试环境: 在ubuntu16.04下,在虚拟机里边运行的ub ...

  7. hql学习记录

    ` String hql = "from SysUser o join o.set where owner_id = :newName"; Query query = this.g ...

  8. vi编辑器快捷键使用详解

    文本编辑器是所有计算机系统中最常用的一种工具.UNIX下的编辑器有ex,sed和vi等,其中,使用最为广泛的是vi,而vi命令繁多,论坛里好像这方面的总结不多,以下稍做总结,以资共享!渴望更正和补充! ...

  9. Yii框架和Vue的完美结合完成前后端分离项目

    背景说明 本文假设你对Yii和Vue都比较熟悉,至少都在项目里用过,另外笔者新人,以后不定时放一些干货,欢迎程序媛关注 Yii是一个PHP全端框架,典型的mvc的项目结构,后端接口都是一个控制器里放了 ...

  10. Spring中操作Hibernate的几种方式

    1.直接操作模版方式HQL: //通过spring的模版方式来操作Hibernate的HQL语句 return this.getHibernateTemplate().find("from ...