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

如果用矩阵相乘来描述,那就可以表述为1xN和NxN的矩阵相乘,结果仍为1xN矩阵
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
a * b = 5 5 5 5 4
所以最终结果就是:a * (b^k)

线性代数不合格的同鞋表示压力很大。。

对一个NxN矩阵求k次方,而且这个k很大,N也不小,怎么办?
所以有高手观察到了,这个矩阵长得有点特殊,可以找到一些规律:
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]

发现神马没有。就是无论是b的几次幂,都符合A[i][j] = A[i-1][j-1]
高手说是这样推倒出来地:
““”
利用矩阵A,B具有a[i][j]=A[i-1][j-1],B[i][j]=B[i-1][j-1](i-1<0则表示i-1+n,j-1<0则表示j-1+n)
我们可以得出矩阵C=a*b也具有这个性质
C[i][j]=sum(A[i][t]*B[t][j])=sum(A[i-1][t-1],B[t-1][j-1])=sum(A[i-1][t],B[t][j-1])=C[i-1][j-1]
“”“

这样就可以开一个N大小的数组来存放每次计算的结果了。而没必要用NxN。
N的问题解决了,但是k还是很大,怎么办?

这时候可以用二分法来求b^k
b^k = b^1 * b^4 * b^16 。。。

计算过程中,必定会出现数字大于M的情况。
切记 x*y = (x%M)*(y%M)

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<stdlib.h>
#include<cstring>
#include<vector>
#define ll __int64
#define pi acos(-1.0)
#define Max 50000
using namespace std;
ll bn[],temp[];
int m,n,d,k;
void mul(ll a[],ll b[])
{
int i,j;
ll ans[];
for (i=;i<n;i++)
for (j=ans[i]=;j<n;j++)
ans[i] += a[j]*b[i>=j?(i-j):(n+i-j)];
for (i=;i<n;b[i]=ans[i++]%m);
}
int main(){
int i;
scanf("%d%d%d%d",&n,&m,&d,&k);
for (i=;i<n;i++)
scanf("%I64d",&bn[i]);
for (temp[]=i=;i<=d;i++)
temp[i]=temp[n-i]=;
while(k){
if (k&) mul(temp,bn);
mul(temp,temp);
k>>=;
}
for(i=;i<n;i++)
if(i) printf(" %I64d",bn[i]);
else printf("%I64d",bn[i]);
printf("");
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(特殊的矩阵,降维优化)

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

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

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

  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(矩阵乘法+特殊的技巧)

    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. UVA 1386 - Cellular Automaton(循环矩阵)

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

  9. UVA1386 【Cellular Automaton】题解

    题面:UVA1386 Cellular Automaton 矩阵乘法+快速幂解法: 这是一个比较裸的有点复杂需要优化的矩乘快速幂,所以推荐大家先做一下下列洛谷题目练练手: (会了,差不多就是多倍经验题 ...

随机推荐

  1. POJ1056 IMMEDIATE DECODABILITY【数据结构】

    题目地址:http://poj.org/problem?id=1056 Description An encoding of a set of symbols is said to be immedi ...

  2. ADO.NET笔记——将DataReader作为函数返回值

    相关知识: 在很多情况下,可能把数据库的访问封装到一个函数中,通过该函数返回一个DataReader对象给调用者.例如定义函数:SqlDataReader returnDR(),然后再Main函数中调 ...

  3. 匹配一级分类和二级分类 名字和url 里面有玄机

    1:要匹配的html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:// ...

  4. ASP.NET MVC局部验证及相关问题

    在上一篇“asp.net mvc常用的数据注解和验证以及entity framework数据映射”话题中,有的博友提到 ‘“同一个实体在3-4个地方会发生修改,每个修改需要验证的方式都不一样,后端就不 ...

  5. Winform ListView根据条件定位到指定行

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  6. jquery鼠标滑过提示title具体实现代码

    jquery鼠标滑过提示title的实现代码. 如下: <script type="text/javascript" src="http://ajax.google ...

  7. 使用dxNavBar动态创建应用程序菜单

    一.如何动态创建dxNavBar内容: function TMain.GetAcitonByCaption(const aCategory,aCaption: string): Integer; va ...

  8. C#不同页面之间通信的方法

    以前做项目的时候经常头疼两个页面之间的交互(汗),这几天看的MVVM项目,忽然感觉好简单的!我自己写了个简单的demo 可以简单实现2个页面之间的交互,新人第一次发博客,不喜勿喷 代码很简单,注释我就 ...

  9. 相同的 birthday

    Description Sometimes some mathematical results are hard to believe. One of the common problems is t ...

  10. Hadoop命令摘录

    一:文件操作 1.建立目录 [hadoop@hadoop1:hadoop]$bin/hadoop dfs -mkdir testdir 在HDFS中建立一个名为testdir的目录 2.上传文件到HD ...