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 ...
 
随机推荐
- Java中的五种单例模式实现方法
			
[代码] Java中的五种单例模式实现方法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 2 ...
 - isinstance使用方法
			
#!/usr/bin/python2.7 def displayNumType(num): print num, 'is', if isinstance(num,(int, long ...
 - linker command failed with exit code 1 (use -v to see invocation)修改方法
			
它的意思是指文件丢失或者文件重复,你就要仔细查看项目中有没有重复的文件,如有重复修改即可,如果是文件丢失,那就麻烦啦!你需要根据报错的地方进行修补文件,最为直接的方法是重新创建项目.还有另外一种情况是 ...
 - HDU 5768 Lucky7  (容斥原理 + 中国剩余定理 + 状态压缩 + 带膜乘法)
			
题意:……应该不用我说了,看起来就很容斥原理,很中国剩余定理…… 方法:因为题目中的n最大是15,使用状态压缩可以将所有的组合都举出来,然后再拆开成数组,进行中国剩余定理的运算,中国剩余定理能够求出同 ...
 - ExtJS4 的dom
			
Ext使用了三个核心的工具类对我们掌握的DOM进行了完美的封装. ┣ Ext.Element(几乎对DOM的一切进行了封彻底装) ┣ Ext.DomHelper(一个强大的操控UI界面的工具类) ┣ ...
 - Adobe Acrobat 9 Pro 注册码
			
来自百度知道,记录与此,以备后用http://zhidao.baidu.com/question/177914535.html 如果你的系统盘是C盘,那么就删除:c:/Documents and Se ...
 - HDU 1890 Robotic Sort | Splay
			
Robotic Sort Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) [Pr ...
 - zepto为什么不支持animate,报animate is not a function
			
在zepto.min.js文件中搜索animate看有没有,如果没有就是没有加入animate的模块 解决办法,去github中打开src/文件夹,找到fx.js文件,把内容追加到zepto.min. ...
 - 苹果充电器USB端的识别电阻的设置
			
苹果为充电器定义了3种充电电流,分别是0.5A/1A/2.1A.具体是由3种不同的电阻组合来实现的.当苹果的设备ipad,iphone,ipod接入USB口充电器时,会先检测USB D+和D-上的电压 ...
 - 【简单dp】 poj 2346
			
题意:给定一个N 求一共有多少个N位数 前N/2个数的和等于后N/2个数的和思路:令F[i][j] 为sum值为j的i位数的个数则问题转化成 求 sum(F[n/2][j] * F[n/2][ ...