矩阵快速幂。

样例是这样构造矩阵的:

矩阵很好构造,但是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. mysql中的substr()函数

    mysql中的substr()函数和hibernate的substr()参数都一样,就是含义有所不同. 用法: substr(string string,num start,num length); ...

  2. 删除 win8.1中的网络1,网络2,宽带连接1,宽带连接2等网络记录

    新建txt文本,保存如下内容为reg文档: Windows Registry Editor Version 5.00 [-HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\W ...

  3. IDAPython: importing “site” failed

    问题:IDA启动时,弹出IDAPython: importing “site” failed对话框. 解决办法:环境变量添加PYTHONHOME,值为python安装路径,比如:C:\Python27

  4. 深入解析FileInputStream和FileOutputStream

    http://swiftlet.net/archives/1363 FileInputStream和FileOutputStream类属于字节类,可以操作任意类型的文件.在数据流的处理过程中,有两种情 ...

  5. PAT (Advanced Level) 1098. Insertion or Heap Sort (25)

    简单题.判断一下是插排还是堆排. #include<cstdio> #include<cstring> #include<cmath> #include<ve ...

  6. C++:预处理指令

    Preprocessor directives 预处理器指令 预处理器指令是指那些包含在我们代码中的预处理器语句行,这些预处理器语句不是真正的代码语句,但是他们指导程序如何进行编译.这些语句总是以 ‘ ...

  7. MFC实现为窗体添加的背景图片

    将一个bmp图片添加到资源中 在资源视图中更改位图资源的ID为IDB_BITMAP_BACKGROUND. 第一种方法: 在Dialog中添加一个Picture Control控件,将Picture ...

  8. PAT1008

    1008. Elevator (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B The highest building in our city has on ...

  9. margin:0 auto在ie7浏览器里面无效

    把文件头改成 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w ...

  10. Dubbo阿里Alibaba开源的分布式服务框架

    [获奖公布]"我的2016"主题征文活动    程序猿全指南,让[移动开发]更简单!      [观点]移动原生App开发和HTML 5开发,你更看好哪个?   博客的神秘功能 D ...