UVA 1386 Cellular Automaton
矩阵快速幂。
样例是这样构造矩阵的:

矩阵很好构造,但是500*500*500*logk的复杂度显然是无法通过这题的。
其实本题构造出来的矩阵是一个循环矩阵,只需直到第一行或者第一列,即可直到整个矩阵是怎么样的。
所以,中间计算的时候,需要直到第一行是什么即可,即1*n的矩阵乘n*n的矩阵。时间复杂度o(n*n*logk)
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<algorithm>
using namespace std; const int maxn=+;
int n,d,k;
long long mod;
long long z[maxn],a[maxn];
long long y[maxn],x[maxn];
long long tmp[maxn][maxn];
long long r[maxn]; void init()
{
memset(y,,sizeof y);
y[]=;
memset(x,,sizeof x);
x[]=;
for(int i=; i<=+d; i++) x[i]=;
for(int i=n; i>=n-d+; i--)x[i]=;
} int main()
{
while(~scanf("%d%lld%d%d",&n,&mod,&d,&k))
{
for(int i=; i<=n; i++) scanf("%lld",&z[i]);
init();
while(k)
{
for(int i=; i<=n; i++)
{
int tot=;
for(int j=i; j<=n; j++) tmp[i][j]=x[tot++];
for(int j=; j<=i-; j++) tmp[i][j]=x[tot++];
} if(k%==)
{
for(int j=; j<=n; j++)
{
long long sum=;
for(int t=; t<=n; t++) sum=(sum+(y[t]*tmp[j][t])%mod)%mod;
r[j]=sum;
} for(int j=; j<=n; j++) y[j]=r[j]; k--;
}
else if(k%==)
{
for(int j=; j<=n; j++)
{
long long sum=;
for(int t=; t<=n; t++) sum=(sum+(x[t]*tmp[j][t])%mod)%mod;
r[j]=sum;
}
for(int j=; j<=n; j++) x[j]=r[j]; k=k/;
}
} for(int i=; i<=n; i++)
{
int tot=;
for(int j=i; j<=n; j++) tmp[i][j]=y[tot++];
for(int j=; j<=i-; j++) tmp[i][j]=y[tot++];
} for(int j=; j<=n; j++)
{
long long sum=;
for(int t=; t<=n; t++) sum=(sum+(z[t]*tmp[j][t])%mod)%mod;
r[j]=sum;
} for(int j=; j<=n; j++)
{
printf("%d",r[j]);
if(j<n) printf(" ");
else printf("\n");
}
}
return ;
}
UVA 1386 Cellular Automaton的更多相关文章
- UVA 1386 - Cellular Automaton(循环矩阵)
UVA 1386 - Cellular Automaton option=com_onlinejudge&Itemid=8&page=show_problem&category ...
- UVa 3704 Cellular Automaton(矩乘)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=15129 [思路] 矩阵乘法-循环矩阵 题目中的转移矩阵是一个循环矩 ...
- 【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 ...
- [POJ 3150] Cellular Automaton (矩阵高速幂 + 矩阵乘法优化)
Cellular Automaton Time Limit: 12000MS Memory Limit: 65536K Total Submissions: 3048 Accepted: 12 ...
- UVA1386 【Cellular Automaton】题解
题面:UVA1386 Cellular Automaton 矩阵乘法+快速幂解法: 这是一个比较裸的有点复杂需要优化的矩乘快速幂,所以推荐大家先做一下下列洛谷题目练练手: (会了,差不多就是多倍经验题 ...
- 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的全部 ...
- uva 620 Cellular Structure
题目连接:620 - Cellular Structure 题目大意:给出一个细胞群, 判断该细胞的可能是由哪一种生长方式的到的, 输出该生长方式的最后一种生长种类, "SIMPLE&quo ...
随机推荐
- org.apache.lucene.queryParser.ParseException: Encountered "<EOF>" at line 1, column 0.
如果出现了下列错误,那是因为用错了函数.把queryParser.Query改称queryParser.parse就通过了 org.apache.lucene.queryParser.ParseExc ...
- PAT1027
People in Mars represent the colors in their computers in a similar way as the Earth people. 火星人在他们的 ...
- 完整的 HTML 4 + HTML 5 实体参考手册
1 完整的 HTML 4 + HTML 5 实体参考手册 http://www.runoob.com/charsets/html-charsets.html 1 下表中的所有实体都能在所有的浏览器中正 ...
- HTML center tag
<center>This text will be center-aligned.</center> 或者可以把一个div给center了,例如将一个html表格给center ...
- 【转】bootstrap 的 affix.js 插件
我觉得 Twitter Bootstrap 提供的 JavaScript 插件可用性非常高,但文档多数简单.按部就班操作了,有效果当然好,没效果的话简直不知道为什么.之前我写过 Scrollspy 用 ...
- Java 错误:找不到或无法加载主类
环境变量的配置有问题,你改改吧:HOME是 D:\Java\jdk1.8.0_11 Path是 %JAVA_HOME%\bin;%JAVA_HOME%\jre\bin CLASSPATH是 .;%JA ...
- 关于SVN 提交一半卡死的问题
解决方案 1:将项目刷新一下 2:然后在到 project - clean 一下项目 3:之后提交svn 不过暂时不能确认 要等下方进度条的 svn更新状态完成 在点击确认进行提交 4:Buildin ...
- 转 通过 spring 容器内建的 profile 功能实现开发环境、测试环境、生产环境配置自动切换
软件开发的一般流程为工程师开发 -> 测试 -> 上线,因此就涉及到三个不同的环境,开发环境.测试环境以及生产环境,通常 ...
- UIImageView动画
NSMutableArray *arrM = [NSMutableArray array]; // 2.加载所有图片 ; i <= ; i++) { NSString *imageName = ...
- 关于js的一些基本知识(类,闭包,变量)
这里写的都是些杂知识,包括私有,类,闭包这些js不可避免的东西,感觉自己有可能会误人子弟.所以有觉得写错了的读者,希望可以及时评论告诉我.我可以及时更正.多谢大家了 1.关于类的创建 类的创建大致可以 ...