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+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的更多相关文章
- [POJ 3150] Cellular Automaton (矩阵高速幂 + 矩阵乘法优化)
Cellular Automaton Time Limit: 12000MS Memory Limit: 65536K Total Submissions: 3048 Accepted: 12 ...
- POJ 3150 Cellular Automaton(矩阵快速幂)
Cellular Automaton Time Limit: 12000MS Memory Limit: 65536K Total Submissions: 3504 Accepted: 1421 C ...
- POJ - 3150 :Cellular Automaton(特殊的矩阵,降维优化)
A cellular automaton is a collection of cells on a grid of specified shape that evolves through a nu ...
- POJ 3150 Cellular Automaton(矩阵高速幂)
题目大意:给定n(1<=n<=500)个数字和一个数字m,这n个数字组成一个环(a0,a1.....an-1).假设对ai进行一次d-step操作,那么ai的值变为与ai的距离小于d的全部 ...
- POJ 3150 Cellular Automaton --矩阵快速幂及优化
题意:给一个环,环上有n块,每块有个值,每一次操作是对每个点,他的值变为原来与他距离不超过d的位置的和,问k(10^7)次操作后每块的值. 解法:一看就要化为矩阵来做,矩阵很好建立,大白书P157页有 ...
- POJ 3150 Cellular Automaton(矩阵乘法+二分)
题目链接 题意 : 给出n个数形成环形,一次转化就是将每一个数前后的d个数字的和对m取余,然后作为这个数,问进行k次转化后,数组变成什么. 思路 :下述来自here 首先来看一下Sample里的第一组 ...
- 【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 ...
- UVA 1386 - Cellular Automaton(循环矩阵)
UVA 1386 - Cellular Automaton option=com_onlinejudge&Itemid=8&page=show_problem&category ...
- UVA1386 【Cellular Automaton】题解
题面:UVA1386 Cellular Automaton 矩阵乘法+快速幂解法: 这是一个比较裸的有点复杂需要优化的矩乘快速幂,所以推荐大家先做一下下列洛谷题目练练手: (会了,差不多就是多倍经验题 ...
随机推荐
- 《squid网卡代理的实现》RHEL6.3——条理清晰,步骤明确
网卡的代理和FQ差不多一个道理.ping 不通不代表不能上网. 实验的目的: 打开2台虚拟主机,一台2个网卡(这台为服务器):一台一个网卡: 2块网卡的服务器使用静态ip可以上网,也就是说网段为1的可 ...
- [大牛翻译系列]Hadoop(16)MapReduce 性能调优:优化数据序列化
6.4.6 优化数据序列化 如何存储和传输数据对性能有很大的影响.在这部分将介绍数据序列化的最佳实践,从Hadoop中榨出最大的性能. 压缩压缩是Hadoop优化的重要部分.通过压缩可以减少作业输出数 ...
- 如何查看 Apache 的版本
查看 Apache 服务器版本的命令行为: httpd -v 或者 apachectl -v 例如:用 Xshell 连接到服务器后,输入:httpd -v 或者:apachectl -v 返回: S ...
- openerp学习笔记 视图更新时删除已存在的菜单或其他对象
删除菜单示例: <delete id="base.menu_module_updates" model="ir.ui.menu"/><dele ...
- [转]MAC下JDK版本的切换
系统里之前先安装里jdk6的,后台又装里7,安装完成后,java -version 版本是7, 导致我eclipse打不开,一开始的做法是,把7的版本给删除掉. 删除的方法也很简单,在命令行中到 / ...
- 第六周 E题 期望.....
Description Given a dice with n sides, you have to find the expected number of times you have to thr ...
- Hive表分区
必须在表定义时创建partition a.单分区建表语句:create table day_table (id int, content string) partitioned by (dt stri ...
- UserLogin
DAL: IUserDAL namespace Dal { /// <summary> /// This interface is defined for user functions. ...
- (转)c指针
转自:http://www.cnblogs.com/wchhuangya/archive/2009/12/24/1631121.html 这两天开始搞BREW了,用的是C的语法.上学时学过的C都还给学 ...
- C# Windows - 菜单栏和工具栏
除了MenuStrip控件之外,还有许多控件可用于填充菜单.3个常见的控件是ToolStripMenuItem,ToolStripDropDown,和ToolStripSeparator.这些控件表示 ...