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. Flex Label自动截取、自动换行

    label.maxDisplayedLines=0;      // 默认多行显示,不截取 label.maxDisplayedLines=1;     //任意整数,显示单行文本,自动截取(...) ...

  2. Ajax - 异步调用后台程序 -JSON

    在ASP.NET使用ajax时基本上每个操作都要新建一个.ashx处理程序,页面很多,每个页面的操作也很多,这样的话项目就会产生新建很多很多的.ashx页面,能不能把方法写在后台中,然后Jquery直 ...

  3. CF 327D - Block Tower 数学题 DFS 初看很难,想通了就感觉很简单

    D. Block Tower time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...

  4. [Java][Android][Process] ProcessBuilder与Runtime差别

    在Android中想要进行Ping,在不Root机器的情况下似乎还仅仅能进行底层命调用才干实现. 由于在Java中要进行ICMP包发送须要Root权限. 于是仅仅能通过创建进程来攻克了.创建进程在Ja ...

  5. Delphi TStream 详细介绍

    Delphi TStream 详细介绍Stream对象,又称流式对象,是TStream.THandleStream.TFileStream.TMemoryStream.TResourceStream和 ...

  6. 在iPhoneApp中加载PDF

    原文: http://ios.biomsoft.com/2012/02/17/load-a-pdf-file-in-the-iphone-app-smoothly/ 本节将学习如何从服务器加载 pdf ...

  7. UNIX下改动时间简单一例

    UNIX下改动时间简单一例 仅仅输入date就显示当前的工作站时间,假设有root超级用户权限,加上參数能够改动 工作站的时间. 格式:date mmddHHMM[cc]yy mm--月份,dd--日 ...

  8. RealThinClient学习(一)

    服务端代码: unit RtcHttpServer; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, ...

  9. 【智能家居篇】wifi网络接入原理(上)——扫描Scanning

    转载请注明出处:http://blog.csdn.net/Righthek 谢谢! 对于低头党来说,在使用WIFI功能时,常常性的操作是打开手机上的WIFI设备,搜索到心目中的热点,输入passwor ...

  10. java泛型 之 入门(interface)

    一:泛型简单介绍: (1)所谓泛型,就是变量类型的參数化. 泛型是JDK1.5中一个最重要的特征.通过引入泛型,我们将获得编译时类型的安全和执行时更小的抛出ClassCastException的可能. ...