矩阵快速幂。

样例是这样构造矩阵的:

矩阵很好构造,但是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的更多相关文章

  1. UVA 1386 - Cellular Automaton(循环矩阵)

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

  2. UVa 3704 Cellular Automaton(矩乘)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=15129 [思路] 矩阵乘法-循环矩阵 题目中的转移矩阵是一个循环矩 ...

  3. 【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 ...

  4. [POJ 3150] Cellular Automaton (矩阵高速幂 + 矩阵乘法优化)

    Cellular Automaton Time Limit: 12000MS   Memory Limit: 65536K Total Submissions: 3048   Accepted: 12 ...

  5. UVA1386 【Cellular Automaton】题解

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

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

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

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

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

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

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

  9. uva 620 Cellular Structure

    题目连接:620 - Cellular Structure 题目大意:给出一个细胞群, 判断该细胞的可能是由哪一种生长方式的到的, 输出该生长方式的最后一种生长种类, "SIMPLE&quo ...

随机推荐

  1. ShellExecute快捷键大全

    文件夹,文件,网址可以创建快捷方式,控制面板 中的设置也可以创建快捷方式,下面是快捷方式的命令,使用方法:在桌面或文件夹的空白处点右键,选择新建,快捷方式,在"请键入项目的位置"输 ...

  2. Drawcli分析

    当前环境:windows7 32位旗舰版.VS2010旗舰版 Drawcli介绍: Drawcli是VS2010中的一个示例程序,能够进行简单的绘图操作,例如线.矩形.圆角矩形.多边形等,位于VS安装 ...

  3. [转]JSONObject与JSONArray的使用

    http://www.cnblogs.com/xwdreamer/archive/2011/12/16/2296904.html 参考文献: http://blog.csdn.net/huangwuy ...

  4. CodeForces--TechnoCup--2016.10.15--ProblemB--Bill Total Value(字符串处理)

    Bill Total Value time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  5. java 对象流

    TV.java import java.io.*; public class TV implements Serializable{ String name; int price; public vo ...

  6. 使用命令行编译as文件成swf

    设置环境变量到flex sdk的目录下.如:D:\Program Files\Adobe Flash Builder 4.5\sdks\flex_sdk_4.6\bin 找到flex-config.x ...

  7. android touchEvent事件学习

    学习网址:http://www.apkbus.com/forum.php?mod=viewthread&tid=44296 1:Android Touch事件传递机制解析 android系统中 ...

  8. linux下的vim使用笔记

    环境:window下可以使用gvim编辑软件 学习主要是在ubuntu15敲击命令学习的视频来自于智普教育vim使用视频1. sudo apt show vi 查看安装的vi版本,当然了我的ubunt ...

  9. springMVC下载文件前修改文件名字

    很多时候,为了方便,下载文件其实就直接写了一个文件在服务器上面的路径,然后直接点击一个这个地址,浏览器就自然而然的开始下载了. 但是这次项目需要在文件下载之前修改文件的名字,也就是说,服务器上文件的名 ...

  10. BootStrap详解之(二)

    六.内容 Bootstrap 将全局 font-size 设置为 14px,line-height 设置为 1.428 中心内容 .lead 将字体大小.加粗.行高修改 (无卵高深用) 标记 mark ...