Cellular Automaton
Time Limit: 12000MS   Memory Limit: 65536K
Total Submissions: 3048   Accepted: 1227
Case Time Limit: 2000MS

Description

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. Theorder
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 orderm.
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

Source


题目大意:
有一个环,长度为n。当中每个结点上面都有数字。能够对环上的每个结点进行更新,使得该结点的值变为环上距离该结点的距离小于等于d的全部节点数之和,最后再对m取模。每次操作更新一遍环上全部点,问经过k次操作之后,环上各点数字为多少。

解题思路:
  首先能够想到用矩阵来取代操作。(对于题目第一组例子)
首先矩阵a = 1 2 2 1 2 

b = 

1 1 0 0 1

1 1 1 0 0

0 1 1 1 0

0 0 1 1 1

1 0 0 1 1
然后能够通过矩阵高速幂来解决。这样复杂度为(logk * n^3),会T。

事实上这个矩阵是有规律的。
b^1 =

[1, 1, 0, 0, 1]

[1, 1, 1, 0, 0]

[0, 1, 1, 1, 0]

[0, 0, 1, 1, 1]

[1, 0, 0, 1, 1]

b^2 =

[3, 2, 1, 1, 2]

[2, 3, 2, 1, 1]

[1, 2, 3, 2, 1]

[1, 1, 2, 3, 2]

[2, 1, 1, 2, 3]

b^3 =

[7, 6, 4, 4, 6]

[6, 7, 6, 4, 4]

[4, 6, 7, 6, 4]

[4, 4, 6, 7, 6]

[6, 4, 4, 6, 7]

b^4 =

[19, 17, 14, 14, 17]

[17, 19, 17, 14, 14]

[14, 17, 19, 17, 14]

[14, 14, 17, 19, 17]

[17, 14, 14, 17, 19]
观察能够发现,仅仅要把矩阵第i行的第一个数字移到最后,就变成了矩阵的第 i + 1 行。所以我们仅仅须要知道矩阵的某一行,就能够推得其它行。所以N^3的复杂度就将为N^2。

代码:
/*
ID: wuqi9395@126.com
PROG:
LANG: C++
*/
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<cmath>
#include<cstdio>
#include<vector>
#include<string>
#include<fstream>
#include<cstring>
#include<ctype.h>
#include<iostream>
#include<algorithm>
#define INF (1<<30)
#define PI acos(-1.0)
#define mem(a, b) memset(a, b, sizeof(a))
#define For(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
using namespace std;
const int maxn = 505;
const int maxm = 505;
int mod, n, k, d;
void Matrix_pow(int a[], int b[]) {
ll c[505] = {0};
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) c[i] += ((ll)a[j] * b[(i + j) % n]) % mod;
c[i] %= mod;
}
for (int i = 0; i < n; i++) b[i] = c[i];
}
int a[505], b[505];
int main () {
while(scanf("%d%d%d%d", &n, &mod, &d, &k) != EOF) {
for (int i = 0; i < n; i++) scanf("%d", b + i);
memset(a, 0, sizeof(a));
a[0] = 1;
for (int i = 1; i <= d; i++) a[i] = a[n - i] = 1;
while(k) {
if (k & 1) Matrix_pow(a, b);
k >>= 1;
Matrix_pow(a, a);
}
for (int i = 0; i < n; i++) printf("%d%c", b[i], " \n"[i == n - 1]);
}
return 0;
}

參考:http://www.cppblog.com/varg-vikernes/archive/2011/02/08/139804.html


[POJ 3150] Cellular Automaton (矩阵高速幂 + 矩阵乘法优化)的更多相关文章

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

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

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

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

  3. POJ - 3150 :Cellular Automaton(特殊的矩阵,降维优化)

    A cellular automaton is a collection of cells on a grid of specified shape that evolves through a nu ...

  4. poj 3735 Training little cats 矩阵快速幂+稀疏矩阵乘法优化

    题目链接 题意:有n个猫,开始的时候每个猫都没有坚果,进行k次操作,g x表示给第x个猫一个坚果,e x表示第x个猫吃掉所有坚果,s x y表示第x个猫和第y个猫交换所有坚果,将k次操作重复进行m轮, ...

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

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

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

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

  7. 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+ ...

  8. poj 2778 AC自己主动机 + 矩阵高速幂

    // poj 2778 AC自己主动机 + 矩阵高速幂 // // 题目链接: // // http://poj.org/problem?id=2778 // // 解题思路: // // 建立AC自 ...

  9. poj 3233(矩阵高速幂)

    题目链接:http://poj.org/problem?id=3233. 题意:给出一个公式求这个式子模m的解: 分析:本题就是给的矩阵,所以非常显然是矩阵高速幂,但有一点.本题k的值非常大.所以要用 ...

随机推荐

  1. iebook 发布到网站 独家秘诀

    iebook 普通版只能产生exe文件,无法生成web公布的文件需要,因此,我们需要专业版. iebook2011版本并没有破解版,下面是一个iebook2010破解版: http://downloa ...

  2. [Machine Learning (Andrew NG courses)]IV.Linear Regression with Multiple Variables

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvenFoXzE5OTE=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA ...

  3. Android:Drag and Drop的应用

    最近看了下Drag and Drop部分的原文,觉得很有意思就像自己试着做一下,说实在的原文真的是不好读啊,要感谢那些为我们发表译文的大神们, 真的是不容易,原文中给了例子,但是只有后面零星的代码,真 ...

  4. spring mvc 接受多对象的处置

    spring mvc 接受多对象的处理 spring mvc感觉非常好用,尤其是对接收对象參数的自己主动绑定非常简便,但对于同一时候传多个对象时有些困扰. 同一时候项目并没有直接使用spring的fo ...

  5. 让工程师爱上CMM,实现管理于无形 --- 中标软件CMMI L5之路 (2/2)

    上节:见 http://blog.csdn.net/sztiger168/article/details/9142069 使用 青铜器RDM在2011年正式在中标软件上线,将研发业务全面流程化,完全支 ...

  6. BCB/Delphi中常用的VCL函数说明(字符串函数)

    本文档是ccrun(老妖)根据网上资料整理而成. --------------------内存分配--------------------函数名称:AllocMem函数说明:在队中分配指定字节的内存块 ...

  7. Delphi颜色的表示(一共5种表示法)

    //全以红色举例: //1. RGB 模式:Self.Color := $0000ff; //不过和HTML.PhotoShop.FireWorks中的 #ff0000 是完全反的,应该叫 BGR. ...

  8. Please verify you invoked Maven from the correct directory

    解决办法: 在cmd中,把当前路径转换到一个含有pom文件的 项目路径下 再使用 类似下面的deploy就行 mvn deploy:deploy-file -DgroupId=com.taobao.n ...

  9. Javascript selection的兼容性写法介绍

    本文为大家讲解下Javascript selection的兼容性写法,感兴趣的朋友可以参考下 function getSelectedText() { //this function code is ...

  10. 对数的操作 开始我的JAVA历程

    package Text; public class Sumn { public static void main (String args[]){ System.out.println(" ...